Linux adduser Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to create a new user account, set the user's password and expiration date, and add the user to existing groups on a Linux system. The adduser command is used to create a new user account, and the passwd and chage commands are used to manage the user's password and account expiration. Additionally, you will learn how to add the new user to existing groups using the usermod command. This lab covers essential system administration tasks for managing user accounts on a Linux system.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux/UserandGroupManagementGroup -.-> linux/id("`User/Group ID Displaying`") linux/FileandDirectoryManagementGroup -.-> linux/pwd("`Directory Displaying`") linux/UserandGroupManagementGroup -.-> linux/usermod("`User Modifying`") linux/UserandGroupManagementGroup -.-> linux/passwd("`Password Changing`") linux/UserandGroupManagementGroup -.-> linux/su("`User Switching`") subgraph Lab Skills linux/id -.-> lab-422540{{"`Linux adduser Command with Practical Examples`"}} linux/pwd -.-> lab-422540{{"`Linux adduser Command with Practical Examples`"}} linux/usermod -.-> lab-422540{{"`Linux adduser Command with Practical Examples`"}} linux/passwd -.-> lab-422540{{"`Linux adduser Command with Practical Examples`"}} linux/su -.-> lab-422540{{"`Linux adduser Command with Practical Examples`"}} end

Create a New User Account

In this step, we will learn how to create a new user account on the Linux system.

First, let's use the adduser command to create a new user named newuser:

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 will prompt you to set a password for the new user, and also ask for some additional user information. Once the user is created, you can switch to the new user account using the su command:

su - newuser

This will switch to the newuser account, and you can verify the new user's home directory:

pwd

Example output:

/home/newuser

Now the new user account has been created successfully.

Set User Password and Expiration

In this step, we will learn how to set the password and expiration date for a user account.

First, let's switch to the newuser account that we created in the previous step:

su - newuser

Now, let's change the password for the newuser account using the passwd command:

passwd

You will be prompted to enter a new password and confirm it:

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

Next, we can set an expiration date for the newuser account using the chage command:

sudo chage -E 2023-12-31 newuser

This will set the account expiration date to December 31, 2023. You can verify the account expiration date using the chage 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

Now the user password has been set, and the account expiration date has been configured.

Add User to Existing Groups

In this step, we will learn how to add a user to existing groups on the Linux system.

First, let's switch back to the labex user, who has sudo privileges:

exit

Now, let's list the existing groups on the system:

sudo grouplist

Example output:

labex
newuser
sudo

We can see that there are three groups: labex, newuser, and sudo.

Now, let's add the newuser account to the sudo group, which will allow the user to execute commands with sudo privileges:

sudo usermod -a -G sudo newuser

The -a option adds the user to the specified groups, and the -G option specifies the groups to add the user to.

To verify that the newuser account has been added to the sudo group, we can use the id command:

id newuser

Example output:

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

You can see that the newuser account is now a member of the sudo group.

Summary

In this lab, we learned how to create a new user account on a Linux system using the adduser command. We set a password for the new user and also configured an expiration date for the account using the chage command. Additionally, we learned how to add the new user to existing groups on the system.

The key steps covered in this lab include creating a new user account, setting the user's password and expiration date, and adding the user to existing groups. These skills are essential for managing user accounts and permissions on a Linux system.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like