15+ adduser command examples in Linux [Cheat Sheet]

Introduction to adduser command

adduser is a command-line utility to create a new user in the Linux system. It is a friendlier frontend to the low-level tool 'useradd'. When adding a new user, it also creates the user directory under the /home directory.

 

How adduser command works

The default configuration file for adduser is /etc/adduser.conf. adduser command will work according to the default value specified in adduser.conf file.

Advertisement

adduser command adds the details of the new user to the following files.

  • /etc/passwd - It stores user account information.
  • /etc/shadow -  It contains the password information of users. The passwords are stored in an encrypted format.
  • /etc/group - It stores group information.

 

Different examples to use adduser command

You can use adduser command to add a normal user, system user, or user group in the Linux system. Only users with root privileges can add a user to the system. So, you will need to log in as a root user or use sudo to execute adduser command.

The general syntax of adduser command is:

$ sudo adduser [option] user

Now, let's see some practical examples of adduser command to add a user or group in the Linux system.

 

1. adduser command to add a new user

With this command, you can add a new user to the system. When adding a new user, you will be asked to enter some information.

$ sudo adduser user

Sample Output:

Advertisement

adduser command to add a new user

/etc/passwd contains the user account information in the following format.

username:password:userID:groupID:comment:user_directory:shell
  • Username: It is the login name for the user. The username should be between 1-32 characters.
  • Password: It contains the user password in an encrypted format (x).
  • User ID: Every user has a unique User Identification Number (UID). 1001 is the user ID of deepak.
  • Group ID:  It shows the primary group ID of a user. 1001 is the primary group ID of deepak.
  • User detail: In between two colons, a GECOS or comment of a user will be displayed if added.
  • Home Directory: The user's home directory. The default location is the /home directory.
  • Login shell: The login shell for the user. The default login shell is /bin/bash.

 

2. Add a system user with adduser command

You can add a new system user using --system option with adduser command.

$ sudo adduser --system user

Sample Output:

adduser command to add a system user

 

3. adduser command to create a new user group

--group option allows you to create a new user group in the system. The group is created without any users.

$ sudo adduser --group group

Sample Output:

ubuntu@golinux:~$ sudo adduser --group linux
Adding group `linux' (GID 1002) ...
Done.

 

4. Add a system group with adduser command

We can use --system and --group option together to create a system group in the system. A system user with the same group name is also created.

$ sudo adduser --system --group group

Sample Output:

adduser command to add a system group

 

5. adduser command to add a new user in different primary group

adduser command adds a new group for a user with the same username. To add a user in a different group, we can use --ingroup option. The group should be already present in the system.

Advertisement
$ sudo adduser --ingroup GROUP user

Sample Output:

adduser command to add a user in different group

 

6. adduser command to add a user with different home directory

By default, adduser creates the user's home directory with username under the /home directory. We can create a user's home directory in a different location using --home option. It also allows us to specify a different name for the user's home directory.

$ sudo adduser --home /directory/home_dir_name user 

Sample Output:

Here, we are adding a user 'sam' with its directory 'record' under the /snap directory.

adduser command to add a user with different home directory

 

7. adduser command to create a user without a home directory

Normally, adduser command automatically creates the user's home directory when adding a user. We can use --no-create-home option to add a user without its home directory.

$ sudo adduser --no-create-home user

Sample Output:

adduser command to create a user without home directory

As we can see, the default home directory is /home/elliot but it is not created.

check home directory

 

8. Add a user with specific user ID with adduser command

Every user has a unique user ID in the Linux system. By default, adduser command assigns the next available user ID to a new user. We can use -u or --uid option to add a new user with a custom user ID.

$ sudo adduser -u userID user

OR

$ sudo adduser --uid userID user

Sample Output:

The user chris is assigned with a custom user ID 4567.

adduser command to add a user with custom user ID

 

9. adduser command to create a  user with specific group ID

We have already seen that the adduser command creates a new group with a user name and adds a user to that group. To add a user to a different group, we can use --gid option. The group should be already present in the system.

$ sudo adduser --gid groupID user

Sample Output:

adduser command to add a user with different group ID

 

10. adduser command to add a user with a custom login shell

adduser command selects the default login shell specified by DSHELL variable in /etc/adduser.conf. The default login shell is /bin/bash. To use a different login shell, we can use --shell option as shown below.

$ sudo adduser --shell SHELL user

Sample Output:

adduser command to create a new user with different shell

 

11. Add an existing user to an existing group with adduser command

Instead of adding a new user, you can also add an existing user to an existing group using adduser command.

$ sudo adduser user group

Sample Output:

add existing user to an existing group with adduser command

 

12. adduser command to create a user with a disabled login

--disabled-login option does not ask to set a password for a new user. The user won't be able to use the account until the password is set.

$ sudo adduser --disabled-login user

Sample Output:

adduser command to add a user with disabled login

We can set or change the user password in the Linux system by using the following passwd command.

ubuntu@golinux:~$ sudo passwd maxim
New password: 
Retype new password: 
passwd: password updated successfully

 

13. Create a user without password using adduser command

--disabled-password also does not ask to set a password when adding a user. It is similar to the above command --disabled-login.

$ sudo adduser --disabled-password user

Sample Output:

adduser command to add a user without password

 

14. adduser command to add a user with a GECOS or comment

A GECOS or comment field is generally used for keeping the information related to the user. We can use --gecos option to add a GECOS or comment when creating a new user.

$ sudo adduser --gecos GECOS user

Sample Output:

adduser command to add a gecos or comment

 

15. adduser command to print the debug information

We can use --debug option to print the information in a verbose mode. It is useful when debugging the adduser.

$ sudo adduser --debug user

Sample Output:

adduser command to print the debug information

 

16. Hide informational messages with adduser command

--quiet option hides the informational messages in the output. It does not hide warnings and errors.

$ sudo adduser --quiet user

Sample Output:

adduser command to suppress informational messages

 

Conclusion

adduser is a useful command to add a user or group in the Linux system. We hope you have learned to use adduser command from this article. If you still have any confusion, please feel free to ask us in the comment section.

 

What's Next

15 useradd command examples in Linux [Cheat Sheet]

 

Further Reading

man page for adduser command

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment