How to use the command 'adduser' (with examples)

How to use the command 'adduser' (with examples)

The ‘adduser’ command is a user addition utility that allows users to create new users and specify various options such as the home directory, password, shell, and group for the new user. This command is particularly useful for system administrators who need to manage user accounts on a Linux system.

Use case 1: Create a new user with a default home directory and prompt the user to set a password

Code:

adduser username

Motivation: When creating a new user, it is common to provide them with a default home directory and prompt them to set a password upon first login. This use case allows you to quickly create a new user account while ensuring that the user has control over their password.

Explanation: The ‘adduser’ command is followed by the desired username. By simply providing the username as an argument, the command will create a new user with a default home directory and prompt the user to set a password.

Example output:

Adding user 'username' ...
Adding new group 'username' (1001) ...
Adding new user 'username' (1001) with group 'username' ...
Creating home directory '/home/username' ...
Copying files from '/etc/skel' ...
New password:
Retype new password:

Use case 2: Create a new user without a home directory

Code:

adduser --no-create-home username

Motivation: Sometimes, you may need to create a user account without allocating a home directory. This can be useful for system accounts that do not require a personal working directory, such as daemon or service accounts.

Explanation: The ‘–no-create-home’ option instructs the ‘adduser’ command not to create a home directory for the new user. If you want to create a user account without a home directory, you can include this option in the command followed by the desired username.

Example output:

Adding user 'username' ...
Adding new group 'username' (1001) ...
Adding new user 'username' (1001) with group 'username' ...

Use case 3: Create a new user with a home directory at the specified path

Code:

adduser --home path/to/home username

Motivation: In some cases, you may want to create a user account with a custom home directory rather than using the default location. This can be useful when your system has a specific directory structure or if you want to organize users into different directories based on their roles.

Explanation: The ‘–home’ option followed by the desired path allows you to specify the location of the home directory for the new user. By including this option in the ‘adduser’ command, you can create a user account with a home directory at the specified path.

Example output:

Adding user 'username' ...
Adding new group 'username' (1001) ...
Adding new user 'username' (1001) with group 'username' ...
Creating home directory 'path/to/home' ...
Copying files from '/etc/skel' ...
New password:
Retype new password:

Use case 4: Create a new user with the specified shell set as the login shell

Code:

adduser --shell path/to/shell username

Motivation: The login shell determines the command interpreter that will be used when the user logs in. By setting a specific shell for a user, you can control their command line environment. This can be useful if you want to create users with different default shells or restrict certain users to specific interpreters.

Explanation: The ‘–shell’ option followed by the desired path allows you to specify the login shell for the new user. By including this option in the ‘adduser’ command, you can create a user account with the specified shell as their default interpreter.

Example output:

Adding user 'username' ...
Adding new group 'username' (1001) ...
Adding new user 'username' (1001) with group 'username' ...
Creating home directory '/home/username' ...
Copying files from '/etc/skel' ...
Changing the user information for username
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]

Use case 5: Create a new user belonging to the specified group

Code:

adduser --ingroup group username

Motivation: Sometimes, you may want to assign a new user to a specific group. This can be useful when you want to grant group permissions or manage users based on their roles within the system.

Explanation: The ‘–ingroup’ option followed by the desired group name allows you to specify the group to which the new user will belong. By including this option in the ‘adduser’ command, you can create a user account and assign them to the specified group.

Example output:

Adding user 'username' ...
Adding new group 'group' (1002) ...
Adding new user 'username' (1001) with group 'group' ...
Creating home directory '/home/username' ...
Copying files from '/etc/skel' ...
New password:
Retype new password:

Conclusion:

The ‘adduser’ command provides a flexible and efficient way to create new user accounts on a Linux system. Whether you need to create a user with a default home directory, customize the home directory path, set a specific shell, or assign the user to a group, you can achieve all of these options with the ‘adduser’ command. By exploring and understanding the various use cases, system administrators can effectively manage user accounts and tailor them to specific requirements.

Related Posts

How to use the command 'az storage account' (with examples)

How to use the command 'az storage account' (with examples)

The az storage account command is part of the Azure CLI (Command-Line Interface) and is used to manage storage accounts in Azure.

Read More
How to use the command 'sha512sum' (with examples)

How to use the command 'sha512sum' (with examples)

The sha512sum command is a GNU Core Utilities tool that can be used to calculate SHA512 cryptographic checksums for files.

Read More
How to use the command 'vboxmanage-clonevm' (with examples)

How to use the command 'vboxmanage-clonevm' (with examples)

This article provides examples of how to use the ‘vboxmanage-clonevm’ command, which allows users to create a clone of an existing virtual machine (VM).

Read More