su为switch user,用于切换用户。
语法和选项
Usage:
su [options] [-] [USER [arg]...]
Change the effective user id and group id to that of USER.
A mere - implies -l. If USER not given, assume root.
Options:
-m, -p, --preserve-environment do not reset environment 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
-h, --help display this help and exit
-V, --version output version information and exit
用法示例
切换test用户
su - test
不切换用户直接执行命令
su - test -c 'ifconfig'
su 和 su - 的区别
su : 切换root用户,不改变当前的工作环境。
su -: 切换root用户,同时切换到新用户的工作环境中,包括工作目录、环境变量(例如HOME、SHELL、USER等)。
[test@node-2 ~]$ su
Password:
[root@node-2 okp]# echo $USER
test
[test@node-2 ~]$ su -
Password:
[root@node-2 okp]# echo $USER
root