How to Add New User & Set password

Last updated by Chandan Singh

We all are aware of the most popular command called ‘useradd‘ or ‘adduser‘ in Linux. There are times when a Linux System Administrator asked to create user accounts on Linux with some specific properties, limitations, or comments.

In Linux, a ‘useradd‘ command is a low-level utility that is used for adding/creating user accounts in Linux and other Unix-like operating systems. The ‘adduser‘ is much similar to useradd command because it is just a symbolic link to it.

In some other Linux distributions, useradd command may come with lightly difference version

When we run ‘useradd‘ command in Linux terminal, it performs the following major things:

  1. It edits /etc/passwd, /etc/shadow, /etc/group and /etc/gshadow files for the newly created user account.
  2. Creates and populates a home directory for the new user.
  3. Sets permissions and ownerships to the home directory

The basic syntax of the command is:

useradd [options] username

 

1. How to Add a New User in Linux

To add/create a new user, all you’ve to follow the command ‘useradd‘ or ‘adduser‘ with ‘username’. The ‘username’ is a user login name, that is used by the user to login to the system.

Only one user can be added and that username must be unique (different from other usernames already exists on the system).

For example, to add a new user called ‘linuxblogger‘, use the following command.

# useradd linuxblogger

When we add a new user in Linux with ‘useradd‘ command it gets created in a locked state and to unlock that user account, we need to set a password for that account with ‘passwd‘ command.

# passwd linuxblogger

Changing password for user linuxblogger
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.

Once a new user created, it’s entry automatically added to the ‘/etc/passwd‘ file. The file is used to store user's information and the entry should be.

linuxblogger:x:504:504:linuxblogger:/home/linuxblogger:/bin/bash

The above entry contains a set of seven colon-separated fields, each field has its own meaning. Let’s see what are these fields:

  1. Username: User login name used to login to the system. It should be between 1 to 32 characters long.
  2. Password: User password (or x character) stored in /etc/shadow file in encrypted format.
  3. User ID (UID): Every user must have a User ID (UID) User Identification Number. By default, UID 0 is reserved for the root user and UID’s ranging from 1-99 are reserved for other predefined accounts. Further UID’s ranging from 100-999 are reserved for system accounts and groups.
  4. Group ID (GID): The primary Group ID (GID) Group Identification Number stored in /etc/group file.
  5. User Info: This field is optional and allows you to define extra information about the user. For example, user full name. This field is filled with a ‘finger’ command.
  6. Home Directory: The absolute location of user’s home directory.
  7. Shell: The absolute location of a user’s shell i.e. /bin/bash.