OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

su – Run A Shell With Substitute User And Group IDs

The su command is used to start a shell as another user. The command syntax looks like this:



su [-[l]] [user]

su [-[l]] [user]


If the “-l” option is included, the resulting shell session is a login shell for the specified user. This means that the user's environment is loaded and the working directory is changed to the user's home directory. This is usually what we want. If the user is not specified, the superuser is assumed. Notice that (strangely) the “-l” may be abbreviated “-”, which is how it is most often used. To start a shell for the superuser, we would do this:



[me@linuxbox ~]$ su - Password: [root@linuxbox ~]#

[me@linuxbox ~]$ su - Password: [root@linuxbox ~]#


After entering the command, we are prompted for the superuser's password. If it is suc- cessfully entered, a new shell prompt appears indicating that this shell has superuser priv- ileges (the trailing “#” rather than a “$”) and the current working directory is now the home directory for the superuser (normally /root). Once in the new shell, we can carry out commands as the superuser. When finished, enter “exit” to return to the previous shell:



[root@linuxbox ~]# exit

[me@linuxbox ~]$

[root@linuxbox ~]# exit

[me@linuxbox ~]$


It is also possible to execute a single command rather than starting a new interactive com- mand by using su this way:


su -c 'command'

su -c 'command'


Using this form, a single command line is passed to the new shell for execution. It is im- portant to enclose the command in quotes, as we do not want expansion to occur in our shell, but rather in the new shell:


[me@linuxbox ~]$ su -c 'ls -l /root/*'

Password:

-rw------- 1 root root 754 2007-08-11 03:19 /root/anaconda-ks.cfg


/root/Mail: total 0 [me@linuxbox ~]$

[me@linuxbox ~]$ su -c 'ls -l /root/*'

Password:

-rw------- 1 root root 754 2007-08-11 03:19 /root/anaconda-ks.cfg


/root/Mail: total 0 [me@linuxbox ~]$


Top OS Cloud Computing at OnWorks: