如何通过远程 shell 更改 Gsettings?

如何通过远程 shell 更改 Gsettings?

我需要通过 Puppet、虚拟终端或 ssh 自动化桌面配置。

gsettings不幸的是,通过 ssh 或虚拟终端调用会出现以下情况:

gsettings set org.compiz.core:/org/compiz/profiles/unity/plugins/core/ hsize "4"

(process:29520): dconf-WARNING **: failed to commit changes to dconf: Cannot autolaunch D-Bus without X11 $DISPLAY

当我设置时,$DISPLAYexport DISPLAY=:0.0出现另一个错误:

(process:29862): dconf-WARNING **: failed to commit changes to dconf: Could not connect: Connection refused

我能做些什么?

答案1

关键是设置DBUS_SESSION_BUS_ADDRESS环境变量。

此主题我找到了以下脚本,它有助于获取该变量的正确值。它需要在桌面上运行的进程的名称,我们想要更改该桌面的 dbus 设置。(可以同时运行多个图形会话)。我们称之为discover_session_bus_address.sh

#!/bin/bash

# Remember to run this script using the command "source ./filename.sh"

# Search these processes for the session variable 
# (they are run as the current user and have the DBUS session variable set)
compatiblePrograms=( nautilus kdeinit kded4 pulseaudio trackerd )

# Attempt to get a program pid
for index in ${compatiblePrograms[@]}; do
    PID=$(pidof -s ${index})
    if [[ "${PID}" != "" ]]; then
        break
    fi
done
if [[ "${PID}" == "" ]]; then
    echo "Could not detect active login session"
    return 1
fi

QUERY_ENVIRON="$(tr '\0' '\n' < /proc/${PID}/environ | grep "DBUS_SESSION_BUS_ADDRESS" | cut -d "=" -f 2-)"
if [[ "${QUERY_ENVIRON}" != "" ]]; then
    export DBUS_SESSION_BUS_ADDRESS="${QUERY_ENVIRON}"
    echo "Connected to session:"
    echo "DBUS_SESSION_BUS_ADDRESS=${DBUS_SESSION_BUS_ADDRESS}"
else
    echo "Could not find dbus session ID in user environment."
    return 1
fi

return 0

使用此脚本,我们可以执行以下操作,假设该unity进程正在我们想要应用设置的桌面上运行:

. ./discover_session_bus_address.sh unity
gsettings set org.compiz.core:/org/compiz/profiles/unity/plugins/core/ hsize "4"

一切应该都能正常运行。

答案2

我有一个 POST-Install 脚本,用于设置我的 gsetting。由于我以 sudo 身份运行该脚本,因此 EUID 为 0,因此我必须找到 $RUID(真实用户 ID)。

以下是我的方法:

#!/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

相关内容