By default, the command sudo
executes a command as root
, which is equivalent to executing it as the system administrator. Running things as root
is necessary when modifying system files, which also happens when you want to install new software on the system.
On Linux systems, each program is executed by an "user." Even if it's your own PC that only you use, there will be multiple "users" registered in the system. One of them will be an user called root
. This root
user is special: it has complete access to the system.
For example, let's say the username you chose when you installed Linux was john
. If you open the terminal and run the command whoami
, it will tell you your username: john
. However, if you execute the command sudo whoami
, it will execute whoami
as the root
user, which means what will be printed on the terminal will be root
, not john
, because it's root
who is running the program.
Similarly, if you run apt install krita
, the Aptitude package manager will try to install Krita, but it won't be able to because you aren't allowed to install things on the system. If you run sudo apt install krita
, it will succeed because root
is allowed to install things on the system.
Sudoer Password
In order to run a command as root
, you must be a "sudoer," which means you're part of the sudoers user group in the system. This would be equivalent to a human system administration account on Linux, as root
isn't a human user or persona, it's closer to a concept.
When you install Linux on a PC, naturally you have a single admin (sudoer) account by default, so tutorials will tell you to run certain commands with sudo
to install software and things like that. However, if you SSH into a shared Linux server, e.g. that hosts your website, everyone who has a website hosted in it may be an user, and none of them may be sudoers. Only the staff managing the server would have administrator access, so you wouldn't be able to use sudo
in this case.
When you're a sudoer and you run sudo
, you will be prompted for your password. That is, the password of the user that is a sudoer. Not the password of the root
user. If you do not have a password and you're a sudoer, you won't be prompted (but honestly, you should have a password even if just to avoid running things as root
by accident).
On Linux Mint, the root
user doesn't have a password by default. What this means is that it's not possible to login as root
. It's possible to set a password for root
, but this doesn't affect how sudo
works at all, since sudo
doesn't care about what's root
's password, it only cares about your password. If you do set a password for root
, you'll be able to login as root
in bash
using the command su --login root
. When you do this, it will ask for root
's password, not your password.
- See How to Run a Program as a Different User in Linux Mint for details.
File Ownership Issues
The reason for some operations requiring root
access is due to the simple nature of Linux file permissions.
A file or directory on Linux may be "owned" by one user and one user group only (user:group
notation). By default, files that are owned by one user can't be modified by other users. If a directory can't be modified, that means other users can't create files in it. The directories where important system files are stored are all owned by root
, which means you can't delete all files on your system even by accident, because you won't have permission to delete the files you do not own. The exception is, of course, root
. The root
user can modify files by any users, and can also transfer ownership of files with the chown
command.
For example, if you run:
sudo touch $HOME/owned-by-root
This will create a file named owned-by-root
in your home directory that is owned by root. Let me explain why.
First, $HOME
will be expanded to /home/john
before sudo
is executed by the shell. The $HOME
directory of root
is /root/
, by the way.
Second, files created by an user are by default owned by that user. Thus, if you create a file while running a program as root
, that file will probably be owned by root
.
You won't be able to do anything with this file because you don't own it. To make the file yours, you can run:
sudo chown $USER:$USER $HOME/owned-by-root
This will change the ownership so that the owning user is you, and the owning user group is also you (on Linux, every user has a group with their own name).
A common problem that happens with new Linux users is that they run applications as root
and those applications create hidden configuration files in their home directory (often in ~/.local/
) which will owned by root
. This means the next time you try to run the application normally, it won't start because the files it needs to read are not owned by you anymore despite being literally inside your home directory.
You can this fix by running the command:
sudo chown --recursive $USER $HOME
This will make all files in your $HOME
directory become owned by you.
Leave a Reply