We’re rewarding the question askers & reputations are being recalculated! Read more.

Questions related to the sudo command that allows users to run programs with the privileges of a different user (normally the root user).

sudo is the recommended tool to elevate privileges in order to carry out administrative tasks in Ubuntu. Its usage is more granulated and configurable than its counterpart su.

This command allows the issuing user to:

  • Run a specific command as the superuser or another user.
  • Start an interactive shell as the superuser or another user.
  • Elevate privileges of certain commands.

The sudo binary is included in the sudo package, which also includes the tools visudo and sudoedit. Using visudo is the recommended way to edit the /etc/sudoers file which configures the way sudo behaves, while sudoedit opens the default editor with super user privileges.

The sudo command can be configured using the /etc/sudoers file. This follows a specific syntax; using visudo is recommended to prevent errors. Using visudo requires elevated privileges, so the normal user would issue sudo visudo.

It is advisable to always use the minimum privileges required for any action. sudo should only be used where necessary.

From the Ubuntu help wiki:

Benefits of using sudo

There are a number of benefits to Ubuntu leaving root logins disabled by default, including:

  1. The installer has fewer questions to ask.
  2. Users don't have to remember an extra password for occasional use (i.e. the root password). If they did, they'd be likely to forget it (or record it unsafely, allowing anyone to easily crack into their system).
  3. It avoids the "I can do anything" interactive login by default. You will be prompted for a password before major changes can happen, which should make you think about the consequences of what you are doing.
  4. sudo adds a log entry of the command(s) run (in /var/log/auth.log). If you mess up, you can go back and see what commands were run.
  5. On a server, every cracker trying to brute-force their way in will know it has an account named root and will try that first. What they don't know is what the usernames of your other users are. Since the root account password is locked, this attack becomes essentially meaningless, since there is no password to crack or guess in the first place.
  6. Allows easy transfer for admin rights by adding and removing users from groups. When you use a single root password, the only way to de-authorize users is to change the root password.
  7. sudo can be set up with a much more fine-grained security policy.
  8. The root account password does not need to be shared with everybody who needs to perform some type of administrative task(s) on the system (see the previous bullet).
  9. The authentication automatically expires after a short time (which can be set to as little as desired or 0); so if you walk away from the terminal after running commands as root using sudo, you will not be leaving a root terminal open indefinitely.

Downsides of using sudo

Although for desktops the benefits of using sudo are great, there are possible issues which need to be noted:

  1. Redirecting the output of commands run with sudo requires a different approach. For instance consider sudo ls > /root/somefile will not work since it is the shell that tries to write to that file. You can use ls | sudo tee -a /root/somefile to append, or ls | sudo tee /root/somefile to overwrite contents. You could also pass the whole command to a shell process run under sudo to have the file written to with root permissions, such as sudo sh -c "ls > /root/somefile".
  2. In a lot of office environments the ONLY local user on a system is root. All other users are imported using NSS techniques such as nss-ldap. To setup a workstation, or fix it, in the case of a network failure where nss-ldap is broken, root is required. This tends to leave the system unusable unless cracked. An extra local user, or an enabled root password is needed here. The local user account should have its $HOME on a local disk, not on NFS (or any other networked filesystem), and a .profile/.bashrc that doesn't reference any files on NFS mounts. This is usually the case for root, but if adding a non-root rescue account, you will have to take these precautions manually. However the advantage of using a local user with sudo is that commands can be easily tracked, as mentioned in the benefits above.

External resources:

Popular questions:

history | excerpt history