Let's review the previous section:
- User groups Add Groupadd
- User Group Modification Groupmod
- User group Delete Groupdel
- The job creates a group with ID 501 group1, then changes to Group2, and the ID becomes 502, and finally the group is deleted, with the following commands:
GROUPADD–G 501 Group1
Groupmod–g 502–n group2 group1
Groupdel group2
In this section, let's take a look at the user Switching related commands
Why do you want to switch users?
In the course of the operation, you need to use a specific user to do certain operations, in most cases because of permissions, such as to modify a file, only the root user has permission to modify, then switch to the root user to operate. Switching users typically have two commands:
1. su command
The SU command does not add parameters, default to root user, need to enter the root user password to verify,
Exit command to quit the root user.
The SU command can add a "-" plus a username, at which point the identity changes, as well as the user's "user environment", such as the user's home directory and other personalization settings for the user.
Normal user switch other users need to know the password of other users, the root user to use the SU command to switch other users, do not need to know the user password
2. sudo command
The use of sudo is to add a command to execute after sudo, indicating that the command is executed as root, such as
sudo useradd user1, which represents the operation of adding a user as root.
The action flow of this command in the system is
- Check if the current user has sudo permissions by checking the/etc/sudoers file
For example, we use User1 to execute sudo useradd command, will error
2. After the user enters their own password, verify the user password
3. After the password is correct, if the current user has permission to execute the command with sudo, the system will execute the command as the root user.
Based on the above process, we need to first give the user sudo permissions, this permission is stored in the/etc/sudoers file, can be edited with VI or VIM (both commands in the face), or with the sudoers file dedicated command Visudo, Here we use Visudo to edit, you can see the root user sudo permissions:
This line means that the root user (the first column), log in from anywhere (all of the second column), can execute any command (all of the third column) of any person (all of the fourth column), and according to this definition, we add the User1 permission
After saving exit, execute the sudo useradd command again in User1, at this time require the input User1 password, prove the permission to take effect
You can also assign permissions to groups of users by using the% group name, such as
%users all= (All) all
Indicates that users in all users groups can execute the sudo command.
How to do not want to enter the password every time, you can use NOPASSWD to configure, as follows:
Execute the sudo useradd command again
You are no longer required to enter a password.
Further, you can set what commands the user has permission to execute, such as we let User1 only execute shutdown command, can do the following configuration
After saving exits, execute the sudo useradd command again in User1
No permissions have been executed
More articles about the public number "kick genius"
Linux System Command Learning series-User Switching command Su,sudo