Which Linux command is used to delete a referenced user account from the system?

If you are logged into a Linux machine or plan to work as a Linux administrator machine, it’s crucial to know how to manage users, such as with the Linux delete user command,

id shanky
3. As a Linux administrator, you often need to work with various applications and logs that depend on which user you are using and its permissions.

Lucky for you, in this tutorial, you will learn how to manage Linux users by running various commands in day-to-day activity.

Let’s go!

Table of Contents

  • Prerequisites
  • Adding a User in Linux
  • Modifying the User Account Expiration Date
  • Resetting User’s Password
  • Changing Account’s Username
  • Managing User Group Membership
  • Removing a User with Linux Delete User
  • Conclusion

Prerequisites

To follow along with this tutorial, it is necessary to have a remote SSH host. This tutorial uses a Linux distribution, which is Ubuntu 18.04.5 LTS with sudo/administration rights.

Related:How to Set Up OpenSSH on a Windows Server [Complete Guide]

Adding a User in Linux

Before you manage users in Linux, obviously, there must be at least one existing user. So let’s start this tutorial by adding a user to a Linux system.

Related:A Windows Guy in a Linux World: Users and File Permission

1. Connect to your Ubuntu machine via SSH using your favorite SSH client.

You should not use the root user for any activity of the Linux machine because if anything goes wrong, it can corrupt the filesystem or even the operating system.

2. Next, run the

id shanky
4 command below to add a user (
id shanky
5) without a password (
id shanky
6) to the home directory (
id shanky
7) of your Linux system. The
id shanky
8 command runs the command with elevated privileges.

sudo useradd shanky --password -m

3. Now run the

id shanky
9 command followed by the user’s name (
id shanky
5) to verify if the user is properly added to the system.

id shanky

You’ll see a randomly generated uid, gid, and groups attributes of the user (shanky) like in the image below.

Which Linux command is used to delete a referenced user account from the system?
Verifying the New User Exists

Other than the uid and gid, perhaps you also want to see the home directory of the user you added. If so, run the

cat /etc/passwd | grep shanky
1 command below. The
cat /etc/passwd | grep shanky
2 file contains all user accounts’ information, either already or newly created in the system.

Notice the

cat /etc/passwd | grep shanky
3 command below filters the content of the
cat /etc/passwd | grep shanky
2 file to find lines with the word
id shanky
5 in them, then pass the result to the
cat /etc/passwd | grep shanky
6 command to print on the terminal.

cat /etc/passwd | grep shanky

You can see in the screenshot below that a home directory (/home/shanky) exists for the user named shanky, which indicates the user exists.

Which Linux command is used to delete a referenced user account from the system?
Verifying User Existence

Modifying the User Account Expiration Date

Now that you have created at least one user, let’s start managing the user, like modifying the user account expiry. The user account expiry is the date when a user account will expire. There are times you need to modify an account expiry for users for a specific purpose, such as the account expired earlier than expected or as per user’s request.

Before modifying the user account’s (shanky) expiry, first, check the current expiry date.

To check the account’s expiry date, run the

cat /etc/passwd | grep shanky
7 command below. The
cat /etc/passwd | grep shanky
8 command lists user’s account information and modifies passwords and accounts expiry dates by default. For this example, the
cat /etc/passwd | grep shanky
9 option is added to list the user’s (
id shanky
5) information.

chage shanky -l

You can see below that the account’s expiry date is Jan 01, 1970

Which Linux command is used to delete a referenced user account from the system?
Listing User’s Information to see Account Expiry Date

Now let’s see a quick example of changing a user account’s expiry.

Run the

cat /etc/passwd | grep shanky
8 command below to change user’s (
id shanky
5) account expiry (
chage shanky -l
3) to September 01, 2021 (
chage shanky -l
4).

chage shanky -E 2021-09-01 # Expiry date format: YYYY-MM-DD

Note that user account expiry is different from password expiry. Password expiry is the date when the current password will no longer work. To modify a user account’s password expiry date instead, replace the

chage shanky -l
3 option with the
chage shanky -l
6 option, then set the maximum days before the password expires. The complete command would be like this:
chage shanky -l
7

List the user’s information as you previously did to see the user account’s new expiry date. As you see below, the new account expiry date is set to Sep 01, 2021.

Which Linux command is used to delete a referenced user account from the system?
Listing User’s Information to see New Account’s Expiry Date

Resetting User’s Password

Earlier, you learned how to modify a user’s account and password expiry option, but it’s also important to learn how to reset a user’s password before it expires. Regularly resetting or changing a user account password helps secure a user’s password from exposing it to attackers. Or due to the most common reason, the user forgot the password.

To reset a user’s password, run the

chage shanky -l
8 command below. The
chage shanky -l
9 command alone lets you change a user’s password, but the function changes when you put additional options with it. For example, if you add the
chage shanky -E 2021-09-01 # Expiry date format: YYYY-MM-DD
0 option, the
chage shanky -l
9 command deletes the user’s password, like this:
chage shanky -E 2021-09-01 # Expiry date format: YYYY-MM-DD
2.

sudo passwd shanky

Enter and confirm the new password in the prompts, as shown below. Once done, you’ll get a message that says passwd: password updated successfully.

Which Linux command is used to delete a referenced user account from the system?
Resetting a User’s Password

Changing Account’s Username

Earlier, you performed lots of activity with an account named “shanky,” which is the account’s username. There are times when you need to change an account’s username, like when they change their name in the organization or as part of account security.

Run the

