Setting umask for su root

Str*_*Bad 7 umask su

In my user account I set the umask to be 022 with my .bashrc, but I want root to use a umask of 077 no matter how I become root (e.g., logging in directly as root on a tty or using sudo or su). If I login to a tty as root, I get 077, so it is fine. For sudo in /etc/sudoers I have set

Defaults umask = 0022
Defaults umask_override
Run Code Online (Sandbox Code Playgroud)

and I get 077, so again fine. For su, in /root/.bashrc I can set

umask 022
Run Code Online (Sandbox Code Playgroud)

and I get a umask of 077, but I am not convinced this is the correct way to do it.

How do you set the umask for su root

Potentially relevant is that my /etc/login.defs has

UMASK           077
USERGROUPS_ENAB yes
Run Code Online (Sandbox Code Playgroud)

slm*_*slm 5

这个答案是特定于 bash 的,其他 shell 具有类似的功能,但既然你提到了你的答案,.bashrc我将假设你正在使用 Bash。

您遇到的是 shell 可以作为交互式 shell 或登录 shell 调用。

  • bash -l - 登录
  • bash -i - 交互的

如果您查看 Bash 手册页的“INVOCATION”部分,您会注意到以下 2 条评论:

  • 登录

    登录 shell 是参数零的第一个字符是 - 或以 --login 选项开头的 shell。

  • 交互的

    交互式 shell 是一个没有非选项参数并且没有 -c 选项的启动,其标准输入和错误都连接到终端(由 isatty(3) 确定),或者一个以 -i 选项启动。
    PS1 已设置,如果 bash 是交互式的,则 $- 包括 i,允许 shell 脚本或启动文件测试此状态。

交互式和登录 shell 按以下顺序读取配置文件。它首先执行此操作:

  1. /etc/配置文件

按照以下顺序依次是其中一个(首先找到的):

  1. ~/.bash_profile
  2. ~/.bash_login
  3. ~/.profile

交互式 shell(不是登录 shell)读取额外的配置文件~/.bashrc. 请注意,他们从不/etc/bashrc直接读取配置文件。~/.bashrc由于这一节,他们通读了文件:

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi
Run Code Online (Sandbox Code Playgroud)

那么umask呢

要让两种类型的 shell 都能读取,umask您需要将它包含在上述两种类型的 shell 调用都会读取的文件之一中,所以我将它放在/root/.bash_profile.