web123456

Linux switch user

1. su

If you need to add a user after logging into the system as user1 with an ordinary user, but it is useful and does not have useradd permissions, then there are two ways:

1. First use exit to exit, then log in as root user, and then useradd to increase the user.

2. Use the su command, su is switch user to switch.

(1) When the su command does not add parameters, it switches to the root user by default. As long as you enter the root password, you can switch to the root identity until you exit with exit.

Note that although I switch to root here, I don’t actually switch home directory automatically.

[plain]  view plain  copy
 print ?
  1. [mysql@ggg2 ~]$ su  
  2. password:
  3. [root@ggg2 mysql]# pwd  
  4. /home/mysql  
  5. [root@ggg2 mysql]# exit  
  6. exit  
(2) Adding a - parameter to the su command can not only switch to root, but also apply the root environment.

You can see that the prompt has changed from $#.

[plain]  view plain  copy
 print ?
  1. [mysql@ggg2 ~]$ su -  
  2. password:
  3. [root@ggg2 ~]# pwd  
  4. /root  
  5. [root@ggg2 ~]# exit  
  6. logout  
(3) The su command can also add a specific user name as a parameter.

[plain]  view plain  copy
 print ?
  1. [root@ggg2 ~]# su - mysql  
  2. [mysql@ggg2 ~]$ pwd  
  3. /home/mysql  
  4. [mysql@ggg2 ~]$ exit  
  5. logout  

2. sudo

Execute commands with other user identities and permissions, rather than switching users.

Although the above su command is convenient, it requires knowing the user's password in advance. If it is leaked, the system security will be severely challenged.

Syntax: UserMySQLTo modify the password using user1: sudo passwd user1 .

The principle is: when running the command, the system checks the /etc/sudoers configuration file to see if the user has permission to execute sudo. If there is permission, the system requires the user to enter his own password. If the password is entered correctly, the system will run the passwd xx command as root.


/etc/sudoers file can be modified with vi (not recommended), but considering that the configuration file is important, Linux provides the visudo command to modify the file, and it automatically detects the syntax when saving to prevent configuration errors from being unable to use the sudo command.

[sql]  view plain  copy
 print ?
  1. [root@wc1 Desktop]# visudo  
  2. .....Omitted....................Omitted.....
  3. ## Allow root to run any commands anywhere  
  4. root    ALL=(ALL)       ALL  
  5. mysql   ALL=(ALL)       ALL#Copy and paste the above root line and change root to mysql

The meaning is:

(1) User mysql (column 1) can log in from anywhere (column 2), execute anyone's (column 3), any command (column 4).

(2) You can also write this way: %mysql ALL=(ALL)     ALL means letting users belonging to the mysql user group log in from anywhere and execute any commands from anyone.

(3) If you don’t want to enter your password, you can configure it like this: %mysql ALL=(ALL)   NOPASSWD: ALL, but this is not very safe.

(4) It is best not to set the last column to ALL, because this is equivalent to having all the permissions of root, which can be set according to requirements, such as the permissions of mysql users to turn off or restart the server:

%mysql   ALL=(ALL)     NOPASSWD:/sbin/shutdown,  /usr/bin/reboot


First, create a new user user1:

[plain]  view plain  copy
 print ?
  1. [root@wc1 Desktop]# useradd user1  

Then, the mysql user uses sudo to modify the password of user user1. If the password of the mysql user is not correct for inputting three consecutive times, the sudo command will not be executed:

[plain]  view plain  copy
 print ?
  1. [mysql@wc1 ~]$ sudo passwd user1  
  2.   
  3. We trust you have received the usual lecture from the local System  
  4. Administrator. It usually boils down to these three things:  
  5.   
  6.     #1) Respect the privacy of others.  
  7.     #2) Think before you type.  
  8.     #3) With great power comes great responsibility.  
  9.   
  10. [sudo] password for mysql:   
  11. Sorry, try again.  
  12. [sudo] password for mysql:   
  13. Sorry, try again.  
  14. [sudo] password for mysql:   
  15. Sorry, try again.  
  16. sudo: 3 wrong password attempts
  17.   
  18. [mysql@wc1 ~]$ sudo passwd user1  
  19. [sudo] password for mysql:   
  20. Change the password of user user1.
  21. New password:
  22. Invalid password: It is based on dictionary words
  23. Invalid password: too simple
  24. Re-enter a new password:
  25. passwd: All authentication tokens have been successfully updated.