As a system administrator, you may need to add users to a machine frequently, either by adding many users to a machine or by adding the same user to many machines.
I am such a system engineer. Every time a new employee enters the company, he has to create one-to-one accounts on up to 50 machines. I don't know why, the old Postmaster always liked to operate one by one. When I entered the company, they handed it over to me. In my opinion, this is a very boring and tasteless thing, so I tried to use scripts to automatically complete these operations.
Basically, there are two ways to add users in batches. One is to use the useradd + passwd command in combination with the script to add users, and the other is to use newusers + chpasswd to add users.
The first method is suitable for adding different users on different machines, or adding a large number of users named by rules on the same machine. The second method can be used to add a large number of user names on the same machine, such as adding all the users on machine A to machine B.
Standard input must be involved in both methods, and the redirection standard input must be used. Note that the password must be copied into two lines in the standard input text for redirection passwd, to achieve the purpose of confirmation.
Example: passwd newuser <newpasswd.txt
So the content of newpasswd.txt should be
Password
Password
Appendix: newusers + chpasswd method (tested, steps pwunconv and pwconv can be omitted on my machine)
(1) edit a text user file. Each line is written in the format of the/etc/passwd password file. Note that the user name, uid, and home directory of each user cannot be the same, the password column can be left blank or enter the X number. The content of user.txt in a sample file is as follows:
User001: 600: 100: User:/home/user001:/bin/bash
User002: 601: 100: User:/home/user002:/bin/bash
User003: 602: 100: User:/home/user003:/bin/bash
User004: 603: 100: User:/home/user004:/bin/bash
User005: 604: 100: User:/home/user005:/bin/bash
User006: 605: 100: User:/home/user006:/bin/bash
(2) run the command/usr/sbin/newusers as root to import data from the user.txt file of the created user and create the user:
Code:
# Newusers <user.txt
You can then run the command vi pw or VI/etc/passwd to check whether the data of these users exists in the/etc/passwd file and whether the user's home directory has been created.
(3) run the/usr/sbin/pwunconv command to decode the shadow password generated by/etc/shadow and write it back to/etc/passwd, delete the shadow password column of/etc/shadow. This is to facilitate the next step in password conversion, that is, to cancel the shadow password function first.
Code:
# Pwunconv
(4) edit the password comparison file for each user. The content of the example file passwd.txt is as follows:
Code:
User001: Password
User002: Password
User003: Password
User004: Password
User005: Password
User006: Password
(5) run the command/usr/sbin/chpasswd as root to create the user password, chpasswd writes the password encoded by the/usr/bin/passwd command to the/etc/passwd password column.
Code:
# Chpasswd <passwd.txt
(6) Confirm that the password is encoded into the/etc/passwd password column, and then execute the command/usr/sbin/pwconv to encode the password as shadow password, write the result to/etc/shadow.
Code:
# Pwconv
In this way, a large number of users are created. Then, you can go to/home to check whether the permission settings of these users in the home directory are correct, and log on to verify that the user password is correct.