Linux commands to manage users and groups

Linux is a multi-user operating system. It means that many user can work on the system simultaneously.

You can find out user information from /etc/passwd file.

Each Linux user should belong to a specific group.

You can find out groups information from /etc/group file.

Here is the snapshot of typical passwd and group file in Linux.

passwd-file-contents

passwd-file-contents

Each line in passwd file contains below information.

<username>:x:<userid>:<groupid>:<group_name>:<home_directory>:<shell>

group-file-in-linux

group-file-in-linux

Each line in group file contains below information.

<groupname>:x:<groupid>

Below is the list of commands to manage users and groups in linux system.

  1. useradd – add new user into the system
  2. userdel – delete the specific user from the system
  3. usermod – modify the user details
  4. groupadd – add new group in the system
  5. groupdel – delete specific group from the system
  6. groupmod – modify the group details
  7. passwd – change or set the password for given user.
  8. finger user – Prints details about the user. Please note that you will to install finger package for this.
  9. whoami – gives name of the user currently logged into the system
  10. id – shows the user id, group id and group details for given user.

Setting up new user in Linux

You can create new user and set password for that user using 2 commands below. Below commands will create new user – “shaun”

sudo useradd shaun
sudo passwd shaun

Modifying user in Linux

You can modify user using usermod command. This command has many options that allow you to update the user.

  1. usermod -d /home/newdir/ shaun -> New home directory for the user shaun
  2. usermod -e 2017-12-30 shaun -> Set the expiry date for the user shaun
  3. usermod -g newg shaun -> change the primary group of user shaun
  4. usermod -aG secondaryg shaun -> append new group to user shaun
  5. usermod -l mark shaun -> change login name of shaun to mark
  6. usermod -L shaun -> lock user shaun
  7. usermod -U shaun -> unlock user shaun
  8. usermod -d /var/shaun/ -m shaun ->move home directory of shaun to new location
  9. usermod -s /bin/sh shaun -> Change the default shell of user shaun
  10. usermod -u 777 shaun -> Change the user id of shaun

You can also use multiple options in single line.

  1. usermod -d /home/kk/ -s /bin/bash -e 2018-02-22 -c “This is abc” -u 4 -aG g1 shaun
  2. usermod -u 111 -g 444 shaun

If you want to view the expiry date of user account, you can use below command.

  1. chage -l shaun

Note that you can change the account expiry details using chage command as well. For example, if you want to change the account expiry of user shaun, you can use below command.

chage shaun

Deleting the user using userdel command in Linux

You can delete a user using below command.

sudo userdel <username>

passwd command in Linux

When you execute passwd command, you will have to enter current password. If you want to change the password for other user, you can use below command. Note that you should be a root user to be able to change password of other users.

passwd <username>

A root user can also disable the password of other users using below command.

passwd -d <username>

If you want to change the passwords of multiple users at the same time, you can use below command followed by list of user name and their passwords.

chpasswd

<username>:<password>

id command in Linux

id command will display your user id, group id and the groups you belong to. If you want to see other user details you can use below syntax.

id <username>

group commands in Linux

You can use below commands to manage groups in Linux.

To add new group say “g1”, you can use below command.

sudo groupadd g1

To rename the group from say “g1”  to “newg1”, you can use below command.

groupmod -n newg1 g1

To change the id of group, you can use below command. Here we are changing the id of group g1 to 1010

groupmod -g 1010 g1

To delete the group say “newg1”, you can use below command.

sudo groupdel newg1

Switching user accounts

“su” command is used to switch user account.
Switch to a different user account using su command. Super user can switch to any other user without entering their password.

sudo is similar command but only allows execution of one command at a time with special privileges.

 

You may also like...