Introduction To Users And Groups In Linux
In this tutorial we will show you the basics for working with Linux Users And Groups, we will slightly upgrade with additional knowledge where necessary and more.
Why do we need to understand user accounts in Linux?
Linux is a multi-user platform, in which each user has different rights. Some can read, others can read and write, or have the right to see certain directories. Also, more than one user can be in the system at the same time.
What this article will cover?
- Linux users [ create, delete, search, lock ]
- User groups [ create , delete , serch, assign ]
- Empowering users
- Adding a user to sudo rs
- Alternative commands
- 60 second video tutorial for easier adaptation
Adding a user in linux
To add a user we use the command adduser, here is the base syntax:
adduser [--home DIR] [--shell SHELL] [--no-create-home] [--uid ID]
[--firstuid ID] [--lastuid ID] [--gecos GECOS] [--ingroup GROUP | --gid ID]
[--disabled-password] [--disabled-login] [--add_extra_groups]
[--encrypt-home] USER
Add a normal user
Code language: Bash (bash)
As you see in the basic syntax on the bottom there is a text “Add a normal user”. That means that we have other options like adding a user group or system group, adding a user to a group and more. If you type adduser –help you will see all of the options but for now we are going to create our first user. Lets say his name is John.
# Adding our first user (no capital letters)
$ adduser john
# Output
Adding user `john' ...
Adding new group `john' (1003) ...
Adding new user `john' (1003) with group `john' ...
Creating home directory `/home/john' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for john
Enter the new value, or press ENTER for the default
Full Name []: John
Room Number []: 13
Work Phone []: 0878555123
Home Phone []: 859 31 21 11
Other []: A test user ( to be deleted after)
Is the information correct? [Y/n] y
Code language: Bash (bash)
Now we have created our first user with the name John Wayne, working in room 13 with a work phone 0878555123 and home phone 0878555123, and we have a good description to know that we need to delete him after this tutorial.
useradd
is native binary compiled with the system. But, adduser
is a perl script which uses useradd
binary in back-end.adduser
is more user friendly and interactive than its back-end useradd
. There’s no difference in features provided.Creating a group and adding it to the user
When we create a user, he automatically gets his primary group for example John:John, this is his main group.
But we can also give John the right to work with other groups they are going to be his supplementary groups, so he can work along with other users. Now lets create a test group and add him to it.
# Create a new group
$ sudo groupadd new_group
# Add user to group
$ sudo adduser john new_group
Code language: PHP (php)
$ sudo usermod -aG sudo john
$
sudo usermod –a –G new_group
johnOk, so now we have a new user John, he has his primary group john and his supplementary group “new_group”. But what other groups does John have? We can check that with our next command “groups”.
# Display which groups a user is member of
$ groups john
john : john cdrom dip plugdev lxd new_group
Code language: PHP (php)
Deleting Users And Groups, disabling user login
$ sudo groupdel new_group
Okey but now we have decided to remove John from new_group so we do the following:
# Remove a user from group
$ sudo gpasswd –d john new_group
Code language: PHP (php)
But lets not stop there, John has been a bad boy and we want to disable him from entering our system:
# Disable user login in system
passwd -l john
# Enable user login in system
passwd -u john
Code language: PHP (php)
After a little thinking we have decided that we want to delete John forever so lets do it!
# Delete user from system
$ sudo deluser john
Code language: PHP (php)
Here is a 60 second video tutorial for easier adaptation on the above commands
Conclusion
Now John is no more 🙂
But seriously, in Linux, working with users and groups is not complicated, there is a wide range of commands, and in this tutorial we tried to show you each one in a way that we think is memorable. In addition, we offer you a short video in which you can see how the commands work.