1,440 reads
1,440 reads

Understanding Sudo and Root in Linux

by Kiran Satya RajAugust 20th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The Linux operating system is just a grouping of files. Every command we run tells the files to group them to complete the work. Every Linux system has its own root user and there's only one. The root user also known as a superuser or The Administrator is the most powerful user of all the users in the system. To get permission to perform the tasks of an administrator as a root user we need to use the keyword 'superuser' to do the tasks again, which again says that doing that is in the same way.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Understanding Sudo and Root in Linux
Kiran Satya Raj HackerNoon profile picture


Learning is inevitable right, on that note I'm starting theĀ Linux file systemĀ 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.



  • 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:



  • 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?

  • The 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



  • 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.


Linux File System


  • 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.



  • 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 a 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.



  • 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 a 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



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



  • 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



Here are handy keyboard shortcuts for Ubuntu Desktop:

https://twitter.com/jksrtwt/status/1551131265918574593


Hope you got a good idea about sudo and root access. Happy learning buddy.


Trending Topics

blockchaincryptocurrencyhackernoon-top-storyprogrammingsoftware-developmenttechnologystartuphackernoon-booksBitcoinbooks