
su(switch user)命令在Linux系统中用于切换当前用户的身份,通常用来从普通用户切换到超级用户(root),但也支持切换到其他非特权用户。
在命令行终端中,我们使用
-h选项查su命令的基本帮助信息。
root@jeven01:~# su -h
Usage:
su [options] [-] [<user> [<argument>...]]
Change the effective user ID and group ID to that of <user>.
A mere - implies -l. If <user> is not given, root is assumed.
Options:
-m, -p, --preserve-environment do not reset environment variables
-w, --whitelist-environment <list> don't reset specified variables
-g, --group <group> specify the primary group
-G, --supp-group <group> specify a supplemental group
-, -l, --login make the shell a login shell
-c, --command <command> pass a single command to the shell with -c
--session-command <command> pass a single command to the shell with -c
and do not create a new session
-f, --fast pass -f to the shell (for csh or tcsh)
-s, --shell <shell> run <shell> if /etc/shells allows it
-P, --pty create a new pseudo-terminal
-h, --help display this help
-V, --version display version
For more details see su(1).语法:
su [选项] [-] [<用户> [<参数>...]]功能:
更改有效用户ID和组ID为<用户>的ID。仅提供一个 - 参数时等同于指定了 -l。如果未指定<用户>, 则默认为root。
选项:
选项 | 描述 |
|---|---|
| 不重置环境变量 |
| 不重置指定的环境变量 |
| 指定主要组 |
| 指定辅助组 |
| 创建一个登录shell |
| 将单个命令传递给shell执行 |
| 将单个命令传递给shell执行,但不创建新会话 |
| 将 |
| 如果 |
| 创建一个新的伪终端 |
| 显示此帮助信息 |
| 显示版本信息 |
默认情况下,不指定用户名时,su会尝试切换到root用户,系统将提示你输入root用户的密码。
admin@jeven01:~$ su
Password:
root@jeven01:/home/admin#指定用户名以切换到特定的用户账户,例如切换到名为admin的用户:
root@jeven01:/home/admin# su - admin
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
admin@jeven01:~$如果你不使用-、-l或--login选项,那么除了UID和GID之外,几乎所有的环境都会保持不变。这可能不是最佳的安全实践,因为某些敏感信息可能会留在环境中。
root@jeven01:~# su admin
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
admin@jeven01:/root$使用-c选项可以让你作为另一个用户执行单个命令。完成后,你将返回到原来的用户身份。
root@jeven01:~# su - -c "pwd" admin
/home/admin原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。