To delete a user on Linux, use the following command:
userdel username. To add users to a specific group in linux you can use this command:
addgroup groupname -G membership
You would need root privileges for that.
The “linux create user” command allows users to create a new Linux account. The “linux delete user” command deletes a Linux account.
It’s critical to know how to manage users, such as using the Linux remove user command, deluser, if you’re logged into a Linux computer or intend to operate as a Linux administrator machine. As a Linux administrator, you’ll often need to interact with a variety of apps and logs that are dependent on the person you’re working with and their rights.
Fortunately, you will learn how to manage Linux users in this article by executing several commands in your daily activities.
Let’s go!
Prerequisites
It is important to have a remote SSH host to follow along with this tutorial. This guide utilizes Ubuntu 18.04.5 LTS with sudo/administrative permissions as the Linux distribution.
Related: [Complete Guide] How to Set Up OpenSSH on a Windows Server
Adding a User to a Linux System
Obviously, there must be at least one existing user in Linux before you may manage users. So let’s get this course started by creating a user on a Linux machine.
Users and File Permissions: A Windows Guy in a Linux World
1. Use your chosen SSH client to connect to your Ubuntu system through SSH.
You should not use the root user for any Linux computer activity because if anything goes wrong, the filesystem or even the operating system might be corrupted.
2. Next, use the useradd command to add a user (shanky) to your Linux system’s home directory (-m) without a password (—password). The sudo command executes a command with administrative privileges.
useradd shanky —password -m sudo
3. Run the id command using the user’s name (shanky) to see whether the user has been correctly added to the system.
As seen in the graphic below, the user’s (shanky) uid, gid, and groups properties are produced at random.
Verifying the Existence of the New User
Aside from the uid and gid, you may also want to look at the user’s home directory. If this is the case, use the grep shanky | cat /etc/passwd command. The /etc/passwd file includes the information for all existing and newly created user accounts on the system.
The grep command screens the contents of the /etc/passwd file for lines containing the word shanky, then passes the result to the cat program, which prints the result on the terminal.
grep shanky | cat /etc/passwd
In the snapshot below, you can see that the user shanky has a home directory (/home/shanky), indicating that the user exists.
Checking for User Existence
Changing the Expiration Date of a User Account
Now that you’ve established at least one user, you may start controlling them, such as changing their account expiration date. The date on which a user account will expire is known as the user account expiration date. You may need to change an account expiration for users for a particular reason, such as if the account expired sooner than intended or at the request of the user.
Check the existing expiration date of the user account before changing the (shanky) expiry date.
Use the chage command to find out when the account will expire. The chage command displays a list of a user’s account information and, by default, updates passwords and account expiration dates. The -l option is used in this example to display the user’s (shanky) information.
The account’s expiration date is January 1, 1970, as shown below.
Account Expiry Date may be found by looking up the user’s information.
Let’s look at an example of adjusting the expiration date of a user account.
To update the user’s (shanky) account expiration (-E) to September 01, 2021, use the chage command (2021-09-01).
# Expiry date format: YYYY-MM-DD chage shanky -E 2021-09-01
It’s important to note that the expiration of a user account is not the same as the expiration of a password. The date on which the current password will no longer function is known as the password expiration date. Replace the -E option with the -M option to change the password expiration date for a user account, then specify the maximum days before the password expires. The whole command would be as follows: shanky change -M 30
To view the user account’s updated expiration date, list the user’s details as you did before. The new account expiration date is set to September 1, 2021, as shown below.
To check the Expiry Date of a New Account, look up the user’s information.
Password Reset for a User
You already know how to change a user’s account and password expiration option, but you should also know how to reset a user’s password before it expires. Resetting or changing a user account password on a regular basis helps protect a user’s password from being exposed to attackers. Alternatively, the user may have forgotten their password for the most usual cause.
Run the sudo passwd shanky command to reset a user’s password. The passwd command by itself allows you to change a user’s password, however when you add extra parameters to it, the function changes. The passwd command, for example, deletes the user’s password if you provide the -d option, as in sudo passwd -d shanky.
In the prompts below, enter and confirm your new password. You’ll get a notification that reads passwd: password changed successfully after you’re finished.
Password Reset for a User
Changing the Username of an Account
You have previously done a number of things using an account called “shanky,” which is the account’s username. There are instances when you need to update an account’s username, such as when the user’s name changes inside the company or for account security reasons.
To modify the account’s (shanky) username to shankyo, use the usermod command below. When you use the usermod command, it alters account files depending on the modifications you provide.
sudo usermod -l shankyo shankyo shankyo shankyo shankyo shankyo s
To see whether the username has changed, execute the id command followed by the user’s name (shankyo).
The user’s login name has been changed to shankyo, as seen below.
Checking to see whether the user’s login name has been changed
Managing the Membership of User Groups
It’s a delicate operation to change a user’s login name. What if you accidentally added a user to a group with restricted access? What would you do if you wanted to remove the user from that group? Don’t worry, the gpasswd command will take care of everything. You can manage groups in your Linux system using the gpasswd program.
By default, when you create a new user (shanky), the system adds that user to a group with the same name (shanky). Although you changed a user’s username from “shanky” to “shankyo,” the user’s account is still in the “shanky” group. As a consequence, the user (shankyo) continues to benefit from the permissions of the “shanky” group.
1. Use the gpasswd command to remove the user (shankyo) from the shanky group (-d).
sudo gpasswd -d shankyo shankyo shankyo shankyo shankyo shankyo shankyo
2. Next, use the groupadd command to create the shankyo group in the /etc/group file, if it doesn’t already exist. The /etc/group file is a text file with one item per line that defines groups. The groupadd command creates a new group account depending on the parameters you provide.
3. Finally, add the user (-a shankyo) to the group using the usermod command (-G shankyo). Then run the groups shankyo command to see which groups the user (shankyo) is a member of.
sudo usermod -a shankyo -G shankyo # Adds the user’shankyo’ to the shankyo group # This function returns the group(s) to which the user’shankyo’ belongs.
Viewing the Groups to Which the User Account Belongs
Delete User in Linux to Remove a User
You now know how to create and edit a user’s account, which is sufficient to manage a user’s account. However, it’s possible that a user account is no longer in use or that it was accidentally added. In such scenario, you’ll want to use the deluser command. According to the settings you give in a command, the deluser command removes users and groups from the system.
To erase all files owned (—remove-all-files) by the user account (shankyo) from your Linux system, use the deluser command. The user’s (shankyo) home directory and mail spool are deleted, as well as the user from the /etc/passwd and /etc/shadow files. The password for the system account is stored in the /etc/shadow file.
shankyo sudo deluser —remove-all-files
To check whether the user has been erased from the system, execute the id command followed by the user’s login name (shankyo).
The following is the message id:’shankyo’: There is no such user.
Verifying that the user account has been deactivated
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 Password Reset for a User.
How would you take your newfound knowledge to the next level? Perhaps automating the reset of a user’s password when it expires?
The “linux add user to group” is a command that allows users to add users to groups. The command is used for adding the current user to a group, or deleting a group and all of its members.
Related Tags
- linux list users
- ubuntu delete user
- linux remove user from group
- userdel -r username
- userdel cannot remove entry from /etc/passwd