How To Create and Delete Users and give users administrative permission in Linux

Linux is a multi-user system, which means that more than one person can interact with the same system at the same time. As a system administrator, you have the responsibility to manage the system’s users and groups by creating new users and assign them to different groups .

Occasionally, you might need to delete a user account. Perhaps because the user moved away from the organization, or it was created for a specific service that no longer runs on the system.

This tutorial covers how to create users and assign administrative privileges’ by assigning them to groups and deleting them whenever the users are no longer needed.

1. How to check the list of users available in our Linux

To see list of users, you can look in to the passwd file in the etc directory using the cat or less or more command:

$ cat /etc/passwd
$ less /etc/passwd   Use the arrow keys to go up and down, and q to exit.
$ more /etc/passed  Use the Enter key or Tab  to go down.
wubie@D-T430:~$ more /etc/passwd
2. How to Add a User to Linux

To Add or delete users, you need to be logged in as root or a user with  sudo access.

In order to add a user you need to log in as a root user (su) or use sudo.

wubie@D-T430:~$ sudo adduser user-1

Now you can see that a new user ‘user-1’ is added:

By running $ cat /etc/passwd OR $ cat /etc/group we can see the new user added.

3. How to Delete/Remove Users in Linux

To delete the user, without removing the user files, run:

wubie@D-T430:~$ sudo deluser user-1

In most Linux distributions, when removing a user account , the user home and mail spool directories are not removed.

If you want to delete the user and its home directory and mail spool, use the --remove-home flag:

wubie@D-T430:~$ sudo deluser --remove-home user-1
4. How to give users Administrative right

In the above home directory we have two users, namely wubie and user-1. Let us check to which groups each of these users belong by running $ groups username command:

By default on Ubuntu systems, members of the group sudo are granted with sudo access. To add the user you created to the sudo group use the usermod command.

If you want the newly created user to have administrative rights, add the user to the sudo group :

$ sudo usermod -aG sudo username

Now ‘user-1’ is also a member of sudo group and hence have administrative right.

5. How to switch a

Switch to the newly created user:

$ su - username

Use sudo to run the whoami command: Is the user have sudo access then the output of the whoami command will be “root”:

If we create another user user-2 but not add it to the sudo group, running sudo command will give the following result:

Conclusion

In Linux, there are two command-line tools that you can use to create a new user account: useradd and adduser. The key difference between adduser and useradd is that adduser is used to add users with setting up account’s home folder and other settings while useradd is a low-level utility command to add users.

To delete a user accounts you can use deluser or userdel commands. userdel is a low-level utility, Debian and Ubuntu users will more likely use the friendlier deluser command instead.

After creating the account with the command useradd you need to run the following command as root to set a password for this newly created account:

$ passwd <username>

In most Linux distributions, when removing a user account with userdel, the user home and mail spool directories are not removed.

Use the -r (--remove) option to force userdel to remove the user’s home directory and mail spool:

$ userdel -r username

If the user you want to remove is still logged in, or if there are running processes that belong to this user, the userdel command does not allow to remove the user. In this situation, it is recommended to log out the user and kill all user’s running processes with the killall command:

$ sudo killall -u username      Once done, you can remove the user.

Another option is to use the -f (--force) option that tells userdel to forcefully remove the user account, even if the user is still logged in or if there are running processes that belong to the user.

$ userdel -f username
If you find this post important, share to your friends