In Linux useradd is a very basic command, but it is not intuitive to use. So much so that an adduser command was added to Ubuntu to simplify the operation of adding users. This article mainly introduces the knowledge of adding users to the Linux command useradd.
1. Function
The useradd or adduser command is used to create a user account and create a user's starting directory. The user permission is 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 notes text, and the notes text is saved in the comment column of passwd.
-d: Specify the home directory when the user logs in, replace the system default value /home/<user name>
-D: Change the preset value.
-e: Specify the expiration date of the account, the date format is MM/DD/YY, for example, 06/30/12. The default indicates permanent validity.
-f: Specify how many days after the password expires. If it is 0, the account will be deactivated immediately; if it is -1, the account will be available all the time. The default value is -1.
-g: Specify the group to which the user belongs. The value can make 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 create the user's login directory.
-M: Do not automatically create the user's login directory.
-n: Uncreate a group named after the user name.
-r: Create a system account.
-s: Specify the shell used by the user after logging in. The default value is /bin/bash.
-u: Specify the user ID number. This value must be unique in the system. 0~499 is reserved for the system user account by default, so this value must be greater than 499.
4. Explanation
useradd can be used to create a user account, and it is the same as the adduser command. After the account is built, use passwd to set the password of the account. The account created using the useradd command is actually saved in the /etc/passwd text file.
5. Application examples
Create a new user account testuser1, and set the UID to 544, the home directory is /usr/testuser1, which belongs to the users group:
The code is as follows
#useradd -u 544 -d /usr/testuser1 -g users -m testuser1
Add -m automatically created if the home directory does not exist
6. Example
Use the administrator account to log in to the system and create user tmp_3452 Password 3sdt5:Eawhg
Add user command:
The code is as follows
[root@ptr228 ~]# adduser tmp_3452
Modify password command:
[root@ptr228 ~]# passwd tmp_3452
When the system prompts to enter the password, it is to enter the password: 3sdt5:Eawhg The system prompts to enter the confirm password and enter it again. OK added successfully.
Add users in batches
When using useradd, if no parameter options are added afterwards, for example: #sudo useradd test created useradd will be the default "three-no" user: no Home Directory, no password, and no system shell.
The steps are as follows:
(1) Create a user name list file (same as above)
(2) Create a file corresponding to the user password, the format is username:password (note the format of the file)
The code is as follows
stu1:tt1 stu2:tt2 stu3:tt3 stu4:tt4 stu5:tt5 stu6:tt6
(3) Batch added script files
The code is as follows
##Add user and generate a user directory for the user under /home/. cat < | xargs -n 1 useradd -m##Update password in batch modechpasswd < ##Convert the above password to password file and group filepwconv##End verification informationecho "OK New creation is completed"(4)Execute the script file,View the execution process root@liu:/home/liu/Desktop/Dos# sh
New creation is completed
The useradd command will not output any information and will not interact with the user if there is no error in execution. However, users must remember those settings, otherwise the added users may have some unexpected results.
8. Create a new oracle user, which initially belongs to the oinstall group and also allows him to belong to the dba group.
#useradd oracle -g oinstall -G dba
Create a new oracle user, which initially belongs to the oinstall group and also makes it belong to the dba group.
9. The shell cannot be used and its user directory is to /var/servlet/service
#useradd tomcat -d /var/servlet/service -s /sbin/nologin
The shell cannot be used and its user directory is /var/servlet/service
Second, userdel deletes the user
Delete the account you just created tmp_3452
Delete user command:
The code is as follows
[root@ptr228 ~]# userdel tmp_3452
Or delete it together with the user directory:
The code is as follows
[root@ptr228 ~]# userdel -f tmp_3452
Note: If the user is still logged in here, it will prompt that the user is logged in and cannot be deleted. At this time, it may be necessary to force the user to log out first.
3. Forced logout of the user already logged in
Check the commands for the currently logged in user:
The code is as follows
[root@ptr228 ~]# w
The following results will be entered:
The code is as follows
12:10:27 up 21:13, 1 user, load average: 0.00, 0.01, 0.08 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 ***.**.***.** 11:33 0.00s 0.08s 0.00s w tmp_3254 ps1 ***.**.***.** 11:33 0.00s 0.08s 0.00s ls
Here we know that the tty of the logged-in user is ps1 to execute the forced exit command pkill:
Command prototype:pkill -kill -t [TTY]
The code is as follows
[root@ptr228 ~]# pkill -kill -t ps1
After executing, execute the name w and you can see that the user has exited.
Repeat the second step to delete the user command, and the deletion is successful.
Summarize
The above is the Linux command useradd added to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!