In this article I will show you how to create and add user accounts in Linux. The two commands adduser and useradd let you add one user at a time, but if you want to add multiple users we need to use the newusers command instead.
This command lets administrators create and update user accounts all in one go in a batch rather than one at a time. And useful in situations where there may be a large number of users on an IT system that you need to setup, for example in banking or a college.
Step 1: Create a user text file
The command can read the user configuration data either from the console or from a text file. The format of the input has to follow a set data format as follows for each user.
And where the fields correspond to: username; user’s password; user’s ID; user’s group ID; comments; home directory; and the default shell.
Suppose we have two user accounts to create, then we could create a file with following data.
userdata.txt dave:asdfg34:1008:1008:admin:/home/dave:/bin/bash fred:fghfgh456:1009:1009:writer:/home/fred:/bin/bash
And in vim save it in a file called newusers.txt as shown in the figure.
Step 2: Set the user file permissions
Since the file has password information in it we also need to set the permissions correctly so it cannot be viewed by unwanted eyes.
chmod 0600 newusers.txt
Step 3: Add and update the users
Now all we need to do is to pass the file as a parameter to the newusers command.
The command will read the file and will then try to update any existing accounts and also create any new accounts by modifying the user and group databases. Note than if an error occurs for instance due to a syntax error no changes are made to the databases. You will need to fix the errors for the command to complete successfully.
Step 4: Check the users were created
You can also check that the accounts were created or updated using grep on the /etc/passwd and /etc/group files as follows.
cat /etc/passwd | grep -E "dave|fred"
That is all there is you need to know about using the newusers command, take a look at the man page for more information.