chage shanky -E 2021-09-01 # Expiry date format: YYYY-MM-DD
3 command below to change the account’s (
id shanky
5) username to
chage shanky -E 2021-09-01 # Expiry date format: YYYY-MM-DD
5. The
chage shanky -E 2021-09-01 # Expiry date format: YYYY-MM-DD
3 command modifies account files based on the changes you specified when you run the command.

sudo usermod -l shankyo shanky

Now run the

id shanky
9 command followed by the user’s name (
chage shanky -E 2021-09-01 # Expiry date format: YYYY-MM-DD
5) to verify if the username is changed.

id shankyo

Below, you can see that the user’s login name is now set to shankyo.

Which Linux command is used to delete a referenced user account from the system?
Verifying if the User’s Login Name is Changed

Managing User Group Membership

Changing a user’s login name is a sensitive task. What if you mistakenly added a user to a group with special permissions? How would you remove the user from that group? Don’t worry; the

chage shanky -E 2021-09-01 # Expiry date format: YYYY-MM-DD
9 command will do the trick. The
chage shanky -E 2021-09-01 # Expiry date format: YYYY-MM-DD
9 command lets you administer groups in your Linux system.

Whenever you add a new user (shanky), the system automatically adds that user to a group with the same name (shanky) by default. You previously changed an account’s username from “shanky” to “shankyo,” but that user’s account remains in the “shanky” group. As a result, the user (

chage shanky -E 2021-09-01 # Expiry date format: YYYY-MM-DD
5) still benefits from the “shanky” group’s permissions.

1. Run the

chage shanky -E 2021-09-01 # Expiry date format: YYYY-MM-DD
9 command below to remove (
chage shanky -E 2021-09-01 # Expiry date format: YYYY-MM-DD
0) the user (
chage shanky -E 2021-09-01 # Expiry date format: YYYY-MM-DD
5) from the group named
id shanky
5.

sudo gpasswd -d shankyo shanky

2. Next, run the

sudo passwd shanky
6 command below to create a group named
chage shanky -E 2021-09-01 # Expiry date format: YYYY-MM-DD
5 in the /etc/group file since the group doesn’t exist yet. The /etc/group file is a text file where groups are defined, one entry per line. The
sudo passwd shanky
8 command creates a new group account based on the values you specified on the command.

groupadd shankyo

3. Finally, run the

chage shanky -E 2021-09-01 # Expiry date format: YYYY-MM-DD
3 command below to add the user (
sudo usermod -l shankyo shanky
0) to the group (
sudo usermod -l shankyo shanky
1). And then, run the
sudo usermod -l shankyo shanky
2 command to return the group(s) that the user (
chage shanky -E 2021-09-01 # Expiry date format: YYYY-MM-DD
5) belongs to.

id shanky
0

Which Linux command is used to delete a referenced user account from the system?
Viewing Groups the User Account Belongs to

Removing a User with Linux Delete User

Now you have learned how to add and modify a user’s account, which is good enough to manage a user’s account. But perhaps a user account is not in use anymore or was added by mistake. In that case, the

sudo usermod -l shankyo shanky
4 command is what you need. The
id shanky
3 command removes users and groups from the system according to the options you specify in a command.

Run the

id shanky
3 command below to delete all files owned (
sudo usermod -l shankyo shanky
7) by the user account (
chage shanky -E 2021-09-01 # Expiry date format: YYYY-MM-DD
5) from your Linux system. The command removes the user account’s (
chage shanky -E 2021-09-01 # Expiry date format: YYYY-MM-DD
5) home directory and mail spool and removes the user from the /etc/passwd and /etc/shadow files. The /etc/shadow file contains information about the system’s account’s password.

id shanky
1

Now run the

id shanky
9 command, followed by the user’s login name (
chage shanky -E 2021-09-01 # Expiry date format: YYYY-MM-DD
5), to verify if the user is deleted from the system.

id shankyo

Below, you can see the message that says id: ‘shankyo’: no such user.

Which Linux command is used to delete a referenced user account from the system?
Verifying the User Account is Removed

Conclusion

In this tutorial, you learned how to add, manage and delete users, one of the many joys of being a Linux system administrator. You’ve also learned how to secure a user account by changing user’s login name and resetting a user’s password.

Now how would kick this newfound knowledge up a notch? Perhaps automating the password reset when a user’s password expires?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

More from ATA Learning & Partners

  • Which Linux command is used to delete a referenced user account from the system?

    Recommended Resources for Training, Information Security, Automation, and more!

  • Which Linux command is used to delete a referenced user account from the system?

    Get Paid to Write!

    ATA Learning is always seeking instructors of all experience levels. Regardless if you’re a junior admin or system architect, you have something to share. Why not write on a platform with an existing audience and share your knowledge with the world?

  • Which Linux command is used to delete a referenced user account from the system?

    ATA Learning Guidebooks

    ATA Learning is known for its high-quality written tutorials in the form of blog posts. Support ATA Learning with ATA Guidebook PDF eBooks available offline and with no ads!

    Which command can be used to delete a user from a Linux system?

    userdel command in Linux system is used to delete a user account and related files.

    What command can you use to delete user accounts?

    To delete a user account from your computer: Type net user and press Enter to view user accounts on your computer. Type net user username /delete, where username is the name of the user you wish to delete. For example, if the username is Bill, you would type net user Bill /delete.

    What are the command for adding and deleting a user in Linux?

    There are two command-line tools that you can use to delete a user account: userdel and deluser . On Ubuntu, you should use the deluser command as it is more friendly than the low-level userdel .