How to run gsettings for another user Ubuntu 18.04.2 LTS
I am trying to run gsettings for another user in Ubuntu 18.04.2 LTS. Specifically, I am attempting to prevent the user's screen from locking. This will be run as part of a bash script. The commands I am using are:
su someuser
dbus-launch gsettings set org.gnome.desktop.screensaver lock-enabled false
Because this is run via ssh, I am opening with dbus-launch to start dbus then attempting a simple call to gsettings. However, I get the error:
dbus[22652]: Unable to set up transient service directory: XDG_RUNTIME_DIR "/run/user/1000" is owned by uid 1000, not our uid 1001
(process:22650): dconf-CRITICAL **: 11:11:27.830: unable to create directory '/run/user/1000/dconf': Permission denied. dconf will not work properly.
(process:22650): dconf-CRITICAL **: 11:11:27.830: unable to create directory '/run/user/1000/dconf': Permission denied. dconf will not work properly.
(process:22650): dconf-CRITICAL **: 11:11:27.836: unable to create directory '/run/user/1000/dconf': Permission denied. dconf will not work properly.
To confirm that the UIDs are being mismatched I checked the /run/user directory:
ls -lah /run/user
Which outputs:
total 0
drwxr-xr-x 4 root root 80 Apr 16 14:25 .
drwxr-xr-x 31 root root 900 Apr 16 14:25 ..
drwx------ 4 adminuser adminuser 100 Apr 16 14:25 1000
drwx------ 11 someuser someuser 260 Apr 16 12:26 1001
I also tried using sudo:
sudo -u "someuser" dbus-launch gsettings set org.gnome.desktop.screensaver lock-enabled false
Which gives the errors:
(process:22264): dconf-CRITICAL **: 14:33:41.124: unable to create directory '/home/adminuser/.cache/dconf': Permission denied. dconf will not work properly.
(process:22264): dconf-CRITICAL **: 14:33:41.124: unable to create directory '/home/adminuser/.cache/dconf': Permission denied. dconf will not work properly.
(process:22264): dconf-CRITICAL **: 14:33:41.135: unable to create directory '/home/adminuser/.cache/dconf': Permission denied. dconf will not work properly.
(process:22264): dconf-WARNING **: 14:33:41.152: failed to commit changes to dconf: GDBus.Error:org.gtk.GDBus.UnmappedGError.Quark._g_2dfile_2derror_2dquark.Code2: Cannot open dconf database: Failed to open file “/home/adminuser/.config/dconf/user”: Permission denied
Which leads to the question, why is gsettings trying to run for adminuser instead of someuser and how can it be directed to run for someuser by adminuser over SSH?
Solution 1:
The issue here is related to Why should users never use normal sudo to start graphical applications? i.e. that before Ubuntu 19.10, the default sudo
configuration does not change $HOME
to that of the target user.
You can change that using the -H
(--set-home
) option:
-H, --set-home Request that the security policy set the HOME environment variable to the home directory specified by the target user's password database entry. Depending on the policy, this may be the default behavior.
So
sudo -Hu someuser dbus-launch gsettings set org.gnome.desktop.screensaver lock-enabled false
Adding the -H
option should not be necessary in newer versions: see How does sudo handle $HOME differently since 19.10?