如何通过远程shell更改Gsettings?


23

我需要通过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

我能做什么?


Ubuntu 18.04上,您可能不仅需要设置DBUS_SESSION_BUS_ADDRESS,还需要设置XDG_RUNTIME_DIR
mejem

Answers:


23

关键是设置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"

一切都会正常进行。


这立即为我工作!
sancho.s恢复莫妮卡

4

在配置期间尝试通过SSH对无业游民的映像进行gsettings更改时遇到了相同的问题。

这个解决方案/ubuntu//a/326773帮了我大忙,没有任何活动来进行主动连接并试图欺骗环境。YMMV ...


-1

我有一个设置gsetting的POST-Install脚本。因为我以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

您的用户ID已存储在$USER环境变量中,而您的UID 已存储在该$UID变量中。运行时sudo,它们位于$SUDO_USER$SUDO_UID变量中。就是说,您为什么还要首先运行它sudo?使用sudo -u $YOUR_USER就像根本不使用一样sudo。最后,您的who | awk . . .命令没有获得您的 ID,而是显示当前登录到系统的所有用户的所有 ID 。
terdon
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.