更多: zhilu jiaocheng

sudo - 以另一个用户身份执行命令

从手册页:

sudo allows a permitted user to execute a command as the superuser or
another user, as specified by the security policy. The invoking user's
real (not effective) user ID is used to determine the user name with
which to query the security policy.

sudo 允许允许的用户以超级用户或者其他用户的身份执行命令,这由安全策略指定。
调用用户的真实(无效)用户 ID 用于确定用于查询安全策略的用户名。

例子:

# sudo -u opc bash -c 'echo $PATH'
/sbin:/bin:/usr/sbin:/usr/bin
# sudo -i -u opc bash -c 'echo $PATH'
/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/home/opc/.local/bin:/home/opc/bin

在“sudo”中,环境变量从原始会话传递到 /etc/sudoers 中定义的“sudo”会话:

Defaults env_reset
Defaults env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS"
Defaults env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"
Defaults env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
Defaults env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
Defaults env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"

定义的变量被保留。

使用“sudo -i”会导致一些变量被重置:

-i, --login
    Run the shell specified by the target user's password data‐
    base entry as a login shell. This means that login-specific
    resource files such as .profile, .bash_profile or .login will
    be read by the shell. If a command is specified, it is
    passed to the shell for execution via the shell's -c option.
    If no command is specified, an interactive shell is executed.
    sudo attempts to change to that user's home directory before
    running the shell. The command is run with an environment
    similar to the one a user would receive at log in. Note that
    most shells behave differently when a command is specified as
    compared to an interactive session; consult the shell's man‐
    ual for details. The Command environment section in the
    sudoers(5) bananaal documents how the -i option affects the
    environment in which a command is run when the sudoers policy
    is in use.
	

运行由目标用户的密码数据库条目指定的 shell 作为登录 shell。 
这意味着 shell 将读取特定于登录名的资源文件,例如 .profile、.bash_profile 或者 .login。

如果指定了命令,则会通过 shell 的 -c 选项将其传递给 shell 以执行。 
如果未指定命令,则执行交互式 shell。 

sudo 在运行 shell 之前尝试更改到该用户的主目录。 
该命令在类似于用户登录时收到的环境中运行。请注意,与交互式会话相比,指定命令时大多数 shell 的行为不同;
有关详细信息,请参阅 shell 手册。 
sudoers(5) 手册中的命令环境部分记录了 -i 选项如何影响在使用 sudoers 策略时运行命令的环境。

例如,在默认的 /etc/sudoers 中,PS1 变量将被保留:

# PS1="%: " sudo -u opc bash
%:
# PS1="%: " sudo -i -u opc bash
[opc@[HOSTNAME] ~]$

如果使用“-i”,它将导致运行登录资源文件,PS1 变量被重置为 /etc/bashrc.conf 中的设置。

Linux 中带有“su”和“sudo”的用户环境变量

su 和 sudo 允许以不同的用户执行命令或者 shell。
根据它们的调用方式,环境变量可能会发生变化,从而导致不同的命令结果。

“su”和“sudo”都允许代表其他用户执行命令。
su 的用法意味着知道“其他”用户密码,除非由 root 调用。
如果授予访问权限,则对用户可以做什么没有太多控制。

在 sudo 中,可以很好地控制用户可以做什么,可以运行什么命令。
无需知道“其他”用户的密码。
权限在配置文件中设置。

su - 使用替代用户和组 ID 运行命令

从手册页:

su allows to run commands with substitute user and group ID.
When called without arguments su defaults to running an interactive
shell as root.
For backward compatibility su defaults to not change the current direc‐
tory and to only set the environment variables HOME and SHELL (plus
USER and LOGNAME if the target user is not root). It is recommended to
always use the --login option (instead it's shortcut -) to avoid side
effects caused by mixing environments.

su 允许使用替代用户和组 ID 运行命令。 
当不带参数调用时,su 默认以 root 身份运行交互式 shell。 
为了向后兼容,su 默认不改变当前目录,只设置环境变量 HOME 和 SHELL(如果目标用户不是 root,则加上 USER 和 LOGNAME)。 
建议始终使用 --login 选项(而不是快捷方式 -)以避免混合环境引起的副作用。

例子:

# su opc -c 'echo $PATH'
/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
# su - opc -c 'echo $PATH'
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/opc/.local/bin:/home/opc/bin

在“su opc”中,用于运行命令的环境变量是原始变量,在这种情况下是用户 root 环境。
如果使用“”或者“-login”调用命令,则环境为“opc”用户,“TERM”除外。

正如手册页所述:

-, -l, --login
  Starts the shell as login shell with an environment similar to a
  real login:
  o clears all environment variables except for TERM
  o initializes the environment variables HOME, SHELL,
  USER, LOGNAME, PATH
  o changes to the target user's home directory
  o sets argv[0] of the shell to '-' in order to make the
  shell a login shell
  
以类似于真实登录的环境启动 shell 作为登录 shell:
   o 清除除 TERM 之外的所有环境变量
   o 初始化环境变量 HOME、SHELL、USER、LOGNAME、PATH
   o 更改目标用户的主目录
   o 将 shell 的 argv[0] 设置为“-”以使 shell 成为登录 shell
日期:2020-09-17 00:14:50 来源:oir作者:oir