Linux基础命令[24]-su

embedded/2024/10/18 12:20:47/

文章目录

    • 1. su 命令说明
    • 2. su 命令语法
    • 3. su 命令示例
      • 3.1 不加参数
      • 3.2 -(登录)
      • 3.3 -c(执行命令)
    • 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 -cand 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 exitFor 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 用户切换到普通用户时,不需要密码。


http://www.ppmy.cn/embedded/16604.html

相关文章

学习STM32第十九天

PWR电源控制 一、电源系统 STM32F4xx电源供电结构框图如下 电压管理器&#xff08;Voltage Regulator&#xff09;共有三种模式 run mode 此模式下&#xff0c;电压管理去稳定向核心、内存和外设提供1.2V电压&#xff0c;输出电压可由软件变换。stop mode 此模式下&#xf…

Java冒泡排序

Java冒泡排序算法是一种简单的排序算法&#xff0c;它通过多次比较和交换相邻元素的方式将最大&#xff08;或最小&#xff09;的元素逐步移动到数组的末尾。以下是Java冒泡排序算法的实现步骤&#xff1a; 遍历数组&#xff0c;从第一个元素开始&#xff0c;依次比较相邻的两…

Node.js使用

Node.js是一个基于Chrome V8引擎的JavaScript运行环境&#xff0c;它使得JavaScript能够脱离浏览器&#xff0c;直接在服务器端运行。Node.js的异步I/O模型使其在处理高并发请求时表现出色&#xff0c;适用于构建网络应用、实时应用等。以下是对Node.js使用的总结&#xff1a; …

设计模式:合成复用原则(Composite Reuse Principle,CRP)介绍

合成复用原则&#xff08;Composite Reuse Principle&#xff0c;CRP&#xff09;是面向对象设计原则之一&#xff0c;它强调通过组合已有的对象来实现新的功能&#xff0c;而不是通过继承已有的类来实现。合成复用原则的核心思想是尽量使用对象组合而不是类继承&#xff0c;从…

云原生Kubernetes: K8S 1.29版本 部署Nexus

目录 一、实验 1.环境 2.搭建NFS 3. K8S 1.29版本 部署Nexus 二、问题 1.volumeMode有哪几种模式 一、实验 1.环境 &#xff08;1&#xff09;主机 表1 主机 主机架构版本IP备注masterK8S master节点1.29.0192.168.204.8 node1K8S node节点1.29.0192.168.204.9node2K…

ThingsBoard处理设备上报的属性并转换为可读属性

一、前言 二、案例 1、AI生成JSON数据体 2、将json数据体直接通过遥测topic发送查看效果 3、可查看目前整个数据都在一起 ​编辑 4、配置附规则链路 5、对msg的消息值&#xff0c;进行数据的转换&#xff0c;并从新进行赋值。 6、规则链路关联关系 7、再次通过MQTT发送遥…

【自然语言处理】InstructGPT、GPT-4 概述

InstructGPT官方论文地址&#xff1a;https://arxiv.org/pdf/2203.02155.pdf GPT-4 Technical Report&#xff1a;https://arxiv.org/pdf/2303.08774.pdf GPT-4&#xff1a;GPT-4 目录 1 InstructGPT 2 GPT-4 1 InstructGPT 在了解ChatGPT之前&#xff0c;我们先看看Instr…

异地组网的供应商信息?

在当前信息时代&#xff0c;许多企业或个人需要跨地域进行网络连接&#xff0c;实现异地组网。异地组网是指通过网络技术将不同地区的计算机或网络设备连接起来&#xff0c;实现信息共享和远程访问的功能。本文将介绍一家供应商【天联】在异地组网领域的优势和相关服务。 【天联…