新问题:我正在尝试制作一个统一的脚本,根据我的喜好初始化新的 Ubuntu 安装,它必须在 sudo 下运行才能安装软件包,但使用 gconftool-2 影响 gconf 设置依赖于未处理的 dbus 会话通过简单地在脚本中更改 UID 的方法来正确地进行。有人知道如何做到这一点吗?
老问题:我正在编写一个 Perl 脚本,将在新安装的 Ubuntu 首次启动时执行。这是为了方便添加存储库、安装软件包和设置 gconf 设置。我的问题是权限。要安装软件包,我需要将脚本作为 sudo 执行,但随后 gconftool-2 调用将作用于 root 用户而不是我的个人用户。
THE NEW QUERY: I am trying to make a unified script that initializes a new Ubuntu install to my liking, it must be run under sudo to install packages, but using gconftool-2 to affect gconf setting relies on the dbus session which is not handled properly by the method of simply changing UID in the script alone. Does someone know how to manage to do this?
OLD QUERY: I am writing a Perl script to be executed on first boot of a new Ubuntu install. This is to ease adding repositories, installing packages, and setting gconf settings. My problem is permissions. To install packages I need the script to be executed as sudo, but then the gconftool-2 call act on the root user and not on my personal user.
发布评论
评论(3)
您可以在脚本中间通过使用
POSIX::setuid()
更改 uid 来更改 uid(请参阅 perldoc POSIX):You can change uids in the middle of the script by altering the uid with
POSIX::setuid()
(see perldoc POSIX):您可以再次使用 sudo 来删除 root 权限,例如:
You can use sudo again to drop your root privileges like:
经过大量阅读和反复试验后,当您以 root 身份运行脚本时,似乎缺少的是未设置 DBUS_SESSION_BUS_ADDRESS 环境变量。必须先设置此项并将 uid 更改为用户的 uid,然后才能设置 gconf 设置。这是我用来尝试的测试脚本。最后运行一个或另一个系统调用来切换窗口按钮顺序。以用户或 root (sudo) 身份尝试该脚本,看看它是否有效。
注意:此处获得了一些不错的信息。
After much reading and trial and error it seems that what is missing when you run a script as root is that the DBUS_SESSION_BUS_ADDRESS environment variable is not set. This must be set AND the uid changed to that of the user before the gconf settings can be set. This is my test script that I used to try it out. Run one or the other of the system calls at the end to switch the window button order. Try the script as the user or as root (sudo) to see that it works.
Note: Got some good info here.