User Management in Linux

A user is an entity, in a Linux operating system, that can manipulate files and perform several other operations. Each user is assigned an ID that is unique for each user in the operating system. In this post, we will learn about users and commands which are used to get information about the users. After installation of the operating system, the ID 0 is assigned to the root user and the IDs 1 to 999 (both inclusive) are assigned to the system users and hence the ids for local user begins from 1000 onwards.
In a single directory, we can create 60,000 users. Now we will discuss the important commands to manage users in Linux.

1. To list out all the users in Linux, use the awk command with -F option. Here, we are accessing a file and printing only first colum with the help of print $1 and awk.

awk -F':' '{ print $1}' /etc/passwd

Awk-Command-to-List-Out-all-the-Users-in-Linux

2. Using id command, you can get the ID of any username. Every user has an id assigned to it and the user is identified with the help of this id. By default, this id is also the group id of the user.

id username

Example: id test



id-of-any-username

3. The command to add a user. useradd command adds a new user to the directory. The user is given the ID automatically depending on which category it falls in. The username of the user will be as provided by us in the command.

sudo useradd username

Example: sudo useradd geeks

Adding-a-user-in-linux

4. Using passwd command to assign a password to a user. After using this command we have to enter the new password for the user and then the password gets updated to the new password.

passwd username

Example: passwd geeks

assigning-a-password-to-the-user-in-linux

5. Accessing a user configuration file.



cat /etc/passwd

This commands prints the data of the configuration file. This file contains information about the user in the format.

username : x : user id : user group id : : /home/username : /bin/bash

Accessing-a-user-configuration-file

Now we will go through the commands to modify information.

6. The command to change the user ID for a user.

usermod  -u new_id username

This command can change the user ID of a user. The user with the given username will be assigned with the new ID given in the command and the old ID will be removed.

Example: sudo usermod -u 1982 test

Changing-the-user-id-in-linux

7. Command to Modify the group ID of a user.

usermod -g  new_group_id username

This command can change the group ID of a user and hence it can even be used to move a user to an already existing group. It will change the group ID of the user whose username is given and sets the group ID as the given new_group_id.

Example: sudo usermod -g 1005 test



modifying-the-group-id-of-a-user-in-linux

8. You can change the user login name using usermod command. The below command is used to change the login name of the user. The old login name of the user is changed to the new login name provided.

sudo usermod -l new_login_name old_login_name

Example: sudo usermod -c John_Wick John_Doe

Changing-user-login-name-in-linux

9. The command to change the home directory. The below command change the home directory of the user whose username is given and sets the new home directory as the directory whose path is provided.

usermod -d new_home_directory_path username

Example: usermod -d new_home_directory test

changing-home-directory-for-a-user-in-linux

10. You can also delete a user name. The below command deletes the user whose username is provided. Make sure that the user is not part of a group. If the user is part of a group then it will not be deleted directly, hence we will have to first remove him from the group and then we can delete him.

userdel -r username

Example: sudo userdel -r new_geeks

deleting-a-user-in-linux-forcefully

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.