When you work with a form of Linux, for example, Ubuntu or CentOS, you quickly encounter the 'sudo' command and the term 'root', but ...
What is sudo / root?
A Linux VPS has a root user, which you can compare with the administrator on a Windows computer. This root user has full access and rights to the operating system of your VPS (unlike other users) by default.
Using Sudo
Many command-line commands can only be executed by a root user. However, if you work via SSH or the VPS console on your VPS, you will usually be logged in as a different user at first.
To be able to execute commands as root user as this (non-root) user, the 'Sudo' command is used, which stands for 'super user do'.
You use 'sudo' by placing it in front of the command you want to execute, for example:
sudo nano /var/log/example
The root user’s password is then requested before the command is actually executed. The password is remembered for 15 minutes. During this time you won't be prompted again for your password.
This is the safest solution for working in Linux with root rights. Using the root user instead of the regular account with the sudo command brings additional security risks. Imagine forgetting to lock your computer while logged in as a root user. A potentially malicious user can cause a lot more damage compared to if you were logged in as a regular user.
Switching to the root user
Alternatively, if you are connected via SSH, you can switch from a regular user to the root user. Thereafter, you do not have to place 'sudo' in front of the commands that you execute. There are two methods for switching to the root user:
- Switching to the root user and (automatically) invoking a new login shell. This resets most environment variables and is the cleanest and most secure method for switching to the root user. It is done using the command:
su -
- Switching to the root user without invoking a new login shell. You could say that using this method, you're faking being a root user.
While using this method, with most operating systems you won't notice much difference and you'll still keep most if not all environment variables, with the exception of Debian 10. Debian 10 keeps the $PATH variable of the user with which you originally logged in. The result is that if you use 'su root' in Debian 10, you're missing folders in $PATH, such as /sbin and you won't be able to use many commands, such as reboot. Besides this, su - is also safer, as is excellently explained with an example on this page.
You switch to the root user without a new login shell with the command:
CentOS Stream / AlmaLinux / Rocky linux:
su root
If your user account has root privileges, you use the following command:
sudo su
Ubuntu:
With Ubuntu, your account has root privileges by default and you use the command:
sudo su
With the 'exit' command, you switch back to the user with whom you initially connected to your VPS.
Debian:
By default, Debian comes without sudo. Do you still want to use sudo instead of switching to the root user (see below)? Then, first switch to the root user and install sudo with:
apt-get install sudo -y
Next, you give yourself root rights before you are actually able to use the sudo command, see the steps above.
Giving yourself root rights
In most OSs, your default user does not have sudo / root permissions. Do you want to give these permissions to yourself? Then run the following command as root user (replace user with the actual username) and then restart your VPS:
usermod -aG sudo user
If you get a message that the sudo group does not exist, add the sudo group (if necessary). To do so, first open visudo:
visudo
Check if the following content is in it and if not, add it (replace sudo by wheel in CentOS):
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
Should you have any questions left regarding this article, do not hesitate to contact our support department. You can reach them via the ‘Contact Us’ button at the bottom of this page.