1. Role
The useradd or adduser command is used to create a user account and create a user's initial directory. The permission is a Super User.
2. Format
Useradd [-d home] [-s shell] [-c comment] [-m [-k template] [-f inactive] [-e expire] [-p passwd] [-r] name
3. Main parameters
-C: add the remarks text. The remarks text is saved in the remarks column of passwd.
-D: specifies the main directory when the user logs on to the system. Replace the default value with/home/<user name>.
-D: change the default value.
-E: specifies the expiration date of the account, in the format of MM/DD/YY, for example, 06/30/12. The default value is permanently valid.
-F: specifies the number of days after the password expires. If it is 0, the account is immediately disabled; if it is-1, the account is always available. The default value is-1.
-G: specifies the group to which the user belongs. The value can be the group name or GID. The user group must already exist. The default value of the period is 100, that is, users.
-G: specify the additional group to which the user belongs.
-M: automatically creates a user's logon directory.
-M: do not automatically create a user's logon directory.
-N: cancel the creation of a group named by the user name.
-R: create a system account.
-S: specifies the shell used after the user logs on. The default value is/bin/bash.
-U: specifies the user ID. This value must be unique in the system. 0 ~ 499 is reserved for the system user account by default, so the value must be greater than 499.
4. Description
Useradd can be used to create a user account. It is the same as the adduser command. After the account is created, use passwd to set the account password. The account created using the useradd command is actually saved in the/etc/passwd text file.
5. Application instance
Create a new user account testuser1 and set UID to 544. The main directory is/usr/testuser1, which belongs to the users group:
The code is as follows: |
Copy code |
# Useradd-u 544-d/usr/testuser1-g users-m testuser1 |
Add-m. If the main directory does not exist, it is automatically created.
6. Example
Log on to the system with the administrator account and create the user tmp_3452 password 3sdt5: Eawhg
Add user command:
The code is as follows: |
Copy code |
[Root @ ptr228 ~] # Adduser tmp_3452 Password change command: [Root @ ptr228 ~] # Passwd tmp_3452 |
The system prompts you to enter the password: 3sdt5: Eawhg. The system prompts you to enter the password and then enter it again. OK is added successfully.
7. Add users in batches
When useradd is used, if no parameter options are added later, for example: # The user created by sudo useradd test is the default "three none" User: one has no Home Directory, and the other has no password, 3. No system Shell.
The procedure is as follows:
(1) create the username.txt file of the user name (same as above)
Create a user password for file serc.txt in the format of username: password (note the file format)
The code is as follows: |
Copy code |
Stu1: tt1 Stu2: tt2 Stu3: tt3 Stu4: tt4 Stu5: tt5 Stu6: tt6 |
(3) batch add script files aa. sh
The code is as follows: |
Copy code |
# Add a user and generate a user directory for the user under/home. Cat <username.txt | xargs-n 1 useradd-m # Update password in batch mode Chpasswd <serc.txt # Convert the above password to the password file and group file Pwconv # end verification information Echo "OK creation complete" (4) run the script file to view the execution process Root @ liu:/home/liu/Desktop/Dos # sh aa. sh |
After the useradd command is created, no information is output and user interaction is not performed if no error occurs during execution. However, you must remember the setting items. Otherwise, the added users may have unexpected results.
II. Delete a user using userdel
Delete the created account tmp_3452
Command to delete a user:
The code is as follows: |
Copy code |
[Root @ ptr228 ~] # Userdel tmp_3452 |
Or delete it together with the user directory:
The code is as follows: |
Copy code |
[Root @ ptr228 ~] # Userdel-f tmp_3452 |
Note: If the user is still logging on, the system will prompt that the user cannot be deleted while logging on. In this case, you may need to force the user to exit.
3. Force logout of logged-in users
View the current logon user command:
The code is as follows: |
Copy code |
[Root @ ptr228 ~] # W |
Enter the following results:
The code is as follows: |
Copy code |
12:10:27 up, 1 user, load average: 0.00, 0.01, 0.08 User tty from login @ IDLE JCPU PCPU WHAT Root pts/0 ***. ***. *** 11: 33 0.00 s 0.08 s 0.00 s w Tmp_3254 ps1 *****. ***. *** 11: 33 0.00 s 0.08 s 0.00 s ls |
Here we know that the tty of the login user is ps1 and the command pkill is executed to force the exit:
Command prototype: pkill-kill-t [TTY]
The code is as follows: |
Copy code |
[Root @ ptr228 ~] # Pkill-kill-t ps1 |
Run the command again. You can see that the user has exited.
Run the delete user command in step 2 again. The deletion is successful.