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?

  1. 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
  2. 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.
  3. 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-loginThis 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-badnameThe 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 GECOSThis option will restrict the adduser to ask for finger information.
--gid IDWhen 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.
--helpThis 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.
--systemThis option creates a system group or system user.
--add_extra_groupsThis option adds a new user to extra groups.
--versionThis 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:

DSHELLThis is the log-in shell, that is used for all users. Reverts to /bin/bash.
DHOMEThis is the directory where the new home directories have to be created. Reverts to /home.
GROUPHOMESThe 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.
LETTERHOMESThe 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.confIt is a revert layout for adduser and addgroup.

Working on addgroup command in Linux

  1. For adding a new user, we use:
adduser username
adduser command in linux with examples

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
adduser command in linux with examples

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
adduser command in linux with examples

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/
adduser command in linux with examples
  • For displaying the version of the adduser command, we use:
adduser –version
adduser command in linux with examples
  • For showing the help section of the adduser command, we use:
adduser -h 
adduser command in linux with examples