bash 使用 sudo 设置其他用户的 gsettings
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20292578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setting gsettings of other user with sudo
提问by Xu Wang
On Ubuntu 13.10, I have all of my gsettings in a file, mygset.sh. For example, mygset.sh contains many lines such as
在 Ubuntu 13.10 上,我的所有 gsettings 都在一个文件 mygset.sh 中。例如,mygset.sh 包含很多行,例如
gsettings set com.canonical.Unity.Launcher favorites "['application://nautilus.desktop', 'application://firefox.desktop', 'application://chromium-browser.desktop', 'unity://running-apps', 'unity://expo-icon', 'unity://devices']"
I have a master install script that I have to run with sudo (e.g. it does sudo apt-get install).
From that master install script I want to call mygset.sh
. However, no matter how I call it it is not changing the settings for my user. I think it is changing the settings of root. I've tried it like (from masterinstall.sh
which is being run as sudo ./masterinstall.sh
):
我有一个必须使用 sudo 运行的主安装脚本(例如,它执行 sudo apt-get install)。从那个主安装脚本我想调用mygset.sh
. 但是,无论我如何称呼它,它都不会更改我的用户的设置。我认为它正在更改root的设置。我已经尝试过(从中masterinstall.sh
运行为sudo ./masterinstall.sh
):
sudo -u "wang" ./mygset.sh
sudo -u "wang" bash -c ./mygset.sh
Neither of those works (they run without error and change the setting [I check within the script with gsetting get] but not for user "wang").
这些都不起作用(它们运行时没有错误并更改设置[我使用 gsetting get 在脚本中检查] 但不适用于用户“wang”)。
When I run mygset.sh
from the command line (without sudo: bash ./mygset.sh
). It works perfectly. Why is there this difference and what can I do to solve it within masterinstall.sh
?
当我从命令行运行时mygset.sh
(没有 sudo: bash ./mygset.sh
)。它完美地工作。为什么会有这种差异,我可以做些什么来解决它masterinstall.sh
?
采纳答案by ssnobody
I recommend you write a 'parent' script, which can then launch the masterinstall using sudo before running myget again as the local user. Examples follows:
我建议您编写一个“父”脚本,然后可以在以本地用户身份再次运行 myget 之前使用 sudo 启动 masterinstall。示例如下:
#!/bin/bash
sudo ./masterinstall.sh
./mygset.sh
回答by GregHNZ
By default sudo sets the uid and the gid to the user you've specified but it doesn't change the environment settings etc.
默认情况下 sudo 将 uid 和 gid 设置为您指定的用户,但它不会更改环境设置等。
Suggest you try -H
first, which sets the $HOME
variable to the target user:
建议您-H
先尝试,将$HOME
变量设置为目标用户:
sudo -u "wang" -H ./myget.sh
If that doesn't work, try -i
which is supposed to simulate the initial login.
如果这不起作用,请尝试-i
模拟初始登录。
A slightly different tack, which I've found sometimes works, is to use su
:
我发现有时有效的略有不同的方法是使用su
:
sudo su - wang
/full/path/to/myget.sh
exit
You'll need to use the full path to the script because the su command changes the current working directory.
您需要使用脚本的完整路径,因为 su 命令会更改当前工作目录。
回答by uzhoasit
Maybe you should run every gsettings set ...
as the user whose settings should change:
也许您应该以gsettings set ...
应该更改设置的用户身份运行 each :
sudo -u wang dbus-launch --exit-with-session gsettings set com.canonical.Unity.Launcher favorites "['application://nautilus.desktop', 'application://firefox.desktop', 'application://chromium-browser.desktop', 'unity://running-apps', 'unity://expo-icon', 'unity://devices']" >/dev/null 2>&1
There may be an error complaining about not creating .dbus/session-bus. Appending >/dev/null 2>&1
will suppress them.
可能会出现错误,抱怨没有创建.dbus/session-bus。附加>/dev/null 2>&1
将抑制它们。
Please be aware that changeing settings on unmountes encrypted homes won't work.
请注意,更改卸载加密家庭的设置将不起作用。
When using pam_mountto automatically map remote shares the script will stop and wait for the pam_mount password. You can workaround this by temporary deactivate pam_mount:
当使用pam_mount自动映射远程共享时,脚本将停止并等待 pam_mount 密码。您可以通过临时停用 pam_mount 来解决此问题:
# deactivate pam_mount
sed 's/@include common-session-noninteractive/#@include common-session-noninteractive/g' -i /etc/pam.d/sudo
sed 's/session\toptional\tpam_mount.so/#session\toptional\tpam_mount.so/g' -i /etc/pam.d/common-session
# do your settings
# reactivate pam_mount
sed 's/#@include common-session-noninteractive/@include common-session-noninteractive/g' -i /etc/pam.d/sudo
sed 's/#session\toptional\tpam_mount.so/session\toptional\tpam_mount.so/g' -i /etc/pam.d/common-session
回答by caracal
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).
我有一个 POST-Install 脚本来设置我的 gsetting。因为我以 sudo 的身份运行脚本,EUID 为 0,所以我必须找到 $RUID(真实用户 ID)。
here is my approach:
这是我的方法:
#!/usr/bin/env bash
# Get the Real Username
RUID=$(who | awk 'FNR == 1 {print }')
# 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
回答by microraptor
After trying a lot of stuff in different combinations this is the only command that worked for me:
在以不同的组合尝试了很多东西之后,这是唯一对我有用的命令:
sudo -H -u <user> DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/<uid>/bus gsettings set...
In a bash script you can use the following function to automatically detect the user and environment of a current session:
在 bash 脚本中,您可以使用以下函数自动检测当前会话的用户和环境:
function run-in-user-session() {
_display_id=":$(find /tmp/.X11-unix/* | sed 's#/tmp/.X11-unix/X##' | head -n 1)"
_username=$(who | grep "\(${_display_id}\)" | awk '{print }')
_user_id=$(id -u "$_username")
_environment=("DISPLAY=$_display_id" "DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$_user_id/bus")
sudo -Hu "$_username" env "${_environment[@]}" "$@"
}
Use it like this:
像这样使用它:
run-in-user-session gsettings set...