How to Create Users and Groups in Linux

Updated: Jan 30

User management is a basic task for Linux admins. We will learn how to create users, create groups, delete users, delete groups with Linux commands today.

We are going to use the following commands in this post.

  • useradd creates new users.

  • groupadd creates new groups.

  • userdel deletes users.

  • groupdel deletes groups.

  • usermod is for making changes to existing users.

  • passwd creates and changes passwords.

The following files are related for Linux user management task.

  • /etc/login.defs

  • /etc/default/useradd

  • /etc/passwd

  • /etc/group

  • /etc/shadow

Useradd - create New Users on Linux

The useradd command is included in most Linux distributions and is configurable to suit our requirements. Now we haven't set the password for this user. The second column for this user in file /etc/shadow shows !!. More info about useradd here. How useradd works in Linux.

[root@howtouselinux ~]# useradd test
[root@howtouselinux ~]# su - test
[test@howtouselinux ~]$ ls -lrta
total 20
-rw-r--r--  1 test test 124 Oct 27  2017 .mkshrc
-rw-r--r--  1 test test 231 Aug 21  2019 .bashrc
-rw-r--r--  1 test test 193 Aug 21  2019 .bash_profile
-rw-r--r--  1 test test  18 Aug 21  2019 .bash_logout
-rw-r--r--  1 test test 172 Feb 17  2020 .kshrc
drwxr-xr-x. 9 root root 124 Dec 30 12:30 ..
drwx------  2 test test  91 Dec 30 12:30 .
[test@howtouselinux ~]$ grep test /etc/passwd
[test@howtouselinux ~]$ id test
uid=50294(test) gid=50294(test) groups=50294(test)
[root@howtouselinux ~]# grep test /etc/shadow

Groupadd - creates new groups

we can use groupadd command to create new groups on Linux.

[test@howtouselinux ~]$ groupadd test
groupadd: group 'test' already exists
[test@howtouselinux ~]$ groupadd testnew
groupadd: Permission denied.
groupadd: cannot lock /etc/group; try again later.
[test@howtouselinux ~]$ exit
[root@howtouselinux ~]# groupadd testnew 
[root@howtouselinux ~]# grep test /etc/group

Userdel - deletes users

We can use userdel command to delete users on Linux. With -r option, this will remove the files of this user ( like home directory and mail pool etc). We need to be careful before we run this command. Otherwise, the files of this user are still on the system. How Userdel Works In Linux

 -r, --remove Files in the user's home directory will be removed along with the home directory itself and the user's mail spool. Files located in other file systems will have to be searched for and deleted manually.

In the following example, we don't use -r option. The home directory for this user and file filefortest under /root are still there.

[root@howtouselinux ~]# touch filefortest
[root@howtouselinux ~]# chown test:test filefortest 
[root@howtouselinux ~]# ls -lrt filefortest 
-rw------- 1 test test 0 Dec 30 13:05 filefortest
[root@howtouselinux ~]# userdel test
[root@howtouselinux ~]# id test
id: test: no such user
[root@howtouselinux ~]# cd /home/test/.
./             ../            .bash_history  .bash_logout   .bash_profile  .bashrc        .kshrc         .mkshrc 
[root@howtouselinux ~]# grep test /etc/passwd
[root@howtouselinux ~]# grep test /etc/group
[root@howtouselinux ~]# ls -lrt filefortest 
-rw------- 1 50294 50294 0 Dec 30 13:05 filefortest

Groupdel - deletes groups

We can use groupdel to delete groups. We may not remove the primary group of any existing user. We must remove the user before you remove the group. We should manually check all file systems to ensure that no files remain owned by this group.

[root@howtouselinux ~]# groupdel testnew  
[root@howtouselinux ~]# grep testnew /etc/group

Usermod - make changes to existing users

We can use usermod to add the user to supplementary groups.

[root@howtouselinux ~]# id test
uid=50294(test) gid=50294(test) groups=50294(test)
[root@howtouselinux ~]# groupadd testnew
[root@howtouselinux ~]# usermod -aG testnew test
[root@howtouselinux ~]# id test
uid=50294(test) gid=50294(test) groups=50294(test),50295(testnew)


Join our newsletter. Get a free Linux account on Cloud.

Get a Free Cloud Server! 

We can use this cloud server to practice Linux commands. Never miss a post!

Thanks for submitting!