useradd – Create a new system user

The useradd command is used to create a new system user in Linux. It is a low-level utility that is used to add new users to the system. Once a new user is added, the user will be able to log in to the system and perform tasks according to the permissions that have been assigned to them.

Overview

The useradd command can be used to create a new user account with default settings or with custom settings. The basic syntax for the command is as follows:

useradd [options] username

Here, username is the name of the user account that you want to create. The command can be run with various options to customize the user account that is being created.

To create a new user with default settings, simply run the useradd command followed by the username:

useradd john

This will create a new user account named john. By default, the user’s home directory will be created in the /home directory and the user’s login shell will be set to /bin/bash.

If you want to create a new user with custom settings, you can use various options with the useradd command. For example, to set a custom home directory for the user, you can use the -d option:

useradd -d /var/www/html/john john

This will create a new user account named john with a home directory of /var/www/html/john.

Other common options that can be used with the useradd command include:

  • -g: Set the primary group for the user
  • -s: Set the login shell for the user
  • -m: Create the user’s home directory
  • -c: Add a comment for the user

Options

The following options are available for the useradd command:

Option Description
-c Add a comment for the user
-d Set the home directory for the user
-e Set the expiration date for the user account
-f Set the number of days after password expiration before the account is disabled
-g Set the primary group for the user
-G Set the supplementary groups for the user
-h Set the home directory for the user (deprecated)
-k Set the skeleton directory for the user
-K Set useradd configuration defaults
-m Create the user’s home directory
-M Do not create the user’s home directory
-n Do not create a group with the same name as the user
-o Allow duplicate UIDs
-p Set the encrypted password for the user
-r Create a system account
-R Set the root directory
-s Set the login shell for the user
-u Set the UID for the user

Troubleshooting Tips

If you encounter issues while using the useradd command, here are some troubleshooting tips:

  • If you receive a “permission denied” error, make sure you are running the command as root or with sudo privileges.
  • If you receive an “invalid option” error, check that you have spelled the option correctly and that it is supported by your version of the useradd command.
  • If you receive a “user already exists” error, make sure that the username you are trying to create does not already exist on the system.

Notes

  • The useradd command only creates a new user account. It does not set a password for the user. To set a password, you can use the passwd command.
  • When creating a new user account, it is recommended to use a unique username that is easy to remember and difficult to guess.
  • By default, the useradd command creates a new user with a UID (user ID) between 500 and 60000.