useradd command in Linux with examples

In Linux, you can have multiple users working on the system simultaneously. That’s what qualifies Linux as a multi-user system. You can utilize the useradd commands in Linux to manage the system’s connected users. System administrators use this command frequently to create new users.

Having multiple users and managing their permissions properly will allow them to have private files on the system. You can also utilize user groups to further divide permissions among a set of users on the system.

In this tutorial, we will discuss the common applications of the useradd command and some of its options to help you best manage your system’s connected users and user groups.

In this tutorial you will learn:

  • How to use the useradd command on Linux
useradd command in Linux with examples
useradd command in Linux with examples
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software useradd
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Frequently Used Options



useradd command in Linux Basic Examples

  1. The useradd command requires you to possess root permissions to create user accounts. Otherwise, you’ll receive a “permission denied” error. So, be sure to login to the root account or just prepend sudo to your command.
    $ sudo useradd user01
    
    Using the useradd command and the sudo command to create a new user
    Using the useradd command and the sudo command to create a new user

    Adding a new user account to a Linux system with the useradd command is performed with specific settings. These settings can be viewed or altered in the /etc/default/useradd file.

  2. Upon creating a new user, you will have to set a user password in order to log into this new account. We can achieve this by running the passwd command followed by name of the new user account. The useradd command will make changes to the /etc/passwd, /etc/shadow, /etc/group and /etc/gshadow files once the new user is created.
    $ sudo passwd user01
    

    Using the passwd command followed by the username to set a password
    Using the passwd command followed by the username to set a password
  3. Depending on the Linux distribution, creating a new user account with the useradd command will not automatically create a new home directory for that user. You’ll have to create one manually after the fact. We can create a new home directory by passing the -m command line option to the useradd commmand followed by the username. Alternatively, you can use the --create-home command line option to create a user home directory.
    $ sudo useradd -m user02
    

    Using the useradd command with the -m option to create a new user with a new home directory included
    Using the useradd command with the -m option to create a new user with a new home directory included
  4. Add the -s option to your command to specify the default shell for the user. This is the shell they will be presented with every time they login.


    $ sudo useradd -m user02 -s /bin/bash
    
NOTE
You can always use the man command to read more about the useradd command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.

Advanced Usage

All of the options for useradd allow us to modify certain settings of a user account upon its creation. Check some of the examples below to see more options.

useradd command in Linux Advanced Examples

  1. Once the home directory is created for the new user, the useradd command will copy various files from the /etc/skel directory. These files will be part of the initialization files for the new user’s home directory. These files can be viewed using the following syntax.
    $ ls -la /home/user02
    

    Using the ls command to view the initialization files for user02's new home directory
    Using the ls command to view the initialization files for user02’s new home directory
  2. By default, when creating a new user with the useradd command, the user will be given the next user identifier available from the /etc/login.defs file. But you can also use the useradd command to create a user ID of your choosing by running it with the -u command line option.
    $ useradd -u 4321 user03
    
    Using the useradd command with the -u option to create a custom user ID for user03
    Using the useradd command with the -u option to create a custom user ID for user03



  3. In Linux, groups are mainly used to effectively determine read, write, and execute permissions to multiple users at once. Every group in Linux carries a specific group ID. User IDs are very similar to group IDs, as one is used to identify a single user and another an entire group of users. So, there’s not much problem when trying to use the useradd command to create a specific group ID, just as we did with user IDs in the previous example. We can achieve this by invoking the -g command line option.
    $ sudo useradd -g users username
    

    The process for assigning new group IDs and verifying them is the exact same as the one for user IDs.

  4. You can also utilize the useradd command with the -c command line option to create a new user account with custom a comment. This comment can be anything, but it’s usually just the full name of the user or their contact information. Once a comment is created for a new user, it’ll be stored in the /etc/passwd file. You can view this comment with the grep command. We’ll show you an example of this in the screenshot, along with creating the new user below.
    $ sudo useradd -c "Example Comment" user08
    
    Using the useradd command with the -c option to create a custom comment for user08 and viewing it with the grep command
    Using the useradd command with the -c option to create a custom comment for user08 and viewing it with the grep command

    It’s important to note that in Linux, as can be observed in the screenshot above, you cannot type multiple words as one variable (like a custom comment) in a command line shell without wrapping it in " quotation marks.

  5. You can also use the useradd command with the -e command line option to create user accounts that will expire at a specified point in time. We can use this command to create temporary user accounts. The format for the specified date must be in YYYY-MM-DD.
    $ sudo useradd -e 2022-02-14 user06
    


    Using the useradd command with the -e option to create a user with an expiration date and verifying it with the chage command
    Using the useradd command with the -e option to create a user with an expiration date and verifying it with the chage command

    As you can see in the screenshot above, this date can always be viewed at any time with the chage command.

Closing Thoughts

In this tutorial, we learned all about the useradd command on Linux. The useradd command is essential to master for users and administrators that frequently manage users and user groups in the Linux terminal.