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, 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. Show
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
PrerequisitesTo 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 LinuxBefore 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.
2. Next, run the 4 command below to add a user ( 5) without a password ( 6) to the home directory ( 7) of your Linux system. The 8 command runs the command with elevated privileges.
3. Now run the 9 command followed by the user’s name ( 5) to verify if the user is properly added to the system.
You’ll see a randomly generated uid, gid, and groups attributes of the user (shanky) like in the image below. Verifying the New User ExistsOther than the uid and gid, perhaps you also want to see the home directory of the user you added. If so, run the 1 command below. The 2 file contains all user accounts’ information, either already or newly created in the system.Notice the 3 command below filters the content of the 2 file to find lines with the word 5 in them, then pass the result to the 6 command to print on the terminal.
You can see in the screenshot below that a home directory (/home/shanky) exists for the user named shanky, which indicates the user exists. Verifying User ExistenceModifying the User Account Expiration DateNow 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 7 command below. The 8 command lists user’s account information and modifies passwords and accounts expiry dates by default. For this example, the 9 option is added to list the user’s ( 5) information.
You can see below that the account’s expiry date is Jan 01, 1970 Listing User’s Information to see Account Expiry DateNow let’s see a quick example of changing a user account’s expiry. Run the 8 command below to change user’s ( 5) account expiry ( 3) to September 01, 2021 ( 4).
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. Listing User’s Information to see New Account’s Expiry DateResetting User’s PasswordEarlier, 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 8 command below. The 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 0 option, the 9 command deletes the user’s password, like this: 2.
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. Resetting a User’s PasswordChanging Account’s UsernameEarlier, 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 3 command below to change the account’s ( 5) username to 5. The 3 command modifies account files based on the changes you specified when you run the command.
Now run the 9 command followed by the user’s name ( 5) to verify if the username is changed.
Below, you can see that the user’s login name is now set to shankyo. Verifying if the User’s Login Name is ChangedManaging User Group MembershipChanging 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 9 command will do the trick. The 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 ( 5) still benefits from the “shanky” group’s permissions.1. Run the 9 command below to remove ( 0) the user ( 5) from the group named 5.
2. Next, run the 6 command below to create a group named 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 8 command creates a new group account based on the values you specified on the command.
3. Finally, run the 3 command below to add the user ( 0) to the group ( 1). And then, run the 2 command to return the group(s) that the user ( 5) belongs to. 0Viewing Groups the User Account Belongs toRemoving a User with Linux Delete UserNow 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 4 command is what you need. The 3 command removes users and groups from the system according to the options you specify in a command.Run the 3 command below to delete all files owned ( 7) by the user account ( 5) from your Linux system. The command removes the user account’s ( 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. 1Now run the 9 command, followed by the user’s login name ( 5), to verify if the user is deleted from the system.
Below, you can see the message that says id: ‘shankyo’: no such user. Verifying the User Account is RemovedConclusionIn 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 GuidebooksMore from ATA Learning & Partners
|