adduser command in Linux with Examples



Name

adduser, addgroup - add a user or group to the system

Synopsis

adduser [options] [--home DIR] [--shell SHELL] [--no-create-home] [--uid ID] [--firstuid
ID] [--lastuid ID] [--ingroup GROUP | --gid ID] [--disabled-password]  [--disabled-login]
[--gecos GECOS] [--add_extra_groups] [--encrypt-home] user

adduser  --system [options] [--home  DIR] [--shell SHELL] [--no-create-home] [--uid ID]
[--group | --ingroup GROUP | --gid ID] [--disabled-password] [--disabled-login] [--gecos
GECOS] user

addgroup [options] [--gid ID] group

addgroup --system [options] [--gid ID] group

adduser [options] user group

Common Options

[--quiet] [--debug] [--force-badname] [--help|-h] [--version] [--conf FILE]

Description

adduser and addgroup programs are used to add users and groups to the system according to command line options and the default configuration parameters set in the configuration file. These default configuration parameters are specified in '/etc/adduser.conf' file. adduser commands are friendlier front ends to the low level tools like useradd, groupadd and usermod programs. Only root or users with sudo privileges can use the adduser command to create new user accounts.

By default adduser and addgroup programs choose Debian policy which is conformant to UID and GID values being assigned to users or groups. adduser program creates a home directory with skeletal configuration, running a custom scripts, and other features. adduser and addgroup command can be run in one of the five modes:

Add a normal user

When the adduser command is called with non-potion arguments and without the --system or --group options, adduser will add a normal user. adduser would choose the first available UID from the range specified for normal users in the configuration file. The UID can be overridden with the --uid option.

By default when a new user is added, it is assigned a primary group with the same name. Users' primary groups can also be overridden from the command line with the --gid or --ingroup options to set the group by id or name, respectively.

adduser will create a home directory as specified in the configuration file /etc/adduser.conf file. The home directory can be overridden from the command line with the --home option. The login shell of the new user can be specified with the --shell option.

Add a system user

When the command is called with the non-optin argument and the --system option adduser will add a system user. If a user with the same name already exists in the system, adduser will exit with a warning.

adduser will choose the first available UID from the range specified for system users in the configuration file (FIRST_SYSTEM_UID and LAST_SYSTEM_UID). If you want to have a specific UID for the new user, you can specify it using the --uid option.

By default, system users are placed in the nogroup group. To place the new system user in an already existing group, use the --gid or --ingroup options. To place the new system user in a new group with the same ID, use the --group option.

A home directory is created by the same rules as for normal users. The new system user will have the shell /bin/false (unless overridden with the --shell option), and have logins disabled. Skeletal configuration files are not copied.

Add a user group

If adduser command is called with the --group option and without the --system option, or addgroup command is called, a new user group will be added to the system. A GID will be chosen from the range specified for system GIDS in the configuration file (FIRST_GID, LAST_GID). To override that mechanism you can give the GID using the --gid option. The group is created with no users.

Add a system group

If addgroup command is called with the --system option, a system group will be added. A GID will be chosen from the range specified for system GIDS in the configuration file (FIRST_SYSTEM_GID, LAST_SYSTEM_GID). To override that mechanism you can give the GID using the --gid option. The group is created with no users.

Add an existing user to an existing group

If adduser command is called with two non-option arguments, adduser command will add an existing user to an existing group.

Options

--conf FILE
   Use FILE instead of default configuration file '/etc/adduser.conf' for default parameters.

--disabled-login
   Do not run passwd to set the password. The user won't be able to login to this user account until the password is set. This is often done while creating system users.

--disabled-password
   Like --disabled-login, but logins are still possible (for example using SSH RSA keys) but not using password authentication. This generally done to enhance system security or make systems more secure.

--force-badname
   By default, user and group names are checked against the configurable regular expression NAME_REGEX (or NAME_REGEX_SYSTEM if --system is specified) specified in the configuration file. This option forces adduser and addgroup to apply only a weak check for validity of the name.

