1.How to Add a New User in Linux?

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

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

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

ubuntu@RushiInfotech:/$ sudo adduser user1

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

ubuntu@RushiInfotech:/$ cat /etc/passwd | grep user1

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

  • Username: User login name used to login into the system. It should be between 1 to 32 characters long.
  • Password: User password (or x character) stored in /etc/shadow file in an encrypted format.
  • 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 UIDs ranging from 1-99 are reserved for other predefined accounts. Further UIDs ranging from 100-999 are reserved for system accounts and groups.
  • Group ID (GID): The primary Group ID (GID) Group Identification Number stored in the /etc/group file.
  • User Info: This field is optional and allows you to define extra information about the user. For example, the user’s full name. This field is filled by the ‘finger’ command.
  • Home Directory: The absolute location of the user’s home directory.
  • Shell: The absolute location of a user’s shell i.e. /bin/bash.

2. Create a User with a Different Home Directory

By default ‘sudo useradd‘ command creates a user’s home directory under /home directory with a username. Thus, for example, we’ve seen above the default home directory for the user ‘account‘ is ‘/home/user1

However, this action can be changed by using the ‘-d‘ option along with the location of the new home directory (i.e. /data/projects). For example, the following command will create a user ‘user2‘ with a home directory ‘/data/projects‘.

ubuntu@RushiInfotech:/$ sudo useradd -d /data/projects user2

3. Create a User with a Specific User ID

In Linux, every user has their own UID (Unique Identification Number). By default, whenever we create a new user account in Linux, it assigns userid 500501502, and so on…

But, we can create users with custom userid with the ‘-u‘ option. For example, the following command will create a user ‘user3‘ with custom userid ‘1114‘.

ubuntu@RushiInfotech:/$ sudo adduser -u 1114 user3
ubuntu@RushiInfotech:/$ cat /etc/passwd | grep user3

4. How to create a Group?

ubuntu@RushiInfotech:/$ sudo addgroup goup1

To create a group we should specify the “sudo addgoup” with “groupname” eg. “sudo addgoup group1.”

5. Create a User with a Specific Group ID

Similarly, every user has their own GID (Group Identifier). We can create users with specific group IDs as well with the -g option.

Here in this example, we will add a user ‘user4‘ with a specific UID and GID simultaneously with the help of ‘-u‘ and ‘-g‘ options.

ubuntu@RushiInfotech:/$ sudo useradd -u 1115 -g group1 user4
ubuntu@RushiInfotech:/$ cat /etc/passwd | grep user4

6. Add a User without Home Directory

In some situations, where we don’t want to assign home directories for a user, due to security reasons. In such a situation, when a user logs into a system that has just restarted, its home directory will be root. When such a user uses the su command, its login directory will be the previous user’s home directory.

To create users without their home directories, ‘-M‘ is used. For example, the following command will create a user ‘user5‘ without a home directory.

ubuntu@RushiInfotech:/$ sudo useradd -M user5
ubuntu@RushiInfotech:/$ ls -l /home/user5

7. Create a User with an Account Expiry Date

By default, when we add user’s with the ‘useradd‘ command user account never get expires i.e their expiry date is set to 0 (which means never expired).

However, we can set the expiry date using the ‘-e‘ option, which sets the date in YYYY-MM-DD format. This is helpful for creating temporary accounts for a specific period of time.

Here in this example, we create a user ‘user6‘ with an account expiry date i.e. 15th September 2023 in YYYY-MM-DD format.

Next, verify the age of the account and password with the ‘chage‘ command for user ‘user6‘ after setting the account expiry date.

ubuntu@RushiInfotech:/$ sudo useradd -e 2023-09-15 user6
ubuntu@RushiInfotech:/$ sudo chage -l user6

8. Create a User with Password Expiry Date

The ‘-f‘ argument is used to define the number of days after a password expires. A value of 0 inactive the user account as soon as the password has expired. By default, the password expiry value set to -1 means never expires.

Here in this example, we will set an account password expiry date i.e. 45 days on a user ‘user7‘ using ‘-e‘ and ‘-f‘ options.

ubuntu@RushiInfotech:/$ sudo useradd -e 2023-09-15 -f 45 user7
ubuntu@RushiInfotech:/$ sudo chage -l user7

9. Add a User with Custom Comments

The ‘-c‘ option allows you to add custom comments, such as the user’s full namephone number, etc to /etc/passwd file. The comment can be added as a single line without any spaces.

For example, the following command will add a user ‘user‘ and would insert that user’s full name, user surname, into the comment field.

ubuntu@RushiInfotech:/$ sudo useradd -c "user surname" username
ubuntu@RushiInfotech:/$ tail -1 /etc/passwd

 10. Create a User Login Shell in Linux

Sometimes, we add users who have nothing to do with the login shell or sometimes we require to assign difference shell to our users. We can assign different login shells to each user with the ‘-s‘ option.

Here in this example, will add a user ‘user8‘ without a login shell i.e. ‘/sbin/nologin‘ shell.

ubuntu@RushiInfotech:/$ sudo useradd -s /sbin/nologin user8
ubuntu@RushiInfotech:/$ tail -1 /etc/passwd

11.  Add a User to Multiple Groups.

The ‘-G‘ option is used to add a user to additional groups. Each group name is separated by a comma, with no intervening spaces.

Here in this example, we are adding a user ‘user1‘ into multiple groups like developers, testing and marketing.

ubuntu@RushiInfotech:/$ sudo addgroup developers
ubuntu@RushiInfotech:/$ sudo addgroup testing
ubuntu@RushiInfotech:/$ sudo addgroup marketing
ubuntu@RushiInfotech:/$ sudo usermod -a -G developers,testing,marketing user1
ubuntu@RushiInfotech:/$ id user1
uid=1003(user1) gid=1004(user1) groups=1004(user1),1017(developers),1018(testing),1019(marketing)


This guide has comprehensively covered the essential aspects of “useradd” commands in the Linux operating system. You have acquired the knowledge needed to create, modify, and delete user accounts effectively. Understanding the nuances of user attributes, home directories, and login shells empowers you to tailor user accounts to suit your system’s requirements precisely. By following best practices and security considerations, you are well-prepared to manage user accounts confidently, ensuring the stability and security of your Linux system.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *