CHAPTER 13
To switch users at the command line, use the su command. Without any arguments, su will switch to the superuser account, also known as root. Alternatively, you can execute su root. Switching users will not change your current working directory or environment variables, unless you specify a hyphen following su. By specifying a hyphen, you simulate logging into the system as that user, and thus are placed into that user's home directory with that user's environment. For example, su - root.
su [username] Change to username or become the superuser.
Common su options:
su - A hyphen is used to provide an environment similar to what the user would expect had the user logged in directly.
su -c command Specify a command to be executed. If the command is more than one word in length, it needs to be quoted.
jason@linuxsvr:~$ export TEST=1 jason@linuxsvr:~$ su oracle Password: oracle@linuxsvr:/home/jason$ echo $TEST 1 oracle@linuxsvr:/home/jason$ pwd /home/jason oracle@linuxsvr:/home/jason$ exit exit jason@linuxsvr:~$ su - oracle Password: oracle@linuxsvr:~$ echo $TEST oracle@linuxsvr:~$ pwd /home/oracle oracle@linuxsvr:~$ exit jason@linuxsvr:~$ su -c 'echo $ORACLE_HOME' oracle Password: jason@linuxsvr:~$ su -c 'echo $ORACLE_HOME' - oracle Password: /u01/app/oracle/product/current jason@linuxsvr:~$ |
If you want to know what user you are working as, run the whoami command.
whoami Displays the effective username.
$ whoami jason $ su oracle Password: $ whoami oracle $ |
The sudo command allows you to run a command with the security privileges of another user. sudo will run the command as the superuser if no username is specified, hence the name "super user do." For example, sudo ls will run the ls command as the root user. sudo is commonly used to install, start, and stop applications that require superuser privileges.
sudo Execute a command as another user, typically the superuser.
One advantage of using sudo over the su command is that you do not need to know the password of the other user, usually the root user. This can eliminate issues that arise from using shared passwords and generic accounts. When you execute the sudo command, you are prompted for the current user's password. If the sudo configuration permits access, the command is executed. The sudo configuration is typically controlled by the system administrator and requires root access to change.
The su command is similar to sudo, but you should note these differences: su (switch user) asks for the new user's password, whereas sudo asks for the current user's password, or possibly no password at all. Su will change the current user of the shell, allowing multiple separate commands to be issued, whereas sudo runs a single command and is finished. For security reasons, sudo is generally preferable to su. The system administrator need not give the user the root password, and has full control over what commands work with sudo.
Here are the common ways to use the sudo command.
sudo -l List available commands that can be executed with sudo.
sudo command Run command as the superuser.
sudo -u root command Same as sudo command.
sudo -u user command Run command as user.
sudo su Switch to the superuser account.
sudo su - Switch to the superuser account with an environment you would expect to see had you logged in as that user.
sudo su - username Switch to the username account with an environment you would expect to see had you logged in as that username.
$ sudo -l User jason may run the following commands on this host: (root) NOPASSWD: /etc/init.d/apache2 (fred) NOPASSWD: /opt/fredsApp/bin/start (fred) NOPASSWD: /opt/fredsApp/bin/stop (root) /bin/su - oracle $ sudo /etc/init.d/apache2 start * Starting web server apache2 $ sudo -u fred /opt/fredsApp/bin/start Fred's app started as user fred. $ sudo su - oracle [sudo] password for jason: oracle@linuxsvr:~$ whoami oracle oracle@linuxsvr:~$ exit $ whoami jason $ |
The output of sudo -l displays what commands can be executed with sudo and under which account. In the previous example, sudo will not prompt for a password for the commands preceded with NOPASSWD. This type of configuration may be required to automate jobs via cron that require escalated privileges.