As Linux is a multi-user system, it requires users to run software, configurations and commands. Those users require essential permissions to gain access to the system resources.
In this tutorial, we will learn about adduser command in Linux with examples.
Linux adduser command
adduser command is used to create a new user in Linux. It is a front-end tool that simplifies the low-level useradd command. You can easily create a user or add a new user to the group using it. In the backend, it uses a Perl script to provide a nice interactive high-level tool. It uses the configuration information in /etc/adduser.conf to perform actions.
While creating a user, adduser command by default creates a home directory, adds a skeleton and set passwords and other user details - that made it a favorite tool for many Linux fans. Like deluser, It is another recommended command when it comes to creating a new user in Debian and its family distributions.
adduser command is by default available on most Linux distributions. In case not found you can easily install it using the package manager from the default repositories.
The basic syntax of adduser command:
adduser [options] username
adduser Examples
Let's look into some of the most common use cases of the adduser command.
1. Add a new normal user
By default, adduser command adds a normal user to the system. To add a new user simply type adduser followed by the name of the user.
Syntax:
adduser [username]
This add a user account with the home directory and set with a password.
For example to add a normal user with name bob, type:
sudo adduser bob
adduser command does the following by default when adding a new user:
- Creates a user using the username provided. Choose the first available UID from the range specified in the configuration file.
- Creates a group having the same name as the user. Choose available GID.
- Adds user to the group created.
- Creates a home directory for the user (/home/<username>).
- Starts passwd command to set up the user's password.
- Asks for additional user details.
To add an organizational user account ie system account use --system option. The difference between a normal user is it has no expiry date and uid is below 999.
2. Add a user with a different home directory
By default adduser command creates user's home directory under /home. You can add --home option to create a new user account with a different directory.
Syntax:
adduser [username] --home [directory-path]
For example to create a user named tom with home directory /mnt/data/tom, type:
sudo adduser tom --home /mnt/data/tom
3. Adding a new user to a group
Using adduser command you can easily add an existing user to an existing group.
Syntax:
sudo adduser [existing-username] [exiting-group-name]
For example to add a user named john to the group named developers, type:
sudo adduser tom developers
Make sure the user name and group already exist in the system.
4. Add a User with no password
The adduser with --disabled-login allows to you to add a user with no password. Until the password is set the user won't be able to login.
Syntax:
sudo adduser --disabled-login [username]
For example to add a user named bobs with no password, type:
This command doesn't prompt for a password. Later if you want the user to login, create a password using passwd command.
5. Add a user with disabled password
The adduser command with --disabled-password option allows you to add a user account with no password. But the user can still possible to login the system using SSH keys.
Syntax:
sudo adduser --disabled-password [username]
For example to add a user named john with no password, type:
This is useful when you need a user with only ssh key login for secure authentication.
You can use --gecos option with empty string to avoid asking for finger information such as Full Name, Room Number, Work Phone, Home Phone, and Other. Example:
sudo adduser --disabled-password --gecos "" bob
6. Use a different config file
Instead of using the default configuration file, we can instruct adduser command to use a custom config file. Use adduser command with --conf option for this.
Syntax:
sudo adduser [username] --conf [custom-config.conf]
Example:
sudo adduser thomas --conf custom-config.conf
7. Add a user with a different shell
Instead of the default shell, you can manually specify the users login shell using --shell option. The default shell is specified in the /etc/adduser.conf configuration file - normally /bin/bash or /bin/sh is used.
You can use cat /etc/shells to list all your available shells in your Linux system. From the list choose your desired shell.
For example, to add a user named tomas with login shell zsh, type:
sudo adduser tomas --shell /bin/zsh
You can verify the login shell of the user by listing passwd file as follows:
grep -i tomas /etc/passwd
adduser command Options
The following table describes some of the useful options for adduser command:
Options | Description |
---|---|
--conf file-path | Use a different configuration file |
--disabled-login | Disables login to a user’s account until the password is set. |
--disabled-password | Disables login using a password. Though users can still log in using SSh RSA keys. |
--gecos GECOS | If this option is given, adduser will not ask for finger information. |
--gid ID | When creating a group, this option forces the new group-id to be the given number. When creating a user, this option will put the user in that group. |
--home DIR | This uses the DIR directory as the user's home directory, rather than the default specified by the configuration file. If the directory does not exist, it is created and skeleton files are copied. |
--shell SHELL | This option uses SHELL as the user's login shell, rather than the default specified by the configuration file. |
--ingroup GROUP | It adds the new user to GROUP instead of a user’s group or the default group defined by USERS_GID in the configuration file. This affects the user's primary group. |
--no-create-home | It does not create the home directory, even if it doesn't exist. |
--quiet | It suppresses informational messages, only show warnings and errors. |
--debug | It is most useful if you want to nail down a problem with adduser. |
--system | It creates a system user or group. |
--uid ID | It forces the new userid to be the given a number. The adduser command will fail if the userid is already taken. |
--firstuid ID | It overrides the first uid in the range that the uid is chosen from (overrides FIRST_UID specified in the configuration file). |
--lastuid ID | It overrides the last uid in the range that the uid is chosen from ( LAST_UID ) |
--add_extra_groups | It adds a new user to extra groups defined in the configuration file. |
Conclusion
To conclude I can say adduser command is a more user-friendly version of useradd that is typically used on Debian-based systems.
For more information browse adduser manual page or type adduser --help on the terminal.
Navigate all-in-one place of Linux Commands for more learning.
Comments