by DAN CALLOWAY (Contributors of the Ubuntu Documentation Wiki)
Published under the Creative Commons Attribution Share-Alike 3.0 License
WEAVERVILLE, NC – 18 April 2010
Background Information
In Linux (and Unix in general), there is a superuser named root. The Windows equivalent of root is Administrators group. The superuser can do anything and everything, and thus doing daily work as the superuser can be dangerous. You could type a command incorrectly and destroy the system. Ideally, you run as a user that has only the privileges needed for the task at hand. In some cases, this is necessarily root, but most of the time it is a regular user.
By default, the root account password is locked in Ubuntu. This means that you cannot login as root directly or use the su command to become the root user. However, since the root account physically exists it is still possible to run programs with root-level privileges. This is where sudo comes in – it allows authorized users (normally “Administrative” users) to run certain programs as root without having to know the root password.
This means that in the terminal you should use sudo for commands that require root privileges; simply prepend sudo to all the commands you would normally run as root. For more extensive usage examples, please see below. Similarly, when you run GUI programs that require root privileges (e.g. the network configuration applet), use graphical sudo and you will also be prompted for a password (more below). Just remember, when sudo asks for a password, it needs YOUR USER password, and not the root account password.
Advantages and Disadvantages
Benefits of using sudo
Some benefits of leaving root logins disabled by default include the following:
- The Ubuntu installer has fewer questions to ask.
- Users don’t have to remember an extra password (i.e. the root password), which they are likely to forget.
- It avoids the “I can do anything” interactive login by default (e.g. the tendency by users to login as an “Administrator” user in Microsoft Windows systems), 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.
- sudo adds a log entry of the command(s) run (in /var/log/auth.log). If you mess up, you can always go back and see what commands were run. It is also nice for auditing.
- Every cracker trying to brute-force their way into your box 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.
- Allows easy transfer for admin rights, in a short term or long term period, by adding and removing users from groups, while not compromising the root account.
- sudo can be setup with a much more fine-grained security policy.
- 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).
- 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:
- Redirecting the output of commands run with sudo requires a different approach. For instance considersudo ls > /root/somefile will not work since it is the shell that tries to write to that file. You can usels | 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".
- 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.
- Alternatively, a sysadmin type account can be implemented as a local user on all systems, and granted proper sudo privileges. As explained in the benefits section above, commands can be easily tracked and audited.
Usage
- When using sudo, password is stored by default for 15 minutes. After that time, you will need to enter your password again.
- Your password will not be shown on the screen as you type it, not even as a row of stars (******). It is being entered with each keystroke!
sudo
To use sudo on the command line, preface the command with sudo, as below: Example #1
sudo chown bob:bob /home/bob/*
Example #2
sudo /etc/init.d/networking restart
To repeat the last command entered, except with sudo prepended to it, run:
sudo !!
Graphical sudo
You should never use normal sudo to start graphical applications as root. You should use gksudo (kdesudo onKubuntu) to run such programs. gksudo sets HOME=~root, and copies .Xauthority to a tmp directory. This prevents files in your home directory becoming owned by root. (AFAICT, this is all that’s special about the environment of the started process with gksudo vs. sudo).
Examples:
gksudo gedit /etc/fstab
or
kdesudo kate /etc/X11/xorg.conf
- To run the graphical configuration utilities, simply launch the application via the Administration menu.
- gksudo and kdesudo simply link to the commands gksu and kdesu
Drag & Drop sudo
This is a trick from this thread on the Ubuntu Forums.
Create a launcher with the following command:
gksudo "gnome-open %u"
When you drag and drop any file on this launcher (it’s useful to put it on the desktop or on a panel), it will be opened as root with its own associated application. This is helpful especially when you’re editing config files owned by root, since they will be opened as read only by default with gedit, etc.
Users
Allowing other users to run sudo
To add a new user to sudo, open the Users and Groups tool from System->Administration menu. Then click on the user and then on properties. Choose the User Privileges tab. In the tab, find Administer the system and check that.
- In Hardy Heron and newer, you must first Unlock, then you can select a user from the list and hitProperties. Choose the User Privileges tab and check Administer the system.
In the terminal this would be: sudo adduser <username> admin, where you replace <username> with the name of the user (without the <>).
Logging in as another user
Please don’t use this to become root, see further down in the page for more information about that.
sudo -i -u <username>
For example to become the user amanda for tape management purposes.
sudo -i -u amanda
The password being asked for is your own, not amanda’s.
root account
Enabling the root account
Enabling the root account is rarely necessary. Almost everything you need to do as administrator of an Ubuntu system can be done via sudo or gksudo. If you really need a persistent root login, the best alternative is to simulate a root login shell using the following command…
sudo -i
To enable the root account (i.e. set a password) use:
sudo passwd rootUse at your own risk!Logging in to X as root may cause very serious trouble. If you believe you need a root account to perform a certain action, please consult the official support channels first, to make sure there is not a better alternative.Re-disabling your root account
If for some reason you have enabled your root account and wish to disable it again, use the following command in terminal...sudo usermod -p '!' root




Recent Comments