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:

2951 questions
1023
votes
7 answers

How can I add a user as a new sudoer using the command line?

After I add a user using adduser, I can't see it via System > Administration > Users and Groups unless I log out and then log in again. Is that normal? Also, can I set a newly added user as a sudoer or do I have to change that only after adding it?…
David B
  • 11,602
981
votes
20 answers

Error message "sudo: unable to resolve host (none)"

When I run sudo the terminal is stuck for a few seconds and then outputs an error message. My terminal looks like this: ubuntu@(none):~$ sudo true sudo: unable to resolve host (none) What can I do to solve it?
Kit Sunde
  • 11,286
564
votes
13 answers

Execute sudo without Password?

Inspired by this question.... I am the sole person using my system with 12.04. Every time I issue a sudo command; the system asks for the user password (which is good in its own way). However I was thinking; without activating the root account; how…
BhaveshDiwan
  • 11,506
  • 10
  • 35
  • 46
395
votes
10 answers

Run a shell script as another user that has no password

I would like to run a script from the main ubuntu shell as a different user that has no password. I have full sudo privileges, so I tried this: sudo su -c "Your command right here" -s /bin/sh otheruser Then I have to enter my password, but I am not…
rubo77
  • 34,024
  • 52
  • 172
  • 299
321
votes
2 answers

'sudo su -' vs 'sudo -i' vs 'sudo /bin/bash' - when does it matter which is used, or does it matter at all?

When I'm doing something that requires root be typed in dozens of times in a row, I prefer to switch my session to a root session. In the various tutorials and instructions I have used on the Internet, I see sudo su, sudo su -, sudo -i and sudo…
Paul
  • 4,606
320
votes
18 answers

How to modify an invalid '/etc/sudoers' file?

How do I edit an invalid sudoers file? It throws the below error and it's not allowing me to edit again to fix it. Here is what happens: $ sudo visudo >>> /etc/sudoers: syntax error near line 28 <<< sudo: parse error in /etc/sudoers near line…
user25165
297
votes
3 answers

How do I run specific sudo commands without a password?

On one particular machine I often need to run sudo commands every now and then. I am fine with entering password on sudo in most of the cases. However there are three sudo commands I want to run without entering password: sudo reboot sudo shutdown…
BhaveshDiwan
  • 11,506
  • 10
  • 35
  • 46
295
votes
6 answers

E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)

I'm trying to run this command in the terminal: sudo apt install software-properties-common This is the error message I get: E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable) E: Unable to acquire the…
GoodNite
  • 2,951
292
votes
7 answers

How to run sudo command with no password?

How does the ubuntu user on the AWS images for Ubuntu Server 12.04 have passwordless sudo for all commands when there is no configuration for it in /etc/sudoers? I'm using Ubuntu server 12.04 on Amazon. I want to add a new user that has the same…
aychedee
  • 8,347
273
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
  • 110,243
270
votes
11 answers

How do I start/stop mysql server?

I tried to find in some articles describing how to correctly start & stop mysql server. I found this link: How to start/stop MySql server on Ubuntu 8.04 | Abhi's Blogging World I ran this command: /etc/init.d/mysql start but I see this error…
user471011
  • 3,569
  • 8
  • 25
  • 19
253
votes
8 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,721
251
votes
8 answers

How do I add a user to the "sudo" group?

In /etc/sudoers I see this: # Allow members of group sudo to execute any command after they have # provided their password # (Note that later entries override this, so you might need to move # it further down) %sudo ALL=(ALL) ALL So how do I add a…
247
votes
8 answers

Aliases not available when using sudo

I was playing around with aliases today and I noticed that aliases don't seem to be available whilst using sudo: danny@kaon:~$ alias alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l='ls…
kemra102
  • 2,986
233
votes
5 answers

Why doesn't `sudo cd /var/named` work?

I want to cd into /var/named but it gives me a permission denied error, and when I want to use sudo to do this I am not permitted. What is the technical reason for this, and is it possible to do this some other way?
Hojat Taheri
  • 5,539
1
2 3
99 100