Safely using the root user in Linux systems
If you are a system administrator working on Linux systems, you would definitely know about the root user. A root user has all the permissions in a Linux system, ranging from modifying files to manipulating or executing processes. However remaining logged in as the root user at all times should be avoided at all times.
Naturally, you are almost never logged in as the root user in your personal computers. However, when you are working on a remote virtual machine, you are given full root access to the system, which runs on the cloud. The right way is to create a new user, and work on it. A detailed explanation of how to do it in Digital Ocean is provided in an existing article on how to test run FileCloud on a VM in just a few minutes.
When do you need root privileges?
You need root access only when you are performing specific tasks like installing software on the system level (which is accessible to all users) or administrative tasks like configuring system level services, adding or managing user accounts.
How is being logged in as root at all times harmful?
The biggest mistake that you can do as the root user is wipe important directories. Running a simple command such as the one given below can wipe your whole computer clean!
rm -rf /
Secondly, running anything- be it a program or a build process- as the root user grants the process all privileges on the system, meaning the process can do absolutely anything on your system during the session. You should understand that a simple typo as the root user can cost you a lot!
One of the reasons Linux is so secure is that you normally don’t work with administrator permissions because only the root user has full access to those. Running a process while logged in through the root user takes that away by granting full access to your system. Imagine the mayhem that some malware can cause!
Unfortunately, most of these tasks that are performed through the terminal are irreversible. Once you commit an error, chances are that you can’t get the previous state back. Therefore, you need to be very careful when logged in as the root user.
How to perform root actions safely?
Logging into the root user every single time for the purposes of running certain management commands or accessing restricted files can be a cumbersome process. Thankfully, there is an easier way to do so.
You can always run specific commands as the root user by prefixing ‘sudo’ to your command. Sudo allows us to run single commands with root user privileges! However, the user you are logged in as must have the sudo privileges, which can be granted by adding the user to the sudoers list. To do so simply run the following command (as root)
adduser <username> sudo
You can alternately edit the /etc/sudoers file but you would need root access to perform taht task too.
Using sudo is safer because in case of brute force attacks, the attacker doesn’t really know which user is being used to make the sudo request, making it very difficult to gain over your system.
With this, we come to the end of the post explaining the use of root privileges in Linux systems safely. We hope that this helped you how the root privileges work in Linux and how you should use them judiciously. If you have any issues, feel free to leave a comment below.