Which is the safest way to get root privileges: sudo, su or login?

I would like to have the root account in safety even if my unprivileged user is compromised.

On Ubuntu you can only use sudo for “security reasons” by default. However I am not sure it is any safer than just using login on a text-mode console. There are too many things that can go wrong if an attacker can run code as my normal user. For example adding aliases, adding stuff to my PATH, setting LD_PRELOAD and X11 keyloggers just to mention a few. The only advantage I can see is the timeout so I never forget to log out.

I have the same doubts about su but it doesn’t even have time limit. Some operations (especially IO redirection) are more convinient with su but security-wise this seems to be worse.

Login on a text-mode console seems to be the safest. Since it is started by init if an attacker can control PATH or LD_PRELOAD he is already root. The keypress events can’t be intercepted by programs running on X. I don’t know if programs running on X can intercept [ctrl]+[alt]+[f1] (and open a fullscreen window that looks like a console) or it is safe like [ctrl]+[alt]+[del] on Windows. Besides that the only problem I see is the lack of timeout.

So am I missing something? Why did the Ubuntu guys decide to only allow sudo? What can I do to improve the security of any of the methods?

What about SSH? Traditionally root can’t log in through SSH. But using the above logic wouldn’t this be the safest thing to do:

  • allow root through SSH
  • switch to text-mode
  • log in as root
  • ssh to the other machine
  • log in as root?
Asked By: stribika


You seem to be assuming that using sudo always preserves environment variables, but this is not always the case. Here is an excerpt from the sudo manpage:

There are two distinct ways to deal
with environment variables. By
default, the env_reset sudoers option
is enabled. This causes commands to
be executed with a minimal environment
containing TERM, PATH,
HOME, SHELL, LOGNAME, USER and USERNAME in addition to variables from
the invoking process permitted by the
env_check and env_keep sudoers
options. There is effectively a
whitelist for environment variables.

If, however, the env_reset option is
disabled in sudoers, any variables not
explicitly denied by the env_check and
env_delete options are inherited from
the invoking process. In this case,
env_check and env_delete behave like a
blacklist. Since it is not possible
to blacklist all potentially dangerous
environment variables, use of the
default env_reset behavior is

In all cases, environment variables
with a value beginning with () are
removed as they could be interpreted
as bash functions. The list of
environment variables that sudo allows
or denies is contained in the output
of sudo -V when run as root.

So if env_reset is enabled (the default), an attacker can’t override your PATH or other environment variables (unless you specifically add them to a whitelist of variables which should be preserved).

Answered By: Cedric

The safest approach is ssh login using (at least) 2048 long key (with password login disabled) using a physical device to store the key.

Answered By: Šimon Tóth

Security is always about making trade-offs. Just like the proverbial server which is in a safe, unplugged, at the bottom of the ocean, root would be most secure if there were no way to access it at all.

LD_PRELOAD and PATH attacks like those you describe assume that there is an attacker with access to your account already, or at least to your dotfiles. Sudo doesn’t protect against that very well at all — if they have your password, after all, no need to try tricking you for later… they can just use sudo now.

It’s important to consider what Sudo was designed for originally: delegation of specific commands (like those to manage printers) to “sub-administrators” (perhaps grad students in a lab) without giving away root completely. Using sudo to do everything is the most common use I see now, but it’s not necessarily the problem the program was meant to solve (hence the ridiculously complicated config file syntax).

But, sudo-for-unrestricted-root does address another security problem: manageability of root passwords. At many organizations, these tend to be passed around like candy, written on whiteboards, and left the same forever. That leaves a big vulnerability, since revoking or changing access becomes a big production number. Even keeping track of what machine has what password is a challenge — let alone tracking who knows which one.

Remember that most “cyber-crime” comes from within. With the root password situation described, it’s hard to track down who did what — something sudo with remote logging deals with pretty well.

On your home system, I think it’s really more a matter of the convenience of not having to remember two passwords. It’s probable that many people were simply setting them to be the same — or worse, setting them to be the same initially and then letting them get out of sync, leaving the root password to rot.

Using passwords at all for SSH is dangerous, since password-sniffing trojaned ssh daemons are put into place in something like 90% of the real-world system compromises I’ve seen. It’s much better to use SSH keys, and this can be a workable system for remote root access as well.

But the problem there is now you’ve moved from password management to key management, and ssh keys aren’t really very manageable. There’s no way of restricting copies, and if someone does make a copy, they have all the attempts they want to brute-force the passphrase. You can make policy saying that keys must be stored on removable devices and only mounted when needed, but there’s no way of enforcing that — and now you’ve introduced the possibility of a removable device getting lost or stolen.

The highest security is going to come through one-time keys or time/counter-based cryptographic tokens. These can be done in software, but tamper-resistant hardware is even better. In the open source world, there’s WiKiD, YubiKey, or LinOTP, and of course there’s also the proprietary heavyweight RSA SecurID. If you’re in a medium-to-large organization, or even a security-conscious small one, I highly recommend looking into one of these approaches for administrative access.

It’s probably overkill for home, though, where you don’t really have the management hassles — as long as you follow sensible security practices.

Answered By: mattdm

This is a very complex question. mattdm has already covered many points.

