howtouselinux

5 commands to manage Users in Linux

Table of Contents

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.

The following commands can be used to manage users and groups in Linux.

  • 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 to Linux user management task.

  • /etc/login.defs
  • /etc/default/useradd
  • /etc/passwd
  • /etc/group
  • /etc/shadow

 

Useradd – create New Users on Linux

Adding a user in Linux is a fairly simple process. You can add a user by using the useradd command. To add a user, type the following command at the command prompt:

useradd username

This will add the user “username” to the system. The user will be added with a default home directory and a default set of permissions. If you want to add a user with a specific home directory, you can use the -d option. For example:

useradd -m -d /home/username username

This will add the user “username” with the home directory /home/username.

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:x:50294:50294::/home/test:/bin/bash
[test@howtouselinux ~]$ id test
uid=50294(test) gid=50294(test) groups=50294(test)
[root@howtouselinux ~]# grep test /etc/shadow
test:!!:18626:7:90:7:30::

Groupadd – create new groups in Linux

Adding a group in Linux is a fairly simple process. You can add a group by using the groupadd command. To add a group, type the following command at the command prompt:

groupadd groupname

This will add the group “groupname” to the system. The group will be added with a default set of permissions. Once you have added a group, you can then add users to the group by using the usermod command.

[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
test:x:50294:
testnew:x:50295:

Userdel – delete users in Linux

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
testnew:x:50295:
[root@howtouselinux ~]# ls -lrt filefortest
-rw——- 1 50294 50294 0 Dec 30 13:05 filefortest

Groupdel – delete groups in Linux

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 in Linux

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)

howtouselinux.com is dedicated to providing comprehensive information on using Linux.

We hope you find our site helpful and informative.

Learn More

Facebook
Twitter
LinkedIn

Linux du command

Basic Usage: Syntax: du [options] [directory/file] Basic Examples: du /path/to/file: Displays disk usage of the specified file. du /path/to/directory: Shows disk usage of all the

Read More »

RHCSA Practice Exam

RHCSA Practice Exam A General Notes Here are some tips to ensure your exam starts with a clean environment: You do not need external servers

Read More »