Open In App

7 Linux Commands For Managing Users

Last Updated : 01 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Linux is a fantastic platform that allows multi-user access options. Different users can access the same Linux OS for working simultaneously. A user account allows multiple people to access directories, terminals, the Internet, etc. There are three types of user accounts:

  • User Account: This account is for general use with limited permissions.
  • System Account: This account is used for operating specific programs such as daemon, MySQL, mail, etc.
  • Root Account: This account is for administrators. It has unlimited access to change the system.

7-Linux-Commands-For-Managing-Users

However, multiple users sharing the same passwords can be a problem for security reasons. Therefore, managing the users becomes essential to keep the system secured. Don’t worry because there are 7 Linux commands for managing users. So let’s discuss these commands in brief with the relevant examples:

1. Create a User

Creating users is one of the most common tasks in Linux, and the useradd command is used for it. For example, let’s add Eddie using the following command:  

sudo useradd Eddie
Linux - Create a User (useradd command)

useradd Command

If you want to assign a password to the user, execute the passwd command in the terminal. 

sudo passwd Eddie
Linux - Create a User (passwd command)

passwd Command

2. Delete a User

For deleting a user, run the userdel command, or add -r to delete its home directory and mail spool. So here we are deleting Norman from the system using the following command:

sudo userdel Norman
Linux - Delete a User

userdel Command

3. Display Detailed Information of a User

You can use the lslogins command to check the comprehensive information of all users. This command provides the complete overview of all users and groups, and here is the example:

$ lslogins
Linux - Display Detailed Information of a User

lslogins command

4. List the Users

You can either use compgen command or /etc/passwd to list down all system users. Output from compgen command is simpler than compared to the /etc/passwd. These are the commands you can execute:

$ compgen -u
Linux - List the Users (compgen command)

compgen command

$ cat /etc/passwd
Linux List the Users (cat /etc/passwd)

 cat /etc/passwd

5. Switch User Accounts

First, let’s identify the current user with the below command:

$ whoami
Linux - Switch User Accounts (whoami)

whoami 

As you can see in the above image, the current user is Morbius. Therefore, we will switch to user Eddie with the su command:

$ su Eddie
Linux Switch User Accounts (su command)

su command

Type exit to stop the user and back to the current user. 

6. Modify a User

With the usermod command, you can change the information of a specific user. In this example, we add a comment “symbiote” with the user “Eddie.”

$ sudo usermod -c "symbiote" Eddie
Linux - Modify a User (usermod command)

usermod command

To verify the successful modification, we check the user account name in the /etc/passwd file through the grep command:

$ grep 'Eddie' /etc/passwd
Linux Modify a User (grep command)

grep command

7. Add or Remove a User from a Group

You can add or remove users to a particular group using the addgroup and delgroup command. In this example, we are adding Eddie to MorbiVerse using the following command:

$ sudo addgroup Eddie MorbiVerse

To remove the user from a group, execute the below command:

$ sudo delgroup Eddie MorbiVerse

With the addgroup command, you can add different users to a group, like if five users are working in the development department, the admin can create a group of those users.

Linux Add or Remove a User from a Group

addgroup command and delgroup command

In a Nutshell:

This was the brief information and the examples of 7 Linux commands for managing users. We have used sudo multiple times in the commands because it gives all the admin privileges to a user. So if you are an admin and want to manage the users, then make sure you use the sudo command to make the changes in the system.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads