Home>Article>Operation and Maintenance> What is the command to switch users in Linux?

What is the command to switch users in Linux?

青灯夜游
青灯夜游 Original
2021-07-13 11:22:13 94878browse

The command to switch users in Linux is "su". This command can switch any identity, including switching from an ordinary user to a root user, from a root user to an ordinary user, and between ordinary users; syntax The format is "su [options] username".

What is the command to switch users in Linux?

#The operating environment of this tutorial: CentOS 6 system, Dell G3 computer.

The command to switch users in Linux is "su".

u is the simplest user switching command. With this command, you can switch any identity, including switching from an ordinary user to a root user, from a root user to an ordinary user, and between ordinary users. switch.

To switch between ordinary users and to switch from ordinary users to root users, you need to know the other party's password. Only by entering the password correctly can you switch; switching from root user to other users does not require knowing the other party's password. , the switch can be successful directly.

The basic format of the su command is as follows:

# su [选项] 用户名

Options:

  • -: Current user Not only does it switch to the identity of the specified user, but the working environment used is also switched to the environment of this user (including PATH variables, MAIL variables, etc.). Use the - option to omit the user name, and it will switch to the root user by default.

  • -l: The use of - is similar, that is, while switching the user identity, the working environment is completely switched, but the user who wants to switch needs to be added later. account.

  • -p: Indicates switching to the identity of the specified user, but does not change the current working environment (the switching user's configuration file is not used).

  • -m: Same as -p;

  • -cCommand: only switch users Execute the command once and automatically switch back after execution. This option is usually followed by the command to be executed.

【Example 1】

[lamp@localhost ~]$ su -root 密码: <-- 输入 root 用户的密码 #"-"代表连带环境变量一起切换,不能省略

【Example 2】

[lamp@localhost ~]$ whoami lamp #当前我是lamp [lamp@localhost ~]$ su - -c "useradd user1" root 密码: #不切换成root,但是执行useradd命令添加user1用户 [lamp@localhost ~]$ whoami lamp #我还是lamp [lamp@localhost ~]$ grep "user1' /etc/passwd userl:x:502:504::/home/user1:/bin/bash #user用户已经添加了

Except like Example 2, the user identity will automatically switch after executing a command Back, other methods of switching users will not automatically switch, and can only be manually switched using the exit command, for example:

[lamp@localhost ~]$ whoami lamp #当前我是lamp [lamp@localhost ~]$ su - lamp1 Password: <--输入lamp1用户的密码 #切换至 lamp1 用户的工作环境 [lamp@localhost ~]$ whoami lamp1 #什么也不做,立即退出切换环境 [lamp1@localhost ~]$ exit logout [lamp@localhost ~]$ whoami lamp

##su## andThe difference between su -Note that when using the su command, there is a completely different difference between having - and not having -. The - option means that while switching user identities, the currently used environment variables will also be included. Switch to the specified user. We know that environment variables are used to define the operating system environment. Therefore, if the system environment does not switch with the user's identity, many commands cannot be executed correctly.

Related recommendations: "

Linux Video Tutorial

"

The above is the detailed content of What is the command to switch users in Linux?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn