文章目录

  • su 命令
    • 帮助信息
      • su --help
      • man su
    • 示例
      • 切换用户
      • 切换用户,并改变环境变量
    • 参考

su 命令

帮助信息

su --help

$ su --help
Usage: su [options] [LOGIN]Options:-c, --command COMMAND         pass COMMAND to the invoked shell-h, --help                    display this help message and exit-, -l, --login                make the shell a login shell-m, -p,--preserve-environment        do not reset environment variables, andkeep the same shell-s, --shell SHELL             use SHELL instead of the default in passwd

man su

$ man su
SU(1)                            User Commands                           SU(1)NAMEsu - change user ID or become superuserSYNOPSISsu [options] [username]DESCRIPTIONThe su command is used to become another user during a login session.Invoked without a username, su defaults to becoming the superuser. Theoptional argument - may be used to provide an environment similar towhat the user would expect had the user logged in directly.Additional arguments may be provided after the username, in which casethey are supplied to the user's login shell. In particular, an argumentof -c will cause the next argument to be treated as a command by mostcommand interpreters. The command will be executed by the shellspecified in /etc/passwd for the target user.You can use the -- argument to separate su options from the argumentssupplied to the shell.The user will be prompted for a password, if appropriate. Invalidpasswords will produce an error message. All attempts, both valid andinvalid, are logged to detect abuse of the system.The current environment is passed to the new shell. The value of $PATHis reset to /bin:/usr/bin for normal users, or/sbin:/bin:/usr/sbin:/usr/bin for the superuser. This may be changedwith the ENV_PATH and ENV_SUPATH definitions in /etc/login.defs.A subsystem login is indicated by the presence of a "*" as the firstcharacter of the login shell. The given home directory will be used asthe root of a new file system which the user is actually logged into.OPTIONSThe options which apply to the su command are:-c, --command COMMANDSpecify a command that will be invoked by the shell using its -c.The executed command will have no controlling terminal. This optioncannot be used to execute interactive programs which need acontrolling TTY.-, -l, --loginProvide an environment similar to what the user would expect hadthe user logged in directly.When - is used, it must be specified before any username. Forportability it is recommended to use it as last option, before anyusername. The other forms (-l and --login) do not have thisrestriction.-s, --shell SHELLThe shell that will be invoked.The invoked shell is chosen from (highest priority first):The shell specified with --shell.If --preserve-environment is used, the shell specified by the$SHELL environment variable.The shell indicated in the /etc/passwd entry for the targetuser./bin/sh if a shell could not be found by any above method.If the target user has a restricted shell (i.e. the shell field ofthis user's entry in /etc/passwd is not listed in /etc/shells),then the --shell option or the $SHELL environment variable won't betaken into account, unless su is called by root.-m, -p, --preserve-environmentPreserve the current environment, except for:$PATHreset according to the /etc/login.defs options ENV_PATH orENV_SUPATH (see below);$IFSreset to “<space><tab><newline>”, if it was set.If the target user has a restricted shell, this option has noeffect (unless su is called by root).Note that the default behavior for the environment is thefollowing:The $HOME, $SHELL, $USER, $LOGNAME, $PATH, and $IFS environmentvariables are reset.If --login is not used, the environment is copied, except forthe variables above.If --login is used, the $TERM, $COLORTERM, $DISPLAY, and$XAUTHORITY environment variables are copied if they were set.Other environments might be set by PAM modules.CAVEATSThis version of su has many compilation options, only some of which maybe in use at any particular site.CONFIGURATIONThe following configuration variables in /etc/login.defs change thebehavior of this tool:CONSOLE_GROUPS (string)List of groups to add to the user's supplementary groups set whenlogging in on the console (as determined by the CONSOLE setting).Default is none.Use with caution - it is possible for users to gain permanentaccess to these groups, even when not logged in on the console.DEFAULT_HOME (boolean)Indicate if login is allowed if we can't cd to the home directory.Default is no.If set to yes, the user will login in the root (/) directory if itis not possible to cd to her home directory.ENV_PATH (string)If set, it will be used to define the PATH environment variablewhen a regular user login. The value is a colon separated list ofpaths (for example /bin:/usr/bin) and can be preceded by PATH=. Thedefault value is PATH=/bin:/usr/bin.ENV_SUPATH (string)If set, it will be used to define the PATH environment variablewhen the superuser login. The value is a colon separated list ofpaths (for example /sbin:/bin:/usr/sbin:/usr/bin) and can bepreceded by PATH=. The default value isPATH=/sbin:/bin:/usr/sbin:/usr/bin.SULOG_FILE (string)If defined, all su activity is logged to this file.SU_NAME (string)If defined, the command name to display when running "su -". Forexample, if this is defined as "su" then a "ps" will display thecommand is "-su". If not defined, then "ps" would display the nameof the shell actually being run, e.g. something like "-sh".SYSLOG_SU_ENAB (boolean)Enable "syslog" logging of su activity - in addition to sulog filelogging.FILES/etc/passwdUser account information./etc/shadowSecure user account information./etc/login.defsShadow password suite configuration.EXIT VALUESOn success, su returns the exit value of the command it executed.If this command was terminated by a signal, su returns the number ofthis signal plus 128.If su has to kill the command (because it was asked to terminate, andthe command did not terminate in time), su returns 255.Some exit values from su are independent from the executed command:0success (--help only)1System or authentication failure126The requested command was not found127The requested command could not be executedSEE ALSOlogin(1), login.defs(5), sg(1), sh(1).shadow-utils 4.5                  08/21/2019                             SU(1)

示例

切换用户

