The two utilities for adding or creating user accounts in Unix/Linux systems are adduser and useradd. These commands are designed to add a single user account in the system at a time. What if you have multiple users accounts to be created? That’s when you need a program such as newusers.
Newusers is a useful command line utility used to update and create new user accounts at a single time. It is intended to be used in IT environments with large systems where a system administrator needed to update or create multiple user accounts in batch. It reads information from stdin (by default) or a file to update a set of existing user accounts or to create new users.
In this article, we will explain how to create multiple user accounts in batch mode using Newusers utility in Linux systems.
To create users in a batch, you can provide their information in a file in the following format, same as the standard password file:
pw_passwd: user’s password
pw_uid: user’s ID
pw_gid: user’s group ID
pw_gecos: defines comments sections.
pw_dir: defines the home directory of the user.
pw_shell: defines user’s default shell.
Attention: You should protect the input file since it contains unencrypted passwords, by setting the appropriate permissions on it. It should only be readable and writable by root.
For example, to add the user accounts vyga and rootadminz, you can create a files called users.txt as shown.
$ sudo vim users.txt
Next, add the user accounts details in the file in the following format.
vyga:213254lost:1002:1002:rootadminz Admin:/home/vyga:/bin/bash rootadminz:@!#@%$Most:1003:1003:rootadminz:/home/rootadminz:/bin/bash
Save the file and set the required permissions on it.
$ sudo chmod 0600 users.txt
Now run the newusers command with the input file to add the above user accounts at once.
$ sudo newusers users.txt
First, newusers program tries to create or update the specified accounts, and then write these changes to the user or group databases. In case of any errors except in the final writes to the databases, no changes are committed to the databases. This is simply how the newusers command works.
If the previous command is successful, check the /etc/passwd and /etc/groups files to confirm that the user accounts have been added as shown.
$ cat /etc/passwd | grep -E "vyga|rootadminz"
For more information, see the newuser man page.
$ man newuser