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.
cd / - switch to root directory
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
cd
Ā - for the user's home directory.cd ..
Ā - for the previous directory from the present directorycd -
Ā for the previous working directory- TryĀ
cd ../..
Ā command from the user's home directory and see where it leads to.
- 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.