Between su and sudo, when you consider a single user, su is a little more secure in that an attacker who has found your password can’t gain root privileges immediately. But all it takes is for the attacker to find a local root hole (relatively uncommon) or install a trojan and wait for you to run su.

Sudo has advantages even over a console login when there are multiple users. For example, if a system is configured with remote tamper-proof logs, you can always find out who last ran sudo (or whose account was compromised), but you don’t know who typed the root password on the console.

I suspect Ubuntu’s decision was partly in the interest of simplicity (only one password to remember) and partly in the interest of security and ease of credential distribution on shared machines (business or family).

Linux doesn’t have a secure attention key or other secure user interface for authentication. As far as I know even OpenBSD doesn’t have any. If you’re that concerned about root access, you could disable root access from a running system altogether: if you want to be root, you would need to type something at the bootloader prompt. This is obviously not suitable for all use cases. (*BSD’s securelevel works like this: at a high securelevel, there are things you can’t do without rebooting, such as lowering the securelevel or accessing mounted raw devices directly.)

Restricting the ways one can become root is not always a gain for security. Remember the third member of the security triad: confidentiality, integrity, availability. Locking yourself out of your system can prevent you from responding to an incident.

The designers of the secured OpenWall GNU/*/Linux distro have also expressed critical opinions on su (for becoming root) and sudo. You might be interested in reading this thread:

…unfortunately both su and sudo are subtly but fundamentally

Apart from discussing the flaws of su and other things, Solar Designer also targets one specific reason to use su:

Yes, it used to be common sysadmin
wisdom to “su root” rather than login
as root. Those few who, when asked,
could actually come up with a valid
reason for this preference would refer
to the better accountability achieved
with this approach. Yes, this really
is a good reason in favor of this
approach. But it’s also the only one. …(read more)

In their distro, they have “completely got rid of SUID root programs in the default install” (i.e., including su; and they do not use capabilities for this):

For servers, I think people need to
reconsider and, in most cases,
disallow invocation of su and sudo by
the users. There’s no added security
from the old “login as non-root, then
su or sudo to root” sysadmin “wisdom”,
as compared to logging in as non-root
and as root directly (two separate
sessions). On the contrary, the
latter approach is the only correct
one, from a security standpoint:


(For accountability of multiple
sysadmins, the system needs to support
having multiple root-privileged
accounts, like Owl does.)

(For desktops with X, this gets

You also absolutely have to deal with…

BTW, they were to replace sulogin with msulogin to allow the setup with multiple root accounts: msulogin allows one to type in the user name also when going into the single user mode (and preserve the “accountability”) (this info comes from this discussion in Russian).

If the concern is that a compromised user account can be used to sniff the password used for sudo or su, then use a one-time passcode for sudo and su.

You can force the use of keys for remote users, but that might not pass muster for compliance purposes. It might be more effective to setup an SSH gateway box that requires two-factor auth, then permit key use from there. Here’s doc on such a setup: Secure your SSH deployment with WiKID two-factor authentication .

Answered By: nowen

I just want to add something a bit off topic.
(for the topic one check ‘/bin/su -‘ here after)

I think that the above “security” should also be linked to
the actual data we want to secure.

It will and it should be different if we want to secure:
my_data, my_company_data, my_company_network.

Usually, if I speak about security I also speak about “data security”
and backup. We can also add fault-tolerant systems and the like.

Given this, I think that security as a whole is an equilibrium between
the usability, the “data security” and the required effort to implement
a secure system.

Ubuntu’s target was, and mostly still is, the final user: Sudo is the default.

Fedora is the free version of RedHat which in turn is more servers oriented:
Sudo used to be disabled.

For the other distributions I have no direct information.

I am using right now, mostly, fedora. And as an old style user I never typed ‘su’.
But I can type “/bin/su -” in a very short time even if I am not exactly
a typist.
The PATH variable.. should not be a problem (I type the path).
Also the “-” (minus) in principle should remove my user environment variables and load only the root ones. i.e. avoiding some extra possible troubles.
Probably also the LS_PRELOAD.

For the rest I guess that @mattdm was pretty precise.

But lets put it in the correct box. Assume that a scripting smart kit
get access to my data. What the hell do you think is he going to do with it?
– Publish my pictures? my?
– Trying to find out my girlfriend name and tell her that I visit porno sites?

In the single user picture the two worst situations are:
– The kid delete all my data: for fun or by mistake
– The kid uses my machine to create a further attack to some other entity.
Or similar targets.

For the first case, I mentioned above, better putting efforts on a backup
than on network security. Yep, you are save. I mean an hardware crash is
not that different.

The second case is more subtle. But there are signals about these activities.
In any case, you can do the possible, but I would not configure my home PC
to be protected from a terroristic attacks!

I will skip the other scenarios.


Answered By: user5520

You could also take a look at privacyIDEA, which does not only provide you the possibility to use OTP for login to the machine (or for doing su/sudo) but it can also do OTP offline.

Furthermore it is capable of managing your SSH keys. I.e. if you have many machines and several root users, all SSH keys are stored centrally. Thus it is easy to “revoke”/delete an SSH key, if the key can not be trusted anymore.

Of course there are organizational tasks and workflows, to secure the system.

Answered By: cornelinux
Categories: Answers Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.