--gecos GECOS
   Set the gecos field for the new entry generated. adduser will not ask for finger information if this option is given.
    
--gid ID
   When creating a group, this option forces the new groupid to ID number. When creating a user, this option will put the user in that group.

--group
   When combined with --system, a group with the same name and ID as the system user is created. If not combined with --system, a group with the given name is created. This is the default action if the program is invoked as addgroup.

--help Display brief instructions.

--home DIR
   Use DIR 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
   Use SHELL as the user's login shell, rather than the default specified by the configuration file.

--ingroup GROUP
   Add the new user to GROUP instead of a usergroup or the default group defined by USERS_GID in the configuration file. This affects the users primary group. To add additional groups, see the add_extra_groups option

--no-create-home
   Do not create the home directory, even if it doesn't exist.

--quiet
   Suppress informational messages, only show warnings and errors.

--debug
   Be verbose, most useful if you want to nail down a problem with adduser.

--system
   Create a system user or group.

--uid ID
   Force the new userid to be the given number. adduser will fail if the userid is already taken.

--firstuid ID
   Override the first uid in the range that the uid is chosen from (overrides FIRST_UID specified in the configuration file).

--lastuid ID
   Override the last uid in the range that the uid is chosen from ( LAST_UID )

--add_extra_groups
   Add new user to extra groups defined in the configuration file.

--version
   Display version and copyright information.

Examples

1. Add new user 'aman', it would ask for user details. Only super user can add new users to the system. Hence use sudo command to add new users.

$ sudo adduser aman
[sudo] password for expert: 
Adding user `aman' ...
Adding new group `aman' (1001) ...
Adding new user `aman' (1001) with group `aman' ...
Creating home directory `/home/aman' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for aman
Enter the new value, or press ENTER for the default
   Full Name []: 
   Room Number []: 
   Work Phone []: 
   Home Phone []: 
   Other []: 
Is the information correct? [Y/n] Y

2. To add new user with a specified user id use -u or --uid option.

$ sudo adduser -u 1500 venugopal 
[sudo] password for expert: 
Adding user `venugopal' ...
Adding new group `venugopal' (1500) ...
Adding new user `venugopal' (1500) with group `venugopal' ...
Creating home directory `/home/venugopal' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for venugopal
Enter the new value, or press ENTER for the default
   Full Name []: 
   Room Number []: 
   Work Phone []: 
   Home Phone []: 
   Other []: 
Is the information correct? [Y/n] 

3. You can chek if the user id assigned to venugopal is 1500 by id command.

$ id venugopal
uid=1500(venugopal) gid=1500(venugopal) groups=1500(venugopal)
$

When creating a new user, the default behavior of the adduser command is to create a group with the same name as the username, and same GID as UID. The --gid option allows you to create a user with a specific initial login group. You can specify either the group name or the GID number.

4. Create a user with a specific group id. The primary purpose of groups is to define a set of privileges such as reading, writing, or executing permission for a given resource that can be shared among the users within the group. The group name or GID must already exist.

$ sudo adduser -u 1501 srinivas --gid 1000
Adding user `srinivas' ...
Adding new user `srinivas' (1501) with group `expert' ...
Creating home directory `/home/srinivas' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for srinivas
Enter the new value, or press ENTER for the default
   Full Name []: 
   Room Number []: 
   Work Phone []: 
   Home Phone []: 
   Other []: 
Is the information correct? [Y/n] Y
$ id srinivas
uid=1501(srinivas) gid=1000(expert) groups=1000(expert)
$

5. To add new groups to the system use addgroup command.

$ sudo addgroup developers
Adding group `developers' (GID 1001) ...
Done.

6. To add a new group with a specified groupid use --gid option.

$ sudo addgroup operations --gid 1501
Adding group `operations' (GID 1501) ...
Done.   

7. To enter verbose mode while adding new users or groups, use --debug option.

$ sudo addgroup developers --debug
Selecting GID from range 1000 to 29999 ...
Adding group `developers' (GID 1001) ...
/usr/sbin/groupadd -g 1001 developers
Done.
Advertisements