How to Use the Command 'adduser' (with Examples)
- Linux
- December 17, 2024
The adduser
command is a utility in Unix-like operating systems used for adding new users to the system. It simplifies the process of setting up user accounts by automating tasks such as creating a home directory, assigning a group, and prompting for a password. By handling these necessities, adduser
efficiently manages user information and helps maintain system security and organization.
Use Case 1: Create a New User with a Default Home Directory and Prompt for a Password
Code:
adduser username
Motivation: When you need to add a new user to your system and want to ensure they have a home directory and a password for security, this command is essential. Having a home directory means that the user has a designated space to store personal files and configurations, while the password helps protect their data.
Explanation:
adduser
: This part of the command initiates the user addition process.username
: This specifies the name of the new user account. It could be any string that conforms to the system’s rules for usernames.
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' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
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 2: Create a New User Without a Home Directory
Code:
adduser --no-create-home username
Motivation: There might be situations where you want to add a user but without assigning them storage space on the system. This is common for users who only need access for specific administrative tasks or software execution, having no need for a personal file storage area.
Explanation:
adduser
: Initiates the process of adding a new user.--no-create-home
: This option tells the system not to create a home directory for the user.username
: Identifies the name of the new account.
Example Output:
Adding user `username' ...
Adding new group `username' (1002) ...
Adding new user `username' (1002) with group `username' ...
Not creating home directory `/home/username'.
Use Case 3: Create a New User with a Home Directory at a Specified Path
Code:
adduser --home /custom/path username
Motivation: Specifying a custom home directory is useful in environments with specific directory structures or storage requirements. For example, placing a user’s home directory on a separate drive for performance or organizational reasons.
Explanation:
adduser
: Begins the process for adding a user.--home /custom/path
: Directs the system to create the user’s home directory at the given path instead of the default.username
: States the desired username.
Example Output:
Adding user `username' ...
Adding new group `username' (1003) ...
Adding new user `username' (1003) with group `username' ...
Creating home directory `/custom/path' ...
Copying files from `/etc/skel' ...
Use Case 4: Create a New User with the Specified Shell as the Login Shell
Code:
adduser --shell /bin/sh username
Motivation: This is particularly useful in environments where users require a specific shell for compatibility with software applications or scripts. Specifying a shell in advance can help streamline configuration and ensure users have the right tools upon login.
Explanation:
adduser
: Begins adding a user to the system.--shell /bin/sh
: Specifies the login shell to be/bin/sh
or any valid shell of your choice.username
: Declares the new user’s account name.
Example Output:
Adding user `username' ...
Adding new group `username' (1004) ...
Adding new user `username' (1004) with group `username' ...
Creating home directory `/home/username' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
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 a Specified Group
Code:
adduser --ingroup groupname username
Motivation: Assigning a new user to a specific group during creation is useful in managing permissions and access control within the system. This ensures that the user has the appropriate rights and access levels right from the start, which is crucial for maintaining security protocols and workflow processes.
Explanation:
adduser
: Initiates the process of adding a new user.--ingroup groupname
: Specifies the primary group the user will belong to, offering appropriate permissions from the group.username
: Designates the name of the user account being created.
Example Output:
Adding user `username' ...
Adding new user `username' (1005) with group `groupname' ...
Creating home directory `/home/username' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
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]
Conclusion:
The adduser
command is an integral tool in user management on Unix-like systems, offering flexibility and control over user creation. By tailoring user options such as directories, shells, and group memberships, system administrators and users can keep their environments organized and secure, ensuring efficient resource management and adherence to security policies.