sections in this module City College of San Francisco - CS260A - Unix/Linux System Administration
Module: Virtual Machines
module list

Becoming root

Uid 0 has unlimited access on a Unix or linux system. Personally, I have never seen a Unix system where uid 0 is not named root, but the name should not matter. The celebrity of the system, root is who everyone wants to be. This everyone includes many people who would like to use the power of the system for their own purposes, not necessarily benevolent. Thus it is very important to treat access to this account with great respect, to limit its use whenever possible, and to protect it from being accessed surreptitiously. It is also important to provide as much monitoring of the use of root as possible.

By convention the prompt changes when you run as root. As root, the $ character in the normal prompt changes to a #. This, however, is configurable on any account by the owner of the account. Anyone can put a # in their prompt.

Logging in as root

Traditionally, users became root by logging on as root directly. This is a very bad idea for two reasons:

Suppose you are root, and, stupidly, are working in the root directory, /. (Believe it or not, this was traditionally the home directory of the root account, which, thankfully, has been changed on most modern systems.) You have created the file examples_of_root_mistakes.txt and need to remove it.  A normal user who is prone to these two poor behaviors might use the command

rm -r *.txt

That command is very similar to the unfortunate command

rm -r * txt

which could easily be mistyped since the key for a period is very close to the space bar.

This horrific example is extreme. However, each of us can remember when we have mistyped a command using either a wildcard operator or the -r option to rm or both with grievous results. Unlike a normal user, root is often working in a directory where such a mistake can cripple the system so completely that it will not function when it is restarted! Mistakes by root can also result in the loss of user data, which puts users in a very nasty mood, and seriously undermines the credibility of the administrator. It is thus important to limit the time spent operating in the shell as root to the minimum required.

In situations where you must become root and perform operations in the shell, it is much better to become root temporarily using the su command.

Using su to become root

The su command changes the effective user (euid) and group id (egid) (we will discuss this difference in the section on set-uid and set-gid) to another user and runs a shell. Commonly confused with superuser, the substitute user command su is normally invoked as 

su [ - ]  [user]

Here, if no alternate user name is given, it defaults to root.  su requests the password for the user. If the password is correct, it changes its euid and egid appropriately  and runs the user's default shell.  The - option, which is shorthand for -l, indicates that a login shell should be run. Otherwise, a non-login shell is run. Use of this option determines which initialization files are read by the new shell. Without the use of - , the new shell will get a copy of the current shell's environment variables (including PATH), umask, current directory, etc. If - is used, the shell initialization will reinitialize these parameters, since the /etc/profile file will be re-sourced as well as ~/.bash_profile. The two most important effects of this have to do with the PATH and current directory:

setting su [user] su - [user]
PATH current setting kept. If the new user is root, the system administration directories may not be in the PATH initialized to the default PATH of the new user. If the new user is root, the new PATH will contain the system administration directories.
current directory the current directory does not change changed to new user's home directory

Traditionally, directories containing system administration commands were not in the PATH of normal users, although current releases of linux have changed this practice. If the sysadmin directories are not in your PATH when you run the su command, you won't have them in your PATH when you become root! This is alleviated by resetting your PATH to root's PATH using su -

One underutilized feature of su is to pass the shell it creates a single command. This is done with the -c command option. For example, suppose you need to become root momentarily to edit a configuration file:

su - -c 'vi /etc/xxx.conf' 

The su command would ask for root's password, login as root, execute the vi command, and, when vi exits, would exit the root shell. (Note that if this is an attempt to restrict root access, this solution has a serious flaw, as we will see later.) Note the absolute path of the file to edit. This is necessary because the - option has been used, which changes directory to root's home directory. 

Another case is 

su -c 'chmod 755 configfile'

In this case, configfile is in the current directory. This is ok since the - option was not given, and the current directory of the new shell does not change when the new shell starts.

Although the su command is usually used without a command to run, resulting in the execution of a root shell, becoming root via su is highly recommended compared to logging in as root directly due to two reasons:

su -
< do what you have to as root >
exit

Protecting the root account

The root password is the most sensitive piece of information on your system. Protect it. Here are some do's and don'ts:

passwd
clear

In short

Avoid whenever possible giving anyone else the root password. Although they require some configuration, there are effective, manageable methods to provide someone with the privileges they need and avoid giving away the store. This is the subject of the future sections. Next, however, we will examine the effective use of permissions to avoid having to give away privileges.


Prev This page was made entirely with free software on linux:  
the Mozilla Project, Kompozer, and
Openoffice.org    
Next

Copyright 2013 Greg Boyd - All Rights Reserved.