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
    
    * * * 胖并快乐着的死肥宅 * * *
  • 相关阅读:
    sqlserver创建链接服务器连接sqlserver脚本
    两步快速获取小程序源码
    SQL判断是否存在该数据 有则更新,没有则插入
    利用c#+jquery+echarts生成统计报表(附源代码)
    每晚定时重启IIS和数据库服务可节省服务器资源
    SQL中的循环、for循环、游标
    sql的行转列(PIVOT)与列转行(UNPIVOT)
    SQLServer 简单数据拆分
    IIS安装与MVC程序部署
    (六)HTTP和HTTPS(转)
  • 原文地址:https://www.cnblogs.com/bpzblog/p/13290069.html
Copyright © 2011-2022 走看看