Linux Basic Series: User and Group Management(P4)

Photo by Andrew Moca on Unsplash

Linux Basic Series: User and Group Management(P4)

This article explains how to create, change and manage user and groups in Linux.

This is the 4th part of 7 part series where I explain the Basics of Linux.

Creating and managing user accounts

Create new user accounts

The 'useradd' command is used to create new user accounts.

sudo useradd new_user

This command adds a new user with the username 'new_user'.

The above commands tels us that new_user has been created.

Change the password for a user account

The 'passwd' command is used to set or change the password for a user account.

sudo passwd new_user

This command changes or sets new password of 'new_user'.

Now you can log in using the command:

su new_user


Modify user account

The 'usermod' command is used to modify existing user accounts.

sudo usermod -c "This is a new  user" new_user

This will add a comment about the user or a short description related to the user.

sudo usermod -d /home/newuser new_user

This will change the home directory of the user to /home/newuser.


Managing groups

Create new groups

The 'groupadd' command is used to create new groups.

sudo groupadd newgroup

This will create a new group called "newgroup".

Add or remove users

The 'usermod' command can also be used to add or remove users from a group.

sudo usermod -aG  newgroup new_user

In this command, the '-aG' option specifies that you want to add the user "newuser" to the "newgroup" group. The '-a' option means that you want to append the group membership to the existing list of groups that the user belongs to, rather than replacing it. The '-G' option specifies the name of the group to which you want to add the user.

Delete group

The 'groupdel' command is used to delete groups.

sudo groupdel newgroup

This command will delete 'newgroup' group from the system, along with any associated user accounts or permissions.


Changing user and group ownership

Change owner

The 'chown' command is used to change the owner of a file or directory.

sudo chown new_user test.txt

The above command will change the owner of a file called 'test.txt' to a user named 'newuser'


Change group ownership

The 'chgrp' command is used to change the group ownership of a file or directory.

sudo chgrp newgroup test.txt

The above command will change the group ownership of the file 'test.txt' to a group named 'newgroup'.