Today's lab session is essential to have a solid start in this module. Make sure you complete all the tasks before the next class.
Lab 1 part 2 focuses on how to:
Lab 1.2 Learn how to use Linux commands to manage users of your VM.
This tutorial describes the steps to manage users with different security privileges. We will mainly use the sudo keyword, representing the system's superuser. We already used the sudo in lab 1.2 to run commands (for example, installing software).
The
sudo(from the “superuser do” phrase) represents a group of accounts with special security privileges related to installing/uninstalling software, manipulating folders and files, and making system and application configurations.Note that numbering continues from Lab 1.2
- Let’s create a new user with a different username; for example, you can use your name. In my case, I will create a new user called
bilbofrom the Shire 🌲.- Note that we are still logged in as our existing user (with sudo privileges).
- We will use the
sudo addusercommand followed by the user name, in our case,bilbo. - Enter a password and then complete the rest (or leave it empty by pressing
Enter).- My password is
myprecious❗
- My password is
$ sudo adduser bilbo
...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
...
Changing the user information for username
Enter the new value, or press ENTER for the default
Full Name []: Bilbo Baggins
Room Number []: Bag end
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] Y- Our new user
bilbo🙋 is now ready. *bilbohas a new directory in/home/bilbo. * Butbilbois not a superuser (sudoer). - To add the new user as
sudo, we need to use theusermodcommand.
$ sudo usermod -aG sudo bilbo- This command adds
bilboto thesudogroup. What does-aand-Gstand for? Simple running theusermodcommand. This shows a list of possible options.
$ usermod
Usage: usermod [options] LOGIN
Options:
...
-G, --groups GROUPS new list of supplementary GROUPS
-a, --append append the user to the
supplemental GROUPS mentioned by
the -G option without removing
him/her from other groups
...-
Let’s recap; we have a new user
bilbothat is also asudouser.🚩 The Linux community usually refers to sudo user as sudoer. Respectively,
yodaandbilboare bothsudoerusers. -
I will switch to the new user account using the following command.
$ su - bilboNote that the dash
–option helps you to change to the new user while at the same time navigating tobilbo’s home directory.
- You can always exit from
bilboand return to your current user by running theexitcommand. For the moment, remain asbilbo. - Now, let’s change the
bilbo's password; we will use thepasswdcommand.
$ sudo passwd bilbo
[sudo] password for bilbo: # Type in the default password
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully🚨 Your password is again hidden.
- Let’s create a new user called
frodo; his password issting. Go on and create a new user.
You might need to go back ⬆️ to step 42.
- We can see the folders created in the home directory using the following command.
There might be a few user home directories there, 🚨 note that user
ubuntuis asudoer,and it is a default created.
$ ls /home
... bilbo frodo ... ubuntu ...- I will delete user
frodo🙅 ; I will need to run the following command for this action.
$ sudo userdel frodo- Let's check once more the folders in the home directory.
$ ls /home
... bilbo frodo ... ubuntu ...
🚨 Mmm.. deleting a user does not necessarily remove the user's home folder.
- Let's delete it.
$ rm -rf /home/frodo
rm: cannot remove '/home/frodo/.bash_logout': Permission denied
rm: cannot remove '/home/frodo/.profile': Permission denied
rm: cannot remove '/home/frodo/.bashrc': Permission denied🚨 We cannot delete it! Except if we are a
sudo!
Permission deniedsignifies that the folder belongs tofrodoand not tobilbo.
- Apparently, we need to be a
sudoerto run such commands to force deletion of the folders belonging to other users. Try the following command.
$ sudo rm -rf /home/frodo
Now the folder is gone!
- Let's validate our assumptions.
$ ls /home
... bilbo ... ubuntu ...
-
Let’s summarize the commands that need
**sudoer**privileges-
sudocommand → Runs a command as a superuser -
adduser frodo→ Creates a new user `frodo`` -
``usermod -aG sudo frodo
→ Makesfrodo` a superuser -
su - frodo→ Switch between users, by navigating into their home directory -
passwd frodo→ Changefrodo's password. -
userdel frodo→ Deletesfrodo, but notfrodo's home directory
-
-
The following command changes the user privileges from
sudotonon-sudouser, in other words, it downgrades an account privilege.
$ sudo gpasswd -d username sudo
Make sure that you always have access to a superuser account before downgrading or deleting other accounts.
🚨 Linux operating system allows you to remove
sudoaccess from an account that you are already logged in to. If you have only onesudoaccount and you downgrade its privileges, you will lose superuser access to your system.This means that your Linux system is now
sudo lockedmaking your system unusable (you cannot install delete etc. but you can list and manipulate only your files).
🏁 Well done! You completed parts 1 and 2, now do both one more time 💪