mk@mk-Lenovo-Y430P:~$ whoami // 当前用户
mk
mk@mk-Lenovo-Y430P:~$ pwd // 当前工作目录
/home/mk
mk@mk-Lenovo-Y430P:~$ su root // 切换到 root 用户
Password:
root@mk-Lenovo-Y430P:/home/mk# whoami
root
root@mk-Lenovo-Y430P:/home/mk# pwd
/home/mk

当只切换用户而不改变环境变量时,执行某些命令将受限。例如,查看 Java 的版本信息:

root@mk-Lenovo-Y430P:/home/mk# java -versionCommand 'java' not found, but can be installed with:apt install default-jre
apt install openjdk-11-jre-headless
apt install openjdk-8-jre-headless

切换用户,并改变环境变量

mk@mk-Lenovo-Y430P:~$ whoami
mk
mk@mk-Lenovo-Y430P:~$ pwd
/home/mk
mk@mk-Lenovo-Y430P:~$ su --login root
Password:
root@mk-Lenovo-Y430P:~# whoami
root
root@mk-Lenovo-Y430P:~# pwd
/root

在这种情况下,能正常执行 java -version 命令:

root@mk-Lenovo-Y430P:~# java -version
java version "1.8.0_241"
Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)

参考

Linux su 命令

Linux su 命令相关推荐

  1. Linux whoami命令、Linux su命令、Linux w命令

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. Linux whoami命令用于显示自身用户名称. 显示自身的用户名称,本指令相当于执行" ...

  2. linux su命令在哪里,Linux su命令

    本人以前一直习惯直接使用root,很少使用su,前几天才发现su与su -命令是有着本质区别的! 大部分Linux发行版的默认账户是普通用户,而更改系统文件或者执行某些命令,需要root身份才能进行, ...

  3. linux su -含义,linux su命令详解步骤

    su命令是变更为其它使用者的身份,超级用户除外,需要键入该使用者的密码.那么它的具体语法是怎样的呢?下面由学习啦小编为大家整理了linux su命令的相关知识,希望对大家有帮助! linux su命令 ...

  4. linux su 的含义,linux su命令的真正含义,linuxsu命令

    linux su命令的真正含义,linuxsu命令 linux中,我以root登录,和以其他用户登录然后使用su命令切换至root用户,有什么不一样,如果一样的话,我执行su命令之后,其他的用户还在不 ...

  5. linux su命令_Linux Su命令示例教程

    linux su命令 The su short for substitute super user command using to change currently logged user.  Th ...

  6. linux中的su-命令的功能,linux su命令参数及用法详解(linux切换用户命令)

    linux su命令参数及用法详解(linux切换用户命令) 发布时间:2012-07-21 12:12:39   作者:佚名   我要评论 su的作用是变更为其它使用者的身份,超级用户除外,需要键入 ...

  7. 【Linux】一步一步学Linux——su命令(103)

    00. 目录 文章目录 00. 目录 01. 命令概述 02. 命令格式 03. 常用选项 04. 参考示例 05. 附录 01. 命令概述 su命令用于切换当前用户身份到其他用户身份,变更时须输入所 ...

  8. #linux# su命令细节错误

    2019独角兽企业重金招聘Python工程师标准>>> #前言# 在学习su命令时,几乎所有的国内书籍都说是切换用户功能.今天在linux系统下想开启httpd(即Apache服务器 ...

  9. linux的su参数,linux su命令参数及用法详解

    linux 命令 资料整理:www.linuxso.com  Linux安全网 建议大家切换用户的时候 使用  su -  root  这样,否则可能发现某些命令执行不了 关于su .su - 及 s ...

  10. linux su命令参数及用法详解--linux切换用户命令

    最近发现用"su root" 命令进入到root用户时,一些命令如shutdown.init.ifconfig等等不能被执行,上网一查发现用"su -root" ...

最新文章

  1. Linux入门之进程管理(4)之进程与文件
  2. C++11中的右值引用
  3. Dynamics CRM 依赖组件类型为应用程序功能区导致的无法删除实体问题的解决方法...
  4. 最新dotCMS SQL注入漏洞 攻击者可获得敏感数据 绿盟科技发布安全威胁通告
  5. Rails测试《十》不能错过的杂七杂八
  6. 2020项目商机_营销“心”思维,赢得“新”商机 ——2020年第二期军师项目顺利落幕...
  7. (转)spring boot整合redis
  8. 把VSCode当作记事本使用
  9. 企业架构 | TOGAF架构能力框架
  10. Unity3D 最实用的插件推荐
  11. 虚拟仿真实验室 服务器,中国美术学院|虚拟仿真实验教学共享平台
  12. 【Android Studio】XUI框架的使用记录:源代码Demo安装+从Demo中获取捷径快速开发自己的APP
  13. excel中单元格的引用方法
  14. 若依RuoYi-Vue 入门零接触超详细(一)
  15. 《敏捷教练-如何打造优秀的敏捷团队》读书笔记
  16. [×××.launch]is neither a launch file in package [××] nor is [××] a launch file name解决办法
  17. 如何恢复 Linux 上删除的文件
  18. _motz_ forum.php_开启模块化大门 moto z体验
  19. Unity 关于音频如何播放短时间存在的音效(例如:击中敌人时的音效)
  20. Vue中methods与computed区别

热门文章

  1. 【计算机网络微课堂】1.3 三种交换方式:电路交换、分组交换和报文交换
  2. 头的各个部位示意图_人体头部结构图:人体图片头部组织图文解读
  3. Word一行排列多个图片并插入题注
  4. wps ppt 如何批量换背景
  5. HTML网站导航栏的制作
  6. 用spss进行数据的标准化处理_用spss怎样对数据进行标准化
  7. 网站推广优化教程100条(完整版)
  8. 2022新版H5拼团抽奖拆盲盒模式源码+功能强大
  9. 企业微信通讯录可以导出吗?如何导出?
  10. rabbitmq 消息确认机制ACK