Linux基础命令[24]-su

本文详细介绍了Linuxsu命令的用法,包括基本参数、不加参数、登录模式和-c选项用于执行命令的情况,以及root用户与普通用户之间的权限差异。
摘要由CSDN通过智能技术生成

1. su 命令说明

su:以用户身份执行命令,基本信息如下:

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

For more details see su(1).

基本参数如下:

选项作用
-m, -p, --preserve-environment不重置环境变量
-g, --group 指定主组
-G, --supp-group 指定附加组
-, -l, --login登录 shell
-c, --command ,–session-command <命令>使用 -c 向 shell 传递一条命令
-f, --fast向shell 传递 -f 选项(csh 或 tcsh)
-s, --shell 若 /etc/shells 允许,则运行 shell
-h, --help显示此帮助并退出
-V, --version输出版本信息并退出

2. su 命令语法

su [options] [-] [USER [arg]...]

3. su 命令示例

3.1 不加参数

当前若为 root 用户,使用 su 切换用户不需要密码,且不会改变目录和环境变量。

[root@localhost home]# ls
aaa  demo1  demo2  root2  tuser15
[root@localhost home]# su demo2
[demo2@localhost home]$ pwd
/home
[demo2@localhost home]$ ls
aaa  demo1  demo2  root2  tuser15
[demo2@localhost home]$ 

在这里插入图片描述

若当前用户为普通用户,使用 su 切换用户时,需要输入密码,如果 su 不加用户名,则切换至 root 用户。

[demo2@localhost home]$ su tuser15
Password: 
[tuser15@localhost home]$ su
Password: 
[root@localhost home]# 

在这里插入图片描述

3.2 -(登录)

su - 相当于使用用户登录了系统,所以切换用户的同时,会直接定位到该用户的家目录,且使用该用户的环境变量。

[root@localhost home]# su - tuser15
Last login: Tue Apr 23 10:42:35 PDT 2024 on pts/0
[tuser15@localhost ~]$ pwd
/home/tuser15
[tuser15@localhost ~]$ 

在这里插入图片描述

3.3 -c(执行命令)

-c 以用户身份执行后面的命令。

[root@localhost ~]# su - tuser15
Last login: Tue Apr 23 10:51:01 PDT 2024 on pts/0
[tuser15@localhost ~]$ su - root -c "ls"
Password: 
anaconda-ks.cfg  original-ks.cfg  thome  thome3
[tuser15@localhost ~]$ ls
[tuser15@localhost ~]$ su - root
Password: 
Last login: Tue Apr 23 10:52:38 PDT 2024 on pts/0
[root@localhost ~]# ls
anaconda-ks.cfg  original-ks.cfg  thome  thome3
[root@localhost ~]# 

在这里插入图片描述

4. 总结

su 以别的用户身份去执行接下来的命令,或直接切换到某个用户。root 用户切换到普通用户时,不需要密码。

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值