24/7/365 Support

Creating Ubuntu user accounts in batch mode

In this recipe, you will see how to create multiple user accounts in batch mode without using any external tool.

Getting ready

You will need a user account with root or root privileges.

How to do it...

Follow these steps to create a user account in batch mode:

  1. Create a new text file users.txt with the following command:
    $ touch users.txt
  2. Change file permissions with the following command:
    $ chmod 600 users.txt
  3. Open users.txt with GNU nano and add user account details:
    $ nano users.txt
  4. Press Ctrl + O to save the changes.
  5. Press Ctrl + X to exit GNU nano.
  6. Enter $ sudo newusers users.txt to import all users listed in users.txt file.
  7. Check /etc/passwd to confirm that users are created:

How it works…

We created a database of user details listed in same format as the passwd file. The default format for each row is as follows:

username:passwd:uid:gid:full name:home_dir:shell

Where:

  • username: This is the login name of the user. If a user exists, information for user will be changed; otherwise, a new user will be created.
  • password: This is the password of the user.
  • uid: This is the uid of the user. If empty, a new uid will be assigned to this user.
  • gid: This is the gid for the default group of user. If empty, a new group will be created with the same name as the username.
  • full name: This information will be copied to the gecos field.
  • home_dir: This defines the home directory of the user. If empty, a new home directory will be created with ownership set to new or existing user.
  • shell: This is the default login shell for the user.

The new user command reads each row and updates the user information if the user already exists, or it creates a new user.

We made the users.txt file accessible to owner only. This is to protect this file, as it contains the user's login name and password in unencrypted format.

Help Category:

What Our Clients Say