setting gsettings of other user with sudo
Categories:
Setting GSettings for Another User with Sudo on Ubuntu

Learn how to securely modify GSettings for a different user account using sudo
and dbus-launch
on Ubuntu and similar Debian-based systems, covering common pitfalls and best practices.
Modifying GSettings for the currently logged-in user is straightforward using the gsettings
command. However, when you need to change GSettings for another user account, especially from a script or an administrative context, the process becomes more complex. This article will guide you through the necessary steps to achieve this securely and effectively on Ubuntu and other Debian-based distributions, leveraging sudo
and dbus-launch
to interact with the target user's D-Bus session.
Understanding the Challenge: D-Bus and User Sessions
GSettings relies on D-Bus, an inter-process communication system, to interact with the user's desktop environment. When you run gsettings
as root
(via sudo
), it typically operates within the root
user's D-Bus session, not the target user's. This means direct sudo gsettings set otheruser ...
commands will fail or modify root
's settings, which is not the desired outcome. To correctly modify another user's GSettings, you need to execute the gsettings
command within that user's D-Bus session context.
flowchart TD A[Admin User (sudo)] --> B{Attempt gsettings for Other User} B --> C{Is D-Bus session active for Other User?} C -- No --> D[Error: Cannot connect to D-Bus] C -- Yes --> E[Execute gsettings in Other User's D-Bus context] E --> F[GSettings modified for Other User]
Flowchart illustrating the challenge of setting GSettings for another user.
The Solution: sudo
, dbus-launch
, and Environment Variables
The key to success lies in two main components: sudo -u <target_user>
to switch to the target user, and dbus-launch
to ensure the gsettings
command runs within a D-Bus session that the target user owns. Additionally, you'll need to correctly set the DBUS_SESSION_BUS_ADDRESS
environment variable, which tells dbus-launch
where to find the target user's D-Bus session.
sudo -u <target_user> DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u <target_user>)/bus" dbus-launch gsettings set org.gnome.desktop.interface gtk-theme 'Yaru-dark'
Example command to set the GTK theme for a target user.
/run/user/$(id -u <target_user>)/bus
is a common location for the user's D-Bus session socket. The $(id -u <target_user>)
part dynamically retrieves the User ID (UID) of the target user, making the command robust.Step-by-Step Implementation
Let's break down the process into actionable steps. For this example, we'll assume the target user is named testuser
and you want to change their GTK theme to 'Yaru-dark'.
1. Identify the Target User's UID
First, you need the User ID (UID) of the target user. This is crucial for constructing the correct D-Bus session address. You can get it using the id -u
command.
2. Construct the D-Bus Session Address
With the UID, you can form the DBUS_SESSION_BUS_ADDRESS
. This variable points to the D-Bus socket for the target user's session. It typically follows the format unix:path=/run/user/<UID>/bus
.
3. Execute the gsettings
Command with sudo
and dbus-launch
Combine sudo -u <target_user>
, the DBUS_SESSION_BUS_ADDRESS
environment variable, and dbus-launch
to run the gsettings
command within the target user's context. Remember to replace <target_user>
with the actual username and specify the GSettings schema and key you wish to modify.
4. Verify the Change
Log in as the target user or use sudo -u <target_user> gsettings get <schema> <key>
to confirm that the GSettings value has been successfully updated.
# Example: Setting the GTK theme for 'testuser'
TARGET_USER="testuser"
TARGET_UID=$(id -u "$TARGET_USER")
DBUS_ADDRESS="unix:path=/run/user/${TARGET_UID}/bus"
sudo -u "$TARGET_USER" DBUS_SESSION_BUS_ADDRESS="$DBUS_ADDRESS" dbus-launch gsettings set org.gnome.desktop.interface gtk-theme 'Yaru-dark'
# Verify the change
sudo -u "$TARGET_USER" DBUS_SESSION_BUS_ADDRESS="$DBUS_ADDRESS" dbus-launch gsettings get org.gnome.desktop.interface gtk-theme
Complete script to set and verify GSettings for another user.