Sudo and root access

Learning is Inevitable, on that note, I'm starting the sudo and root access after An Introduction to Linux and Terminal Basics, moving on to the next one.

Sudo and root access

  • Everything we see on Linux is a file, the Linux operating system is just a grouping of files. Every command we run tells the files to group them to complete the work. Now I'm a user I live inside the home directory of the root directory.
  1. cd / - switch to root directory

  2. ls /home/ - listing the files in the home directory here in this case listing users.

Screenshot from 2022-08-09 22-01-57.png

  • Now every process or command I run, it'll always run as me being the owner of that process. It does not require any permission for accessing the directories which reside in the user directory /home/USERNAME but what if I need to run a command which accesses packages from / - root directory or install new packages?

  • These tasks seem out of hand for USER working with limited directories and permission.

  • Let's see if that's true:

Screenshot from 2022-08-11 14-24-11.png

  • Now I can't just change it into the root directory and try running it, it doesn't work like that, when we open up our terminal we see ourselves in the home directory as a user by default.

  • It means that I am responsible for the directories/files/applications that reside home directory, what's the catch here? why so? Why it doesn't work and need special permissions?

  • Because the changes made by the root user / super user are applied to the system as a whole irrespective of the user.

What is root access?

  • Root user also known as a superuser or The Administrator is the most powerful user of all the users in the system. Even if there's only one human user in the system there are more users in the system itself like system daemons and more, you can know more about it using the command sudo cat /etc/passwd

  • '/etc/passwd` is the file that contains data about all the users in the system.

  • The whole system files, packages, and applications come under the root, having root privileges means that one has complete control over the system, every Linux system has its own root user and there's only one.

  • As it is said Linux system is nothing but a group of files it is decided when the root permissions are needed and when not.

  • To view the permissions to a file or directories you can use the ll command which stands for long list

Screenshot from 2022-08-13 13-17-03.png

  • The image shown above lists the permissions of the applications, files, and packages of the root directory, it is the highest directory of all, every other directory comes under the root directory even the user directory as well
  1. cd - for the user's home directory.

  2. cd .. - for the previous directory from the present directory

  3. cd - for the previous working directory

  4. Try cd ../.. command from the user's home directory and see where it leads to.

linuxfilesystem.png

  • Hope the image above explains why running commands as a root doesn't require permissions

What is sudo?

  • To get permission to perform the tasks of an administrator as a user we need to use the keyword sudo as a prefix before the command, it stands for superuser do which again says that doing tasks as a root user.

Screenshot from 2022-08-13 18-30-49.png

  • While you're working on a project or setting up a new environment for an application or trying a new tool, sudo prefix is necessary to install new software, application, or tools, you may have to use it multiple times over a period of time so it's better to switch to temporary root session using the command sudo su which stands for superuser do switch user

  • The terminal switches to a new shell session to run commands as the root user therefore we don't have to use sudo multiple times

  • sudo su - is the command to start a new shell for a temporary root session where the environment and path variables will the set to the root directory as if the root user has logged into the terminal.

Screenshot from 2022-08-16 10-09-00.png

  • exit or ctrl + d - To exit the shell

  • sudo and su are two different command operations, I can run the su command without the sudo prefix but it still asks for a password which is the root password and each time I run the su command it asks for password whereas the sudo has a time limit of 15 minutes(it can be increased but not recommended) each time I run it, It doesn't ask for a password when sudo is used in that time frame.

  • I can also extend the time limit by using the command sudo -v for that particular session and sudo -k to reset the time stamp.

  • I can always know more about the flags and the commands by using man command_name

Screenshot from 2022-08-16 10-54-39.png

  • sudo -s, sudo -i are the commands(recommended) that work the same as the sudo su and sudo su - command

Screenshot from 2022-08-16 10-59-18.png

  • su command asks for the root password and it's disabled by default when I installed Ubuntu and started using it, I had to set the password for the root but what if I run the su command before, then I cannot run it I need to set the password.

  • But it's always better to let the user use the sudo prefix to log into the root shell, and we can permit who can use it while working in groups the admin can control that task so that the system administrator doesn't have to share the root password

  • A system administrator also can handle the access of using sudo prefix to members of the group on who can use it and who cannot.

  • Logging as root only works on a terminal and I'm still not allowed to log in as root in the graphical interface.

change password

  • To change the password of the user or even the root user the command is sudo passwd USERNAME, sudo prefix not needed if you logged in as a root user

  • Running the command with no arguments provided changes the password of the root

Screenshot from 2022-08-16 18-08-18.png

  • Hope you got a good idea about sudo and root access. Keep working and Happy learning.