如何以root用户身份运行Perl脚本,但仍会影响用户gconf设置 [英] How can I run a Perl script as root yet still affect user gconf settings

查看:126
本文介绍了如何以root用户身份运行Perl脚本,但仍会影响用户gconf设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新查询:我正在尝试根据自己的喜好来初始化一个新的Ubuntu安装的统一脚本,它必须在sudo下运行才能安装软件包,但是使用gconftool-2来影响gconf设置取决于dbus会话,仅通过单独更改脚本中的UID的方法无法正确处理。有人知道如何管理吗?

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?

旧查询:我正在编写一个Perl脚本,该脚本将在新的Ubuntu安装的首次启动时执行。这是为了轻松添加存储库,安装软件包和设置gconf设置。我的问题是权限。要安装软件包,我需要以sudo的身份执行脚本,但是gconftool-2调用将以root用户而不是我的个人用户的身份运行。

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.

推荐答案

经过大量阅读和反复试验后,似乎以root身份运行脚本时缺少的是未设置DBUS_SESSION_BUS_ADDRESS环境变量。必须先设置此项,并且uid更改为用户的uid,然后才能设置gconf设置。这是我用来尝试的测试脚本。最后运行一个或另一个系统调用以切换窗口按钮顺序。

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.

#!/usr/bin/perl

use strict;
use warnings;

use POSIX;

# get the user's name (as opposed to root)
my $user_name = getlogin();
# get the uid of the user by name
my $user_uid = getpwnam($user_name);
print $user_name . ": " . $user_uid . "\n";

my %dbus;
# get the DBUS machine ID
$dbus{'machine_id'} = qx{cat /var/lib/dbus/machine-id};
chomp( $dbus{'machine_id'} );
# read the user's DBUS session file to get variable DBUS_SESSION_BUS_ADDRESS
$dbus{'file'} = "/home/" . $user_name . "/.dbus/session-bus/" . $dbus{'machine_id'} . "-0";
print "checking DBUS file: " . $dbus{'file'} . "\n";
if (-e $dbus{'file'}) { 
  open(my $fh, '<', $dbus{'file'}) or die "Cannot open $dbus{file}";
  while(<$fh>) {
    if ( /^DBUS_SESSION_BUS_ADDRESS=(.*)$/ ) {
      $dbus{'address'} = $1;
      print "Found DBUS address: " . $dbus{'address'} . "\n";
    }
  }
} else {
  print "cannot find DBUS file";
}

# set the uid to the user's uid not root's
POSIX::setuid($user_uid);
# set the DBUS_SESSION_BUS_ADDRESS environment variable
$ENV{'DBUS_SESSION_BUS_ADDRESS'} = $dbus{'address'};

my $command1 = 'gconftool-2 --set "/apps/metacity/general/button_layout" --type string "menu:maximize,minimize,close"';
my $command2 = 'gconftool-2 --set "/apps/metacity/general/button_layout" --type string "menu:minimize,maximize,close"';
system($command1);
## or
#system($command2);

注意:获得了一些很好的信息此处

Note: Got some good info here.

这篇关于如何以root用户身份运行Perl脚本,但仍会影响用户gconf设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