How to Use useradd Command in Linux

The useradd command in Linux is one of those utilities that you either never heard of if you’re a casual user. Most distros have an in-built user administration tool that features their graphical user interface for ease of use. But if you’re a dedicated system administrator, this command is one of your everyday tools.

What is useradd Command in Linux?

useradd is a native binary included on UNIX-based systems designed to create, modify, and delete users. Through this command, an admin can set up the user name, password, and other important attributes such as authorization via user group settings.

Why Make Another User Account Using the Command Line?

Most users will not need multiple accounts. An admin account, a family-shared account, and a guest account are often enough to get a system running securely. But if you’re a server admin who is tasked to manage all accounts for new and existing users, you would need to be proficient in useradd.

How About adduser?

useradd and adduser fundamentally have the same functions. However, adduser is a Perl script rather than a native binary. It is a more user-friendly option for the front-end Debian operations while useradd is more prominent in the back-end.

Unlike useradd, however, adduser will create a default user account automatically. If you want to streamline the configuration of bulk accounts, or if you want to only set up temporary ones, the best option is to use useradd.

Default useradd Syntax

Unlike commands like sed, useradd has a pretty straightforward syntax. All you have to do is to execute the command followed by any options, and username as the last part of the instruction. You can add the passwd command after a semicolon to streamline the password configuration process.

$ useradd options name
$ useradd [option] ‘arguments’ name; other commands

Relevant Options for useradd Command in Linux

There are only a few options for the useradd command due to its simple functionalities. Hence, only a few deviations can be done on for the command’s format. However, automating the password and account configuration is a more challenging feat alone.

Nevertheless, here are some of the useradd options to choose from based on your current needs and preferences.

-D: Create a user account with default options

-d: Set up another home directory instead of the default

-e: Configure an account with an expiry date

-p: Create an account with a password

-u: Option used for creating an account with custom UID

-M: Set up a user without a home directory

-s: Configure the login shell of a new account

-g: Configure the group for a new account

Setting up an automation script is convenient for dealing with batch accounts. A popular approach for automatic account configuration is by using these commands: echo, passwd, and read (if the command would need to create accounts from a list). Additionally, the userdbct command will check if the user already exists within the database, which is helpful for validation checks.

Example Uses of useradd Command

Getting started with the useradd command is pretty straightforward. But first, you need to know how the number of accounts required and decide whether automation is better than manual execution. Here are the fundamental uses cases for the useradd command.

1. Creating a New User Account

You need root access to execute useradd, so don’t forget to run sudo su. Once you’re in elevated access, run the command and the -p option to set a password. Follow up with the password and then the username. When completed with no error, the command line won’t return anything.

As you can see, the account grian can already be seen in the list of new users. You can skip the password if you don’t want to configure it yourself. If you change your mind later on, the passwd command is a handy tool to change it.

using useradd command in Linux
user account creation with useradd

2. Accounts with Custom User ID and Home Directory

A unique user ID is helpful if you want to implement custom access for specific users only. This method lets you make changes without affecting other users of the system.

To create a custom user ID or UID, execute useradd followed by the -u option and the custom UID. After that, set up the user name, and you’re good to go. You can check a user’s UID and Group User ID or GUID with id username. The example below sets user grian’s UID to 1234.

$ useradd -u UID username
changing user ID with useradd

Another option is to configure a custom home directory for the user. This feature can be useful in some situations wherein the user is temporary or if a custom home directory is a more convenient option.

In the example below, we opened a new grian account, but let’s set up the home directory to /grumbot/projects. Using grep on user/etc/passwd is done to confirm that the changes in the home directory are applied. As can be seen below, its home directory was changed.

$ useradd -d pathname username
useradd change home directory

If you don’t want a user to have a home directory, the option to do this is the -M option. When checked through ls, there would be no file or directory available.

useradd command in linux

3. Restricted Accounts (with Limited Password or Effectivity Date)

You can also create restricted accounts with passwords that expire after a specific date. Options -e and -f are used to get the effect; -e indicates that an account expires after the indicated date while -f makes the password invalid after certain days.

It’s best to set your account and password expiry options within one line to avoid further re-configuration later on.

$ useradd -e <date_of_account_expiry> -f <days_before_pw_expiration>
useradd command in linux

4. Creating Accounts with Custom Login Shells

For those who need to create accounts with a custom login shell, you need to use the -s followed by the pathname of the preferred shell. This is useful in some instances such as simulating an initial login with bash -l, or when simulating a root login.

$useradd -s pathname username
useradd command in linux

5. Managing User Group

And lastly, you can set up the GUID to assign a group for a username. You can use the groupadd command to create a new group. You can use id username to check if an account is a part of a group or not.

$ useradd -g groupname username
useradd command in linux

And that’s it for the useradd command, a very reliable and must-have tool for system administrators. Check out our other guides for using mapfile, mknod, tmux, or other commands. There are also guides in installing PIP or from the how-to section.

If this guide helped you, please share it. ?

Leave a Reply
Related Posts