zoukankan      html  css  js  c++  java
  • Linux用户和组管理命令-切换用户su

    切换用户或以其他用户身份执行命令

    su: 即 switch user,命令可以切换用户身份,并且以指定用户的身份执行命令

    格式:

    su [options...] [-] [user [args...]]

    常见选项:

    -l --login su -l UserName 相当于 su - UserName
    -c, --command <command> pass a single command to the shell with -c
    

    切换用户的方式:

    • su UserName:非登录式切换,即不会读取目标用户的配置文件,不改变当前工作目录,即不完全切换
    • su - UserName:登录式切换,会读取目标用户的配置文件,切换至自已的家目录,即完全切换
      说明:root su至其他用户无须密码;非root用户切换时需要密码
      注意:su 切换新用户后,使用 exit 退回至旧的用户,而不要再用 su 切换至旧用户,否则会生成很多的bash子进程,环境可能会混乱。

    换个身份执行命令:

    su [-] UserName -c 'COMMAND'
    范例:

    [root@centos8 ~]#getent passwd mage
    mage:x:1001:1001::/home/mage:/bin/bash
    [root@centos8 ~]#usermod -s /bin/false mage
    [root@centos8 ~]#getent passwd mage
    mage:x:1001:1001::/home/mage:/bin/false
    [root@centos8 ~]#su - mage
    Last login: Fri Mar 27 09:18:57 CST 2020 on pts/0
    [root@centos8 ~]#whomai
    -bash: whomai: command not found
    [root@centos8 ~]#whoami
    root
    

    范例:

    [root@centos8 ~]#su -s /sbin/nologin wang
    This account is currently not available.
    [root@centos8 ~]#whoami
    root
    [root@centos8 ~]#su -s /bin/false wang
    [root@centos8 ~]#whoami
    root
    

    范例:

    [wang@centos8 ~]$su - root -c "getent shadow"
    

    范例:

    [root@centos8 ~]#su - wang -c 'touch wang.txt'
    [root@centos8 ~]#ll ~wang/
    total 0
    -rw-rw-r-- 1 wang wang 0 Mar 27 09:31 wang1.txt
    -rw-rw-r-- 1 wang wang 0 Mar 27 09:32 wang2.txt
    

    范例:

    [root@centos8 ~]#su bin
    This account is currently not available.
    [root@centos8 ~]#su -s /bin/bash bin
    bash-4.4$ whoami
    bin
    bash-4.4$
    [root@centos8 ~]#getent passwd tss
    tss:x:59:59:Account used by the trousers package to sandbox the tcsd
    daemon:/dev/null:/sbin/nologin
    [root@centos8 ~]#su - -s /bin/bash tss
    Last login: Fri Mar 27 09:46:43 CST 2020 on pts/0
    su: warning: cannot change directory to /dev/null: Not a directory
    -bash: /dev/null/.bash_profile: Not a directory
    [tss@centos8 root]$pwd
    /root
    [tss@centos8 root]$whoami
    tss
    
    * * * 胖并快乐着的死肥宅 * * *
  • 相关阅读:
    【递推】【HDU 2073】无限的路 (找规律)
    【省赛】山东省第七届ACM省赛(部分水题)
    【思维】牛客练习赛16 B-漂亮的树
    【搜索】牛客练习赛16 C-任意点 (类似求联通块)
    输入输出
    Python入门——运行python的两种方式&变量&常量
    编程语言分类
    What is an Operating System?
    计算机硬件&编程基础
    《SPA设计与架构》之认识SPA
  • 原文地址:https://www.cnblogs.com/bpzblog/p/13290069.html
Copyright © 2011-2022 走看看