adduser Command in Linux

Introduction to Linux User Account Management

This tutorial provides a practical guide to managing user accounts on a Linux system. You will learn fundamental system administration tasks such as creating new user accounts, setting user passwords and expiration dates, and assigning users to appropriate groups. We'll utilize the adduser command for account creation, passwd and chage for password and expiration management, and usermod for group assignments. Mastering these skills is crucial for any systemadmin working with Linux environments.

Creating a New User Account in Linux

This section details the process of creating a new user account on your Linux system.

We will use the adduser command to create a user named newuser. This command simplifies the process of adding new accounts.

sudo adduser newuser

Example output:

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

The adduser command prompts for a password and other optional user information. After creation, you can switch to the new user's account using the su command.

su - newuser

This command logs you in as newuser. Verify the home directory:

pwd

Example output:

/home/newuser

This confirms successful user creation and home directory setup.

Setting User Password and Account Expiration in Linux

This section covers how to manage user passwords and set expiration dates for enhanced security.

First, switch to the newuser account:

su - newuser

Change the password for the newuser account using the passwd command.

passwd

You will be prompted to enter and confirm the new password:

Changing password for newuser.
New password:
Retype new password:
passwd: password updated successfully

To set an expiration date, use the chage command with sudo privileges:

sudo chage -E 2023-12-31 newuser

This sets the account to expire on December 31, 2023. Verify with the chage -l command:

sudo chage -l newuser

Example output:

Last password change					: Feb 27, 2023
Password expires					: Dec 31, 2023
Password inactive					: never
Account expires						: Dec 31, 2023
Minimum number of days between password change		: 0
Maximum number of days between password change		: 99999
Number of days of warning before password expires	: 7

The output confirms that the password and account expiration settings have been applied successfully.

Adding a User to Existing Groups in Linux

This section demonstrates how to add users to existing groups, granting them specific permissions.

Switch back to the labex user with sudo privileges:

exit

List existing groups using a utility like `grouplist` (this command may vary depending on your distribution and configuration). A standard approach is using `getent group`

sudo grouplist

Example output:

labex
newuser
sudo

The output shows the available groups: labex, newuser, and sudo.

Add the newuser account to the sudo group, granting sudo privileges. This is a common task for systemadmins.

sudo usermod -a -G sudo newuser

The -a option ensures that the user is added to the group without removing them from other groups. The -G option specifies the group.

Verify the group membership using the id command:

id newuser

Example output:

uid=1001(newuser) gid=1001(newuser) groups=1001(newuser),27(sudo)

The output confirms that newuser is now a member of the sudo group.

Summary of Linux User Management

This tutorial covered the essential steps for managing user accounts on a Linux system, including creating new users with adduser, setting passwords and expiration dates using passwd and chage, and adding users to groups with usermod. These skills are fundamental for any systemadmin working with Linux.

Key takeaways include understanding how to create and configure user accounts, manage their security through password policies and expiration dates, and control their permissions by assigning them to relevant groups. These are crucial skills for managing user access and maintaining a secure Linux environment.

400+ Linux Commands