2017-06-30 40 views
0

我在Lubuntu 16.04上使用Bash。 LTS,但我不确定这对这个问题是否重要。sudo -u有缺陷的文件权限

我注意到,当我以标准用户创建文件时,该文件具有664个权限。但是,当我是root用户并通过-u参数为同一用户执行相同的命令时,它具有644个权限,因此组的写入权限丢失。

我想这是一大败笔,因为sudo联机手册明确规定:

 -u user, --user=user 
      Run the command as a user other than the default target user (usually root). The user may be either a user name or a 
      numeric user ID (UID) prefixed with the ‘#’ character (e.g. #0 for UID 0). When running commands as a UID, many 
      shells require that the ‘#’ be escaped with a backslash (‘\’). Some security policies may restrict UIDs to those 
      listed in the password database. The sudoers policy allows UIDs that are not in the password database as long as the 
      targetpw option is not set. Other security policies may not support this. 

现在我知道-u参数的行为从具有可预期的行为不同,我的问题是:

如何确保在根shell中启动的命令执行刚好是,因为它将从其他用户的shell执行?

备注:我知道我可以通过修补umask来解决这个问题,但这并不能保证我的行为在任意数量的其他情况下没有差异。

+0

”的执行完全按照它从另一个我们执行的那样执行呃的外壳?“ - 执行该用户的shell。例如“'sudo -u用户sh -c'一些命令''”。请注意,每个用户都指定他们自己的shell。 'getent passwd user'获取详细信息 –

+0

有趣的是,这不起作用 - 文件许可仍然是644 – Wanderer

+1

Stack Overflow是一个编程和开发问题的网站。这个问题似乎与题目无关,因为它不涉及编程或开发。请参阅帮助中心的[我可以询问哪些主题](http://stackoverflow.com/help/on-topic)。也许[超级用户](http://superuser.com/)或[Unix&Linux堆栈交换](http://unix.stackexchange.com/)会是一个更好的地方。 – jww

回答

0

一个非常干净的解决方案,显示了预期的行为是这样的:

sudo su <username> -c '<any commands>'

2

它看起来像的umask取决于外壳是否互动

$ umask 
0002 
$ sudo -u $USER bash -c umask 
0022 
$ sudo -u $USER bash -ic umask 
0002 

这似乎是从/etc/bashrc,它适用umask 002只有

  • 它不是一个登录shell ,
  • UID大于或等于200,并且
  • 用户名等于组名

/etc/profile,如果满足最后两条标准,则应用umask 002。我不确定是否有其他内容重写此内容,因为shopt login_shell将打印相同的内容,而不管外壳是否为交互式,并且UID也相同。

你可以得到用户的默认外壳thusly

$ getent passwd $USER | cut --delimiter=: --fields=7 
/bin/bash 

结合他们:

$ sudo -u $USER $(getent passwd $USER | cut --delimiter=: --fields=7) -ic umask 
0002 
+0

对我来说,这不起作用:使用你的组合命令,无论从标准用户shell还是根shell启动,我都会得到'umask' 0022,所以它与我想实现的目的相反。另外,如果在根shell中启动,则会出现错误:'bash:/root/.bashrc:Permission denied' – Wanderer

+0

那么,如果您在根shell中运行此命令,'$ USER'的计算结果为“root”,那么您为了得到预期的结果,你必须用你感兴趣的用户替换它*两次。 – l0b0

+0

是的,我确实以这种方式替换了它们...... – Wanderer