Linux基础命令[24]-su

文章目录

    • [1. su 命令说明](#1. su 命令说明)
    • [2. su 命令语法](#2. su 命令语法)
    • [3. su 命令示例](#3. su 命令示例)
      • [3.1 不加参数](#3.1 不加参数)
      • [3.2 -(登录)](#3.2 -(登录))
      • [3.3 -c(执行命令)](#3.3 -c(执行命令))
    • [4. 总结](#4. 总结)

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 命令语法

shell 复制代码
su [options] [-] [USER [arg]...]

3. su 命令示例

3.1 不加参数

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

shell 复制代码
[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 用户。

shell 复制代码
[demo2@localhost home]$ su tuser15
Password: 
[tuser15@localhost home]$ su
Password: 
[root@localhost home]# 

3.2 -(登录)

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

shell 复制代码
[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 以用户身份执行后面的命令。

shell 复制代码
[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 用户切换到普通用户时,不需要密码。

相关推荐
没有bug.的程序员几秒前
服务网格(Istio)与传统微服务深度对垒:流量治理内核、代码侵入性博弈与运维收益实战指南
java·运维·微服务·istio·流量治理内核·代码侵入性
TongSearch18 分钟前
Tongsearch分片的分配、迁移与生命周期管理
java·服务器·数据库·elasticsearch·tongsearch
是Dream呀32 分钟前
自动化打造信息影响力:用 Web Unlocker 和 n8n 打造你的自动化资讯系统
运维·前端·爬虫·自动化
JinchuanMaster33 分钟前
Ubuntu20.04安装50系显卡驱动[不黑屏版本]
linux·人工智能·深度学习·ubuntu·机器学习·机器人·gpu算力
蜜汁小强33 分钟前
为 Github 创建本地 .ssh 关联 (RSA 以支持老系统)
运维·ssh·github
中草药z34 分钟前
【Linux】拆解 Linux 容器化核心:Namespace 隔离 + cgroups 资源控制,附 LXC 容器生命周期实战
运维·docker·容器·虚拟化·namespace·lxc·cgroups
草莓熊Lotso35 分钟前
Linux 程序地址空间深度解析:虚拟地址背后的真相
java·linux·运维·服务器·开发语言·c++·人工智能
郝学胜-神的一滴37 分钟前
使用Linux命名管道(FIFO)实现无血缘关系进程间通信
linux·服务器·开发语言·c++·程序人生
Jinkxs37 分钟前
【Linux】零基础入门:一篇吃透操作系统核心概念
linux·运维·服务器·网络·操作系统
懒神降世40 分钟前
基于iVentoy的PXE服务器的部署实战指南
运维·服务器·开发语言·云原生·vmware·openeuler·iventoy