create multiple user accounts in linux

How to Create Multiple User Accounts in Linux

Often system administrators need to create multiple users. But the typical commands for user creation, adduser and useradd, allow you to create only one user at a time. So what to do in such cases? You can use newuser for this purpose. Newuser command allows you to create as well as update multiple new user accounts at a time. It allows you to input user information via file or standard input (stdin), or update a set of users. In this article, we will learn how to create multiple user accounts in Linux.


How to Create Multiple User Accounts in Linux

Here are the steps to create multiple user accounts in Linux. For this purpose, you need to create a file with user information, mentioned in the following format.

pw_name:pw_passwd:pw_uid:pw_gid:pw_gecos:pw_dir:pw_shell
  • pw_name: username
  • 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.

For example, let us create a simple file with user information.

$ sudo vim users.txt 

Add the following information for a couple of users.

test:213254lost:1002:1002:Test Admin:/home/test:/bin/bash
test2:@!#@%$Most:1003:1003:Test 2:/home/test2:/bin/bash

Save and close the file. Modify its permissions so that it is secure. This is because it contains plain passwords for your user accounts.

$ sudo chmod 0600 users.txt 

Run the newuser command to read the list of users from users.txt file and create new users according to the information.

$ sudo newuser users.txt

It is important to understand how the above command works. newuser will read the input file, create or update specified accounts, and then write these changes to their group or user databases. If there are any errors in the process, no changes are made to the database.

If the above command is successful, you can check /etc/passwd and /etc/groups for changes.

$ cat /etc/passwd | grep -E "test"

For more information about this command, you can check its man documentation.

$ man newuser

In this article, we have learnt how to create multiple user accounts in Linux. You can use it to easily add multiple users, especially in an IT environment. It is much better than writing a shell script to loop through the list of usernames and passwords, and calling useradd or passwd command. This is because calling useradd only allows you to add users but not set their passwords, while using passwd allows requires you to interactively enter passwords for each user.

Also read:

How to Find PHP.ini
How to Delete Rows from DataFrame Based on Condition
How to Iterate Over Rows in Pandas DataFrame
How to Fix SettingWithCopyWarning in Pandas
How to Get Row Count in Pandas

Leave a Reply

Your email address will not be published.