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?
  • In your ssh solution, do you mean that you want to ssh into the potentially comprimsed box from another box? 1/ If yes, then, to follow your train of thought, you must make sure that your other box is not compromised before you ssh from it. 2/ If no, then you have the same problem.
    – asoundmove
    Mar 4, 2011 at 18:20
  • @asoundmove: There are 4 users with my ssh situation: root@hosta, root@hostb, stribika@hosta, stribika@hostb. Assuming an attacker can run arbitrary code as both stribikas (after all they are running flashplugin and whatnot) I still want safe root access. So both boxes can be partially compromised.
    – stribika
    Mar 4, 2011 at 18:36
  • 1
    "The safest way to get root privileges" is like asking for the safest way to use a chainsaw. It's inherently dangerous; what you want is risk mitigation. Sep 6, 2020 at 9:14

8 Answers 8


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.

  • I will accept this as the answer since it clears up a lot about what the Ubuntu developers were thinking when making sudo the default method. Remote logging also seems like a great idea and given that I'm using syslog-ng already it shouldn't be too hard to set up so all my computers log to all the others. I'm going to stick with my current practice of switching to text-mode and logging in from there for full root access. Delegating a subset of rights is certainly a good way to use sudo and one I have never considered.
    – stribika
    Mar 4, 2011 at 19:42
  • 10
    sudo is very similar to User Account Control in Windows Vista. Both are actually designed not to increase security, but actually to make it easier to decrease it in a controlled way. But because they make it easier to temporarily elevate your privileges in a low-friction manner, you don't need to run with elevated privileges for extended periods of time, thus increasing security. (This wasn't that big of a problem in Unix, but in Windows I remember running as Administrator for days on end, just because switching was so painful.) Mar 5, 2011 at 1:10
  • Password sniffing trojaned ssh daemons? If they can replace the ssh daemon, they already have root.
    – psusi
    Aug 15, 2011 at 20:01
  • @psusi: yes, on that system; in an environment where passwords are shared (by a directory service) between multiple systems, it's easy for a compromise to spread in this way. Even when it's a single system, it's a hassle to reprovision everyone's password after a reinstallation after the compromise has been detected.
    – mattdm
    Aug 15, 2011 at 20:05
  • 1
    The potential difficulties of changing all your company's root passwords is also mentioned as the final item in the Ops Report Card, which is well worth reading.
    – Wildcard
    Jun 1, 2016 at 1:04

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.

  • If they can find your password, then they can find the root password just as easily if not easier. Also you can use sysrq-k as a secure attention sequence.
    – psusi
    Aug 15, 2011 at 20:19
  • Linux has security attention keys, have a look at the kernel documentation Jun 12, 2016 at 23:10
  • @btshengsheng Linux can have a “secure attention key”, but since it can be configured out, you can't be confident that it's operating on any particular machine. It's also dependent on the keyboard layout, so it can be bypassed by reassigning one of the keys. It's called a “secure attention key”, but it doesn't quite fit the bill. Jun 12, 2016 at 23:16

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 flawed.

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 trickier.)

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).

  • 2
    For a system that's basically meant to be a firewall and not much more this is fine, but if you're intending to run, as a random example, mysql/postgresql/bind/powerdns/apache and whatnot in a production system, you start running into problems, much as they describe with X. Essentially they've traded a lot of usability for a degree of security; it's up to the systems administrator to decide whether it's a fair tradeoff. Personally, I'll stick to Debian. Sep 23, 2011 at 10:13

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 encouraged.

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).

  • 1
    I meant if the attacker can control my path he can make me run anything instead of the real /usr/bin/sudo. For example /tmp/sudo which prints Password: displays nothing when I type, sends what I typed to him, says the password is wrong, then execs the real sudo.
    – stribika
    Mar 4, 2011 at 17:47
  • Hmm, I guess in that case you could just run /usr/bin/sudo directly instead of relying on the shell to find it for you. But you're right, it's a problem I didn't think of.
    – Cedric
    Mar 4, 2011 at 17:52
  • 2
    @CedricStaub: As far as I can tell noone seems to think of this. I can give the full path but try this: alias /usr/bin/sudo="echo pwned" Besides that there are a ton of programs involved (X, xterm, the shell) any of which can steal the password. That's why I said there are far too many problems with switching safely.
    – stribika
    Mar 4, 2011 at 17:59
  • 1
    Did you tried it? I did: bash: alias: `/usr/bin/sudo': invalid alias name
    – maaartinus
    Mar 4, 2011 at 22:22
  • @maaartinus: Interesting. It works in ZSH.
    – stribika
    Mar 5, 2011 at 9:22

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

  • 1
    Sure thing but root-to-root or unprivileged-to-unpriviliged then switch to root? (I'm actually using 4096 bit keys just to be on the safe side. Larger keys are not supported everywhere.)
    – stribika
    Mar 4, 2011 at 18:23
  • @stribika Well, both. If the machine is compromised you still don't loose your key. That prevents spreading (biggest problem with passwords). Mar 4, 2011 at 18:40

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.

cheers F


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 .


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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .