Questions tagged [sudo]

The sudo command allows users to run programs with the privileges of a different user (normally the root user). Use this tag for questions related to configuring and using the sudo command.

The sudo command (an acronym/abbreviation of "superuser do" or "substitue user do") 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:

2895 questions
269
votes
6 answers

Where are sudo's insults stored?

For those who like humour, sudo can be configured to print a random more or less insulting or funny phrase instead of the neutral Sorry, try again. by adding the line below to /etc/sudoers (using the command sudo visudo, not editing…
Byte Commander
  • 107,489
236
votes
7 answers

Sudoers file, enable NOPASSWD for user, all commands

Preface This is a fairly complex question related to the sudoers file and the sudo command in general. NOTE: I have made these changes on a dedicated machine running Ubuntu Desktop 13.04, that I use purely for learning purposes. I understand it's a…
nicholsonjf
  • 2,551
  • 2
  • 15
  • 8
123
votes
6 answers

Why is sudoers NOPASSWD option not working?

I have a NOPASSWD line in /etc/sudoers (edited with visudo) gatoatigrado ALL=(ALL) NOPASSWD: /bin/set-slow-cpufreq However, the output is, gatoatigrado@coral:~> sudo -n /bin/set-slow-cpufreq sudo: sorry, a password is required to run sudo This…
gatoatigrado
  • 1,803
117
votes
4 answers

Remove sudo privileges from a user (without deleting the user)

If you give a user sudo privileges how can I remove the sudo privileges and make the user become just a regular user? I used: sudo adduser username sudo Now I've changed my mind.
Joe C
  • 1,335
  • 2
  • 10
  • 10
82
votes
2 answers

Who are incidents really reported to, and how can a sudo user access the reports?

When my non-sudo account tries to run a sudo command: nonsudo@Hairy14:$ sudo hello An incident is reported: [sudo] password for nonsudo: nonsudo is not in the sudoers file. This incident will be reported. I'm guessing it's not really Father…
Tim
  • 32,861
  • 27
  • 118
  • 178
56
votes
4 answers

How to cancel the currently-applied remaining sudo time-out?

When I've just run sudo, and don't want it to remain active for the normal duration of its time-out, how can I cancel that remaining active time-out? sudo -k kills it for the current terminal session, but if a process is currently running in…
Peter.O
  • 24,681
54
votes
6 answers

How can I fix broken sudo - sudo: parse error in /etc/sudoers near line 23?

I am getting this error: sudo: parse error in /etc/sudoers near line 23 sudo: no valid sudoers sources found, quitting sudo: unable to initialize policy plugin I was trying to disable password authentication so I don't have to type password every…
54
votes
3 answers

How do I sudo without having to enter my password?

I'm trying to setup sudo without no password but still I get prompted for password, I'm using ubuntu, here is the relevant line from the sudoers configuration file : gandalf ALL=(ALL:ALL) NOPASSWD: ALL
45
votes
1 answer

The command could not be located because '/usr/bin' is not included in the PATH environment variable

Can someone tell me why I get this error when I try to add a repository and what if anything I can do about it? When I try to add a repository using: sudo add-apt-repository ppa:upubuntu-com/tor I get the following error: Command 'sudo' is…
35
votes
3 answers

How to solve "sudo: /etc/sudoers.d is world writable"

How can I fix the following error with sudo? /etc$ sudo sudo: /etc/sudoers.d is world writable usage: sudo [-D level] -h | -K | -k | -V usage: sudo -v [-AknS] [-D level] [-g groupname|#gid] [-p prompt] [-u user name|#uid] usage: sudo…
kismat
  • 351
  • 1
  • 3
  • 3
32
votes
3 answers

How often is the password asked for sudo commands? Where can I set it up?

I execute quite a lot of sudo commands. I noticed that if I execute one and then another one within few minutes, the 2nd time I don't get the message: [sudo] password for my_username: but I do get it when there is more time between the execution…
fedorqui
  • 10,069
31
votes
2 answers

sudoers - simple explanation requested

Everytime I want to be able to run something that requires me to be a sudoer too many times, I need to google for the formatting of /etc/sudoers to remind me again what exactly is the proper way to write it. Now I see different writing styles in my…
Redsandro
  • 3,674
29
votes
3 answers

Enable passwordless sudo as a specific user

I have two users on my system: itsadok, my main user, and elasticsearch, a user with a different ulimit for running ElasticSearch. I would like to be able to run stuff as the secondary user without being prompted for password every time. I added…
itsadok
  • 2,904
26
votes
6 answers

sudo error, is mode 0777, should be 0440

I am getting sudo errors, how do I fix this error? sudo: /etc/sudoers is mode 0777, should be 0440 sudo: no valid sudoers sources found, quitting
user20698
  • 301
25
votes
5 answers

How to just gain root permission without running anything?

When I run a command with sudo like the following, subsequent sudo commands will not ask for a password anymore. sudo ls But this still runs ls. If I don't want to run any command at the beginning, but just stop subsequent sudo commands from asking…
user1424739
  • 991
  • 4
  • 12
  • 20
1
2 3
14 15