adduser Command in Linux with Examples
What is the adduser command?
The adduser command creates a new user, and this command may be used to add apre-existing user to a group that already exists.
How to use the adduser command to create a new user?
- Adding a normal user: When we use the adduser command without the --group or --system options and with one non-option argument, a normal user is added. The first UID is chosen from the range declared for normal users in the configuration file by the adduser command. If you want, you can override UID using the –uid option. Similarly, the range declared in the configuration file can be overridden using the –firstuid and –lastuid options. To set VERBOSE i.e., the environment variable, the following rules should be kept in mind:
- --quiet option is defined.
- --debug option is defined
- or neither of --quiet nor --debug is defined
- Adding a system user: To add a system user, the adduser command has to be called with the --system option and one non-option argument. If already a user exists with the same name then the adduser command returns with a warning. If you want to suppress the warning, you may use the --quiet option.
- Adding an existing user to a group that already exists: If the adduser is run with two non-option arguments then the user is added to a group if it already exists.
Syntax:
adduser [--system][--firstuidID] [--home DIR]
[--shell SHELL] [--no-create-home]
[--uidID] [--lastuidID]
[--ingroup GROUP | --gid ID]
[--disabled-password]
[--gecosGECOS] [--add_extra_groups]
[--encrypt-home] [--quiet] [--debug]
[--force-badname]
[--help|-h] [--version] [--conf FILE] user
adduser [options] usergroup
Explanation of some options used in the above line code:
--conf FILE | This option use FILE in place of /etc/adduser/conf. |
--disabled-login | This option makes the user set a password for his account, or else he would not be able to use his account. |
--disabled password | This option is similar to –disabled-login, but the user can log in to his account without using a password. |
--force-badname | The user and group names are checked, by default. So, this option pressures adduser and addgroup to check roughly for the cogency of the name. |
--gecos GECOS | This option will restrict the adduser to ask for finger information. |
--gid ID | When this option is used, the given number is forced to be the new group ID. If a user is created/added, due to this option he is added to that group. |
--group | If we want to create the group with the same name as the system user, we combine this option with the --system. The group with the given name is created, if this option is not combined with the --system. |
--help | This option is used to show a help message and exit. |
--home DIR | If we use DIR as the user’s home directory, then if the directory does not exist, it is created and the skeleton files are copied. |
--quiet | This option only displays errors and warnings and hides informational messages. |
--system | This option creates a system group or system user. |
--add_extra_groups | This option adds a new user to extra groups. |
--version | This option shows copyright information and version. |
Configuration:
The defaults for the programs are held by the file /etc/adduser.conf. A single value pair exists in every line, in the form “option = value”. A hash sign (#) is compulsory in a comment line.
Some of the valid configuration options are:
DSHELL | This is the log-in shell, that is used for all users. Reverts to /bin/bash. |
DHOME | This is the directory where the new home directories have to be created. Reverts to /home. |
GROUPHOMES | The home directory is created as “/home/[groupname]/user” if set to yes. Reverts to no. |
SKEL | This is the directory from which the user configuration files have to be copied. Reverts to /etc/skel. |
LETTERHOMES | The home directories have an extra directory added that is the first letter of the login name if this is set to yes. |
Files:
/etc/adduser.conf | It is a revert layout for adduser and addgroup. |
Working on addgroup command in Linux
- For adding a new user, we use:
adduser username
To add a new user, we use the above command. You will just have to enter some required details and after that, a new user account will be created.
- For adding a user with a different shell, we use:
sudo adduser username --shell /bin/sh
If you want to change the default shell, this command can be used. It will change it to /bin/sh.
- For adding a user with a completely different configuration file, we use:
adduser username --conf custom_config.conf
After using this command, it will take configurations from the custom-coding file, and the new configurations will be created according to the custom-coding file configurations.
- For adding a user with a different home directory, we use:
adduser username --home /home/manav/
- For displaying the version of the adduser command, we use:
adduser –version
- For showing the help section of the adduser command, we use:
adduser -h