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.
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
Only root users or users with sudo privileges can add or remove users.
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
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
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.
Keep in mind that linux has multiple ways to add users. Like the binary command useradd. Here is the difference:
```
useradd
is native binary compiled with the system. But,
adduser
is a perl script which uses
useradd
binary in back-end.<br><br>
adduser
is more <strong>user friendly</strong> and interactive than its back-end
useradd
<h3>Creating a group and adding it to the user</h3>
When we create a user, he automatically gets his primary group for example John:John, this is his main group.<br>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.
$ sudo groupadd new_group
$ sudo adduser john new_group
If we want to add John as a <strong>sudo</strong> user we can do the following<br>$ sudo usermod -aG sudo john
Alternative method to <strong>adduser</strong> is <strong>usermod</strong> like for example:<br>$ ```
sudo usermod –a –G new_group
```john
Ok, 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".
$ groups john
john : john cdrom dip plugdev lxd new_group
<h3>Deleting Users And Groups, disabling user login</h3>
To delete a group you can use <strong>groupdel</strong> like so:<br>$ sudo groupdel new_group
Okey but now we have decided to remove John from new_group so we do the following:
$ sudo gpasswd –d john new_group
But lets not stop there, John has been a bad boy and we want to disable him from entering our system:
passwd -l john
passwd -u john
After a little thinking we have decided that we want to delete John forever so lets do it!
$ sudo deluser john
<a href="https://youtu.be/nq8XZClkrU4">Here is a 60 second video tutorial for easier adaptation on the above commands</a>
<h3>Conclusion</h3>
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.
We hope you enjoyed this article on Users And Groups if that is so please rate this page with the stars bellow and subscribe to our <a href="https://www.youtube.com/channel/UCh7Q9uaAt5-Z2lCZXX3OsvQ">YouTube channel</a>.