Skip to Content

Understanding the useradd Command in Linux

Whether you’re a system administrator or an ambitious Linux enthusiast, creating and managing user accounts is an essential skill in the Linux world.

In this post, we’ll delve into the powerful useradd command, offering examples to illustrate its versatility.

Introduction to useradd command

At the heart of user management in Linux is the useradd command. It allows you to create new user accounts, complete with home directories, customized shells, unique user IDs (UIDs), and more.

Mastering useradd not only adds to your command-line proficiency but also sets the stage for effective system administration.

Getting Started with useradd

Before we get into the specifics, let’s look at a basic example. To create a new user named newuser, you can use:

useradd newuser

This simple command creates a new user with default settings. Actually when you execute the useradd command, a series of operations take place behind the scenes to set up the user account:

  1. Creating User Entry: The command first creates an entry for the new user in the /etc/passwd file. This entry contains basic user information such as the username, User ID (UID), Group ID (GID), home directory, shell, and a comment field.
  2. Setting Password: The useradd command doesn’t set a password for the new user by default. If you want to add a password during the creation of the account, you will need to use the passwd command afterward. The encrypted user password is stored in the /etc/shadow file.
  3. Home Directory: If instructed (by using the -m option), the useradd command will create a home directory for the user. The contents of the /etc/skel directory will be copied to this new home directory, setting up a basic environment for the user. The home directory path will be set in the /etc/passwd file.
  4. User Group: By default, useradd creates a group with the same name as the user and makes this user the only member of that group. The group details are stored in the /etc/group file.
  5. User Shell: The useradd command assigns a default shell to the user, usually /bin/bash. This can be changed with the -s option.

These operations collectively set up a new user account in the system. It’s important to note that you need superuser (root) privileges to execute the useradd command and create a new user account.

Digging Deeper: Options and Parameters

The useradd command offers several options for customization:

  • Define Home Directory: With -d option, you can specify a custom home directory. For example, sudo useradd -d /home/custom newuser creates a user named newuser with a home directory at  /home/custom.
  • Specify User ID (UID): The -u option allows you to set a specific UID for the new user. For instance, sudo useradd -u 1001 newuser assigns UID 1001 to newuser.
  • Assign to a Group: To add a user to an existing group during creation, you can use the -G option. For example, sudo useradd -G developers newuser adds newuser to the developers group.
  • Specify Login Shell: With the -s option, you can designate a login shell for the user. For instance, sudo useradd -s /bin/tcsh newuser sets /bin/tcsh as the login shell for newuser.

Remember, not all options may be available on all systems or versions of the useradd command. You can always check which options are available on your system by running man useradd or useradd –help to view the command’s manual page or help message, respectively.

difference between -g and -G options in useradd command

In the useradd command, the -g and -G options are used to specify the user’s primary group and supplementary groups, respectively.

Here’s a more detailed explanation:

  • -g : This option is used to define the primary (or default) group that’s assigned to the user. When a user creates a file, the file’s group ownership is set to the user’s primary group.
    Here’s an example of how to use the -g option:

    sudo useradd -g developers username
    In this example, username is the name of the new user, and developers is the name of the primary group that’s assigned to the user. You can also specify the group’s GID (Group ID) instead of the group’s name.
  • -G : This option is used to define one or more supplementary groups that the user is a member of. In Linux, a user can be a member of multiple groups.

    Here’s an example of how to use the -G option:

    sudo useradd -G staff,admin username
    In this example, username is the name of the new user, and staff and admin are supplementary groups that the user is added to. Again, you can specify each group’s GID instead of the group’s name.

So in short, -g is for setting the primary group, and -G is for setting one or more supplementary groups.

Hands-On Learning

The best way to understand these commands is by trying them out yourself. Open your terminal and experiment with creating users, remembering to substitute newuser with the actual username you want to create.

You can use the following steps to create a new user in a Linux system and then log in via SSH:

  1. Create a New User:
    First, create a new user using the useradd command. Replace newuser with the username that you want to use:
    sudo useradd -m newuser
  2. Set a Password for the New User:
    Set a password for the new user using the passwd command:
    sudo passwd newuser
    You’ll be prompted to enter the password twice.
  3. Allow the User to Use sudo (Optional):
    If you want the new user to have sudo privileges, you’ll need to add the user to the sudo group. On most Linux distributions, you can do this with the usermod command:
    sudo usermod -aG sudo newuser
    The -aG option appends the user to the supplementary group(s). In this case, newuser is added to the sudo group.
  4. Log in Via SSH:
    You can now log in as the new user via SSH from another system. You’ll need the IP address of the Linux system where you created the new user (let’s assume it’s, and you’ll need to enter the password you set for newuser:
    ssh newuser@
    If the SSH server is running on a non-default port (not 22), you’ll need to specify the port with the -p option.
  5. Verification:
    After you enter the password for newuser, you should be logged in to the Linux system.
    You can use the whoami command to verify that you’re logged in as newuser.

Remember to replace newuser with the username you want to create, and replace with the actual IP address of your Linux system.

Check user info in Linux

After the user is created, we need to check the user info to make sure it meet our requirement.

In Linux, there are several commands to check user-related information, such as User ID (UID), Group ID (GID), home directory, and login shell. Here are a few methods:

  1. id command: The id command is the most straightforward way to display user and group information for a specific user:
    id username
    Replace username with the name of the user you’re interested in. This command will display the user’s UID, GID, and all the groups the user is a member of.
  2. finger command: The finger command provides information about a user, including the user’s home directory and login shell:
    finger username
    Note that finger might not be installed on your system by default. You can install it using your system’s package manager (sudo apt install finger on Ubuntu, for instance).
  3. getent command: The getent command displays entries from databases supported by the Name Service Switch libraries, which are configured in /etc/nsswitch.conf.

    You can use it to get user information:
    getent passwd username
    This command will display a line from the /etc/passwd file for username, including the user’s UID, GID, home directory, and login shell.

Remember to replace username with the actual name of the user you’re interested in.

Troubleshooting Common Errors

When using the useradd command, you might encounter some common errors, such as “user already exists,” “cannot create directory,” or “permission denied.”

Make sure the username doesn’t already exist in the system, the path for the new home directory is valid and writable, and you have sufficient permissions (typically by using sudo) to create new users.

The “user already exists” error occurs in Linux when you attempt to create a new user with the useradd command, and a user with the same name already exists in the system.

Here are a few ways to resolve this:

Choose a Different Username: The simplest solution is to choose a different username for the new user and use that with the useradd command.

Use the usermod Command: If you want to modify the existing user instead of creating a new one, use the usermod command. For example, if you want to change the existing user to a different name, you can use
sudo usermod -l new_username old_username

Delete the Existing User: If you no longer need the existing user and want to create a new user with the same name, you can delete the existing user first. Use the userdel command for this: sudo userdel username. Be careful with this command, as it will delete the user and their home directory, losing any files stored there.

adduser vs useradd

While we’re on the topic, it’s worth noting that some Linux distributions provide an adduser command, which is more interactive and user-friendly than useradd. The choice between the two often comes down to personal preference or the specific requirements of your system.

In conclusion, the useradd command in Linux is a powerful tool for creating and managing users. By understanding its various options, you can effectively handle user accounts on your system. So the next time you need to add a new user to your Linux system, useradd has got you covered!

Learning Linux is a journey filled with the discovery of such powerful tools. We hope that this deep dive into the useradd command has empowered you with the knowledge and confidence to manage user accounts on your Linux system effectively.

Remember, the best way to master these skills is through practice. So, don’t shy away from firing up that terminal and experimenting with useradd. Happy exploring!