Users and Groups in Linux

By | June 5, 2023

In this article, I am explaining what are users and groups in Linux and how to create users, apply different policies on users and delete them. I am also explaining how to create groups in Kali Linux and how to add users to different groups.

After reading this blog about Users and Groups in Linux you will gain in-depth knowledge about Users and groups in Linux. So Let’s dig in.

So first I am explaining what is “User”?. Just like you create an account in some social media platform to manage all your activities same is the case with Linux, through a user account, you can manage your activities in Linux.

users and groups in Linux

Types of Users in Linux:

There are mainly three types of users in Linux:

  1. Normal User (Un Privileged User)
  2. System User
  3. Root User (Privileged User)

Groups in Linux:

Just like in Whatsapp, we add multiple people to send messages to all people at a time. Same is the case with Linux. In Linux, we create a group, add different users to the group and then implement policy on that group so that that group policy will be implemented on every user present in that group. It will be easy for us to manage different users by adding them into groups.

Users: Stored in /etc/passwd

Passwords /etc/shadow

Groups /etc/groups

Encrypted Group Passwords /etc/gshadow

How to Create a User in Kali Linux?

adduser username

For example:

If we want to add user xyz into your Kali machine then we can type the above command like

adduser xyz

The above command will not only create a user but will also create a home directory for the user and it will follow settings based on the configuration file.

The configuration file is present at

/etc/adduser.conf

You can view it through the cat command like for example

cat /etc/adduser.conf

How to get information about created users?

getent passwd username

for example:

If I want to get information about user xyz then I can type the above command as

getent passwd xyz

this will display details about user and its group.


To get information about all the users

getent passwd


How to change the password for the user?

passwd userName

For example:

if I want to change a password for the user xyz then I can type the above command as

passwd xyz

Changing the users information like Full Name

chfn userName

For example, If I want to change the name for the user xyz in our case then I can type the above command like

chfn xyz

Changing the shell for the user

For example, to change the shell for the user We can use the command like

chsh

Changing the age of the password for user

We can change the age of password for user through chage command.

chage userName

For Example:

If we want to change the age of a command for user xyz then we can type the above command like

chage xyz

When a user login next time, he or she must change the password.

passwd -e userName

For example:

If we want that our user xyz must change the password then he/she want to login next time. Then we can write above command as:

passwd -e xyz


How to disable a user account in Kali Linux?

To disable a user account in Kali Linux then we can type

passwd -l userName

For Example:

If we want to disable user account for xyz user then we can type the above command as:

passwd -l xyz

Now user account is disabled for the user xyz..

—‘

How to delete a user account in Kali Linux?

To delete a user account you can just type the following command as:

userdel userName

for Example:

To delete a user account of xyz user then we can write the above command as:

userdel xyz


How to delete a user account including its home directory and settings.

userdel -r userName

For Example:

userdel -r xyz

How to add group in Kali Linux?

To add group you can type the following command like:

addgroup groupName

For example:

To add a group name abc you can type the above command like:

addgroup abc


How to delete group in Kali Linux?

To delete group in Kali linux type the following command like

delgroup groupName

for example:

If I want to delte a group named abc. I can type the above command like:

delgroup abc


How to set a password for group in Kali Linux?

gpasswd groupName

For Example:

If I want to set a password for group abc then I can type the above command like

gpasswd abc


How to remove password for group in Kali Linux?

gpasswd -r groupName

For example:

If I want to remove a password for group abc then I can type above command like:

gpasswd -r abc


How to add user to a group?

In order to add a user to group we can type
the following command.

sudo usermod -a -G groupname username

For Example:

To add xyz user to group abc we can type above command like:

sudo usermod -a -G abc xyz

-a means that add user to the group without removing it from other existing group.

-G means that it must be followed by 1 or more group name.


How to add user to multiple groups.

To add a user to multiple groups we can use the following command like:

sudo usermod -a -G groupname1, groupname2 Username


How to remove a user from a group?

To remove a user from a group we can use commands like

sudo gpasswd -d username groupname

For Example:

In order to remove a user xyz from group abc we can use the above command like

sudo gpasswd -d xyz abc

xyz user will be removed from group abc.


How to delete a group in Kali Linux?

To delete a group in Kali Linux type the following command.

sudo groupdel groupName

For exampel: To remove group abc we can type the above command like:

sudo groupdel abc


How to view information about groups.

getent group


How to change a user’s primary group?

sudo usermod -g groupName username

For example:

To change a user primary group from abc to def then we can type the above command like:

sudo usermod -g def xyz


Creating User and assinging group in one command

sudo useradd -g users -G wheel,developers nathan

for example:

To add a user jkl and assinging a group abc.

sudo usermod -g users -G Group1,Group2,Group3


How to display user’s groups in Kali linux?

id userName

For example:

In order to veiw group of users xyz we can user the above command like.

id xyz


Displaying user’s supplementary groups

groups userName


usermod command is used to making changes in existing groups.


How to switch to root user?

sudo su

or

sudo -i


How to add user to sudo group in Kali Linux?

usermod -a -G sudo UserName

for Example:

In order to add xyz user to sudo group we can type the above command as:

usermod -a -G sudo xyz


Changing shell for user in Kali Linux?

chsh -s /bin/bash username

chsh means change shell
-s name fo the shell

/bin/bash

2 thoughts on “Users and Groups in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *