[email protected]Office hours: Mon – Fri 9:00AM – 5:00PMLinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedJuly 8, 2023

How to Add User to Group in Linux.

How To Add User To Group.

In this tutorial, we will explain how to add a user to a group in Linux systems. We will also show you how to remove a user from a group and how to create, delete, and list groups.

 

How to Add an Existing User to a Group

To add an existing user to a secondary group, use the usermod -a -G command followed the name of the group and the user:

sudo usermod -a -G groupname username

For example, to add the user liviu to the sudo group, you would run the following command:

sudo usermod -a -G sudo liviu

Always use the -a (append) option when adding a user to a new group. If you omit the -a option, the user will be removed from any groups not listed after the -G option.

On success, the usermod command does not display any output. It warns you only if the user or group doesn’t exist.

 

How to Add an Existing User to Multiple Groups in One Command

If you want to add an existing user to multiple secondary groups in one command, use the usermod command followed by the -G option name of the group separated by , (commas):

sudo usermod -a -G group1,group2 username

 

How to Remove a User From a Group

To remove a user from a group, use the gpasswd command wit the -d option.

In the following example, we are removing the user username from the group groupname:

sudo gpasswd -d username groupname

 

How to Create a Group

To create a new group , use the groupadd command followed by the group name:

sudo groupadd groupname

 

How to Delete a Group

To delete an existing group, use the groupdel command followed by the group name:

sudo groupdel groupname

 

How to Change a User’s Primary Group

To change a user primary group, use the usermod command followed by the -g option:

sudo usermod -g groupname username

In the following example, we are changing the primary group of the user liviu to developers:

sudo usermod -g developers liviu

 

How to Create a New User and Assign Groups in One Command

The following useradd command creates a new user named nathan with primary group users and secondary groups wheel and developers.

sudo useradd -g users -G wheel,developers nathan

 

Display User Groups

To display complete user information, including all the groups of which a user is a member of, use the id command followed by the username:

id username

If you omit the username, the command will print the information about the currently logged-in user. Let’s check the user liviu:

id liviu
uid=0(root) gid=0(root) groups=0(root)

From the output above, we see that the primary group of the user is users and it belongs to wheel, storage, libvirt, docker, and kvm supplementary groups.

Use the groups command to display the user’s supplementary groups:

groups liviu
wheel storage power users libvirt docker kvm

If no username is passed to the groups command, it will print the currently logged in user’s groups.

 

Conclusion

In this tutorial, we have shown you how to add a user to a group.

The same commands apply for any Linux distribution, including Ubuntu, CentOS, RHEL,

Keep reading
How to Implement Network Segmentation and Resource Isolation in Linux.
February 6, 2024

How to Implement Network Segmentation and Resource Isolation in Linux.

How to Implement Network Segmentation and Resource Isolation in Linux. Securing and managing networks in a Linux environment involves advanced practices like network segmentation and resource isolation. This guide explores these concepts, providing detailed instructions and examples for…

Read article
How to open or close ports in Linux.
February 6, 2024

How to open or close ports in Linux.

How to open or close ports in Linux. Effectively managing ports on your Linux system is crucial for both security and service accessibility. This comprehensive guide will provide a detailed walkthrough on opening and closing ports using the robust tools iptables and ufw. Understanding these…

Read article
How to use ypdomainname command.
January 23, 2024

How to use ypdomainname command.

How to use ypdomainname command. In the realm of Linux networking, the ypdomainname command plays a crucial role in the context of the Network Information Service (NIS). NIS, formerly known as Yellow Pages, is a distributed database service that facilitates the sharing of network configuration and…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, colocation, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Office hours: Mon – Fri 9:00AM – 5:00PM