linux - sudo -u 有缺陷的文件权限

标签 linux bash shell sudo

我在 Lubuntu 16.04 上使用 Bash。 LTS,但我不确定这对这个问题是否重要。

我注意到,当我以标准用户身份创建文件时,该文件具有 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 来解决这个问题,但这并不能保证我的行为在任意数量的其他情况下都没有差异。

最佳答案

看起来 umask 取决于 shell 是否交互式:

$ 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。我不确定是否有其他东西覆盖了它,因为无论 shell 是否交互,shopt login_shell 都会打印相同的内容,并且 UID 也相同。

可以获得用户的默认shell thusly :

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

组合它们:

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

关于linux - sudo -u 有缺陷的文件权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44848158/

相关文章:

c - 为什么要在驱动程序代码中注册 struct cdev

linux - 使用集合和参数的 Bash 脚本 - 无法回显第 10 个参数

bash - 有没有办法让 bash 作业控制安静?

linux - 学生 Shell - cd 不工作

linux - if 结构什么时候不需要括号?

C 编程 : How do I read terminal input if piping from stdin?

bash - 在 bash 脚本中,如何从使用 eval 命令时执行的程序中获取 PID?

macos - 查看加载了哪些 Bash 配置文件脚本

bash - awk 布鲁斯..出了什么问题?

linux - 当别名被变量调用时如何调用别名