5

On Ubuntu Desktop 13.10, I have a setup script which runs as root. It essentially configures the computer as necessary, but I need to set some things with dconf. The script runs in a Terminal window under a regular user with sudo. I need to run the command dconf write /com/canonical/unity/launcher/favorites "['unity://expo-icon', <...>']". (It is run with a variety of different settings.)

I have tested the commands as the login user, and they work just fine. However, when I am logged in as root, I cannot do sudo -u bitc dconf write <...>; I get errors like error: Could not connect: Connection refused or unable to create directory '/root/.cache/conf'.

The script must be run as root because of some other tasks it performs, but it appears that dconf must be run under the specific login session, not just the user, to configure it. I would rather not have a separate script for dconf, but that is possible. Is there a way to attach to the login session (or some other way to set the dconf keys for that user)?

(The script is written in javascript with node.js.)

  • 1
    Doesn't dconf need an X session owned by the user in question to run? – terdon Mar 9 '14 at 3:23
3

Is it sufficient to do this?

sudo -u bitc HOME=/home/bitc dconf write <...>
             ^^^^^^^^^^^^^^^

I was trying to do something similar. I have a script that connects via SSH and runs as root. I want it to change some settings for a user. This seems to work for me:

sudo -u vagrant HOME=/home/vagrant dbus-launch --exit-with-session gsettings set org.freedesktop.ibus.general use-system-keyboard-layout true

I think that's potentially bad because it's spawning a new dbus-daemon for the duration of the this session, but without the dbus-launch I got dbus errors. Perhaps these are specific to gsettings, but I want to mention it in case it helps.

  • Adding the HOME definition saved my day, thanks :) (I'm using dconf from puppet to configure multiple users) – Joril Jul 28 '16 at 8:36
0

I have a POST-Install script that sets my gsetting. Because I run the script as sudo the EUID is 0, there fore I have to find the $RUID (Real User ID).

here is my approach:

#!/usr/bin/env bash
# Get the Real Username
RUID=$(who | awk 'FNR == 1 {print $1}')

# Translate Real Username to Real User ID
RUSER_UID=$(id -u ${RUID})

# Set gsettings for the Real User
sudo -u ${RUID} DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/${RUSER_UID}/bus" gsettings set org.gnome.desktop.interface clock-show-date false

exit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.