top of page

How to add a user to a group in Linux operating system?

We can assign a user to a group during adding a new user account. But how do we add an existing user to a group? This article helps you to learn how to add a user to a new group.


The right Linux command for the job is the usermod command and it can be explained as below :


NAME
       usermod - modify a user account

SYNOPSIS
       usermod [options] LOGIN

DESCRIPTION
       The usermod command modifies the system account files 
       to reflect the changes that are specified on the 
       command line.

As we can see, the Linux usermod command can be used to modify a user account. However in this article , we'll only use usermod command to add user to a new group.


For this example, we'll create a new group to practice. Use the groupadd command to create a new group:

imjhachandan@HP06Ubuntu:~$ groupadd DBA
groupadd: unable to lock group file
imjhachandan@HP06Ubuntu:~$ sudo groupadd DBA
[sudo] password for imjhachandan:
imjhachandan@HP06Ubuntu:~$

Don't forget to use sudo command in Ubuntu, else you'll get the groupadd: unable to lock group file error as in the example above or else add new group using sudo command like below mentioned:

root@HP06Ubuntu:~# groupadd DBA

Check whether the DBA group has been created in /etc/group file:

root@HP06Ubuntu:~# less /etc/group |grep DBA
DBA:x:1001:
root@HP06Ubuntu:~# logout

Next, we are going to add user to a new group. For this example, we are going to add a user called imjhachandan to the DBA group. Below are step by step instructions.


Use usermod -G option to add user to a new group:

imjhachandan@HP06Ubuntu:~$ sudo usermod -G DBA imjhachandan
[sudo] password for imjhachandan: 
imjhachandan@HP06Ubuntu:~$ 

Use Linux groups command to check whether the DBA group has been added to imjhachandan's group:

imjhachandan@HP06Ubuntu:~$ groups imjhachandan
imjhachandan : imjhachandan DBA
imjhachandan@HP06Ubuntu:~$ 

As you can see, the user imjhachandan now has DBA as a second group. We can also check /etc/group to verify user currently in the DBA group:

imjhachandan@HP06Ubuntu:~$ less /etc/group |grep DBA
DBA:x:1001:imjhachandan
imjhachandan@HP06Ubuntu:~$ 

That's all for this article.

Comments


jc_logo.png

Hi, thanks for stopping by!

Welcome! to my “Muse & Learn” blog.

This website will help you to learn useful queries/SQL, Tips to troubleshoot problem and their remediation, perform DB related activities etc... and don't forget to muse with us :)....

It cover few useful information on below topics :

 

MySQL, SQL Server, DB2, Linux/UNIX/AIX, HTML ....

Let the posts
come to you.

Thanks for submitting!

  • Instagram
  • Facebook
  • Twitter
© 2023 By ImJhaChandan
bottom of page