So I've been reading into the differences between using su
and sudo
, and everyone seems to be in agreement that the sudo
approach is safer than allowing access to the root account itself. They say that with the root account you could break your entire system with just a single command. This I understand. BUT the initial user created on the system also has access to all commands using sudo. This I can find out by running su -l
. If this is the case, then I can simply run sudo <game-ending command>
to ruin my system. So how is this approach better or safer then allowing me direct access to the super user account? I can run all of the same commands...
Is it because using sudo
on the command line I am explicitly telling the computer I think I know what I am doing? Do they think people will forget they are under the super user account unless they explicitly say so?
People also state that if the system were compromised and entered by someone foreign, being under the root account would allow them to do terrible things to my system. But it seems to me if they already have access to my account, and know my password, they can do these same terrible things by using sudo and entering my password since they don't even need to know the super user's password. What am I not getting here?
Personally I do not necessarily consider it safer and most of the benefits (of sudo) are on a multi user system. On a single user system it probably is a wash.
The benefits are (in no particular order):
sudo -i
is probably the best method to isolate root's environmental variables from your user. This comes up from time to time but is moderately esoteric. See https://help.ubuntu.com/community/RootSudo#Special_notes_on_sudo_and_shellsThere are probably more benefits , but, those are the major ones, IMHO.
See also - https://help.ubuntu.com/community/RootSudo
To try to answer some of your other thoughts:
So while you have observed problems or flaws with sudo, su has the exact same vulnerabilities and su is not superior to sudo in those aspects, IMHO
Imagine you have 20 minutes to do something complex. You’re a bit hungover and you have to rush. “Let’s use su” you say. “It’ll save some time” is your reasoning.
By accident you type
instead of
Your system is now bricking itself and you have 10 minutes until your deadline.
If you explicitly choose when you need root, you can minimise the chance of this happening. Root might not be needed for
rm -r ./*
so why use it? Why take the risk?That’s what “safety” means here. Minimising the risk of users (all users, not just beginners) making a fatal mistake.
Of course, this is an extreme example that shouldn’t be allowed to happen in a production environment (I guarantee it has happened in a prod environment).
Security wise there’s some stuff that sudo is better for too. As @Panther says - logging, restrictions, root password is SPOF, etc.)
I want to add a bit of historical perspective to the other answers. Unfortunately, I do not have any sources ready except for my own memories of Usenet discussions and magazine articles.
Some time ago, in the 1990s, distributions were making it easier to install Linux on your own hardware, even with not much computer knowledge.¹ Thus, Linux started to attract more and more people that surprisingly had not previously been drilled as system administrators on some UN*X dialect. Instead, many were used to (single user) systems like Windows 95/98. And they learned that most Linux system administration tasks made it necessary to work under that strange "root" account.
Thus, some users just logged in as root and used that account for all their daily work. Why should they have to type
su
and the root password again and again or login into a new tty just for some admin commands? But using root for everything is of course not a good idea, as you could do a lot more harm to your system with some unmindful command in the wrong place. This even led some distro (was it SuSE?) to modify the desktop background for the root user to display a big warning that you should use that account only for admin tasks.So, the Ubuntu way with
sudo
has some advantages (in addition to those already listed by Panther).sudo
caches your credentials, so for multiple admin commands in sequence, you only have to enter your password once (in contrast tosu
). This reduces the urge to just open a shell or a new terminal with root privileges.¹ And for those not daring to do it themselves, there were install parties.
² But you can use a command like
sudo -i
orsudo su - root
to get a root shell after you logged in as a normal user.³ But you know of course that you should not simply copy&paste commands from the Internet, right?
It has been possible to disable root login through ssh for decades. The Ubuntu way of disabling the root account and making people sudo everything is nothing more than a gimmick. Just "sudo -s" and you have a root shell. Disable root login through ssh and leave it there.
Depending on the configuration,
sudo <game ending command>
will not necessarily work. Of course, if thesudoers
configuration reads "user ALL=(ALL) ALL",sudo
will offer no additional protection. You can however specify a list of privileged commands you need to run often, like installing new packages, and leave out dangerous commands likerm
.This way you can run all commands if you login as
root
, but since you will only need this occasionally, the risk of running<game ending command>
will be substantially reduced.That sudo allows you to allow only certain commands is not the only benefit of sudo over su.
In most shops, it is not even the most important benefit.
With sudo, you know who performed a particular command.
Now maybe this doesn't matter to you. For example, you might be the only user of your computer. If so, then sudo possibly isn't any better than su.
But plenty of hosts have more than one admin.
sudo then becomes very useful because if someone does the wrong thing, sudo lets you know who did it.
That allows you to understand why errors happened, and to educate people to prevent errors recurring, or if necessary, to remove the tools from someone.
As a bonus, when people know that their actions are recorded, they are often that little bit more careful.
Sudo isn't perfect.
If two people do "sudo sh" at the same time, then it can be hard to attribute commands to one or the other.
And a malicious admin can always remove or edit logs - although doing that cleanly isn't always as easy as people think, especially if you have centralised logging.
sudo doesn't necessarily stop a stupid or malicious action, for all the reasons identified in the original question.
But it does give you a much better chance of preventing a recurrence.