System administrators use the su
command to become the
But you can use it for lots more:
Become another user temporarily, without logging off your account.
Become another user without tying up another terminal port.
Switch between multiple users any time (on systems with job control).
Do a "quick login" to another user's account, especially when the system is busy.
When you type:
UNIX starts a
that runs as the user
After you use the cd
command to go to the user's home directory, you can
run commands as if you'd logged into that account (more or less... see below).
End the subshell and go back to the account where you typed
command or a
Or, on systems with
job control (12.8
you can stop the subshell temporarily and go back to the account where you
started the su
To do that, type
's shell has job control
(most shells do); otherwise, enter
at the shell prompt.
If the su
subshell doesn't have job control but your starting shell
command you run from the subshell will stop the command and the
You can use suspend
to start multiple su
sessions from the same
You can go back to your original login, from any of those sessions,
without losing your shell history, current directory, etc.
Because these shells run on the same
as your login shell, su
doesn't tie up other tty/pty ports like multiple logins or multiple
This is helpful on busy machines with lots of users.
On any UNIX system, you can type
to go back
to the original login.
But on systems with job control, you can su
to several other users and
jump back to your original login at any time.
Job control lets you suspend an su
and go back to the
place you left off without typing another su
Many shells have a suspend
command that lets you do that.
On other shells, you may be able to enter
(your job suspend
character) or make a command
to stop the current shell:
alias suspend='kill -STOP $$'
Here's a demo.
I'm logged in to the account jerry
on the computer wheeze
ed to the superuser, sarah
, and manuals
I'm using job control to switch users:
 Stopped su
 - Stopped su sarah
 + Stopped su manuals
...Do stuff as manuals
...Do stuff as root
I use that so much that I've made a single-letter
that does a suspend
|It's easier to jump between accounts if the shells'
have the username
in them, as shown above.
use the command whoami
to see which user you are.
Your system should have one or both; both GNU versions are on the
CD-ROM. Also, to see your original login name (the account where you started the
who am i
Some System V versions don't change the environment variable
to the right value for the account you su
That means a cd
command will take you to the home directory of
your original login, not the home directory of your su
Also, a C shell you start on the other account won't read your .cshrc
The best fix for that is a shell script named su
that sets the variable
The script is run by the
(tilde) operator (14.11
for finding the account's home directory.
Add this script to a directory before /bin
in your path (8.7
or make an alias or shell function that runs the script instead of the standard
# su - fix incorrect $HOME and USER with system 'su' command
foreach arg ($argv)
# find first non-option argument
if ("x$arg" !~ x-*) then
setenv HOME ~$arg
setenv USER $arg
exec /bin/su $argv:q
echo "$0 ERROR: can't find username."
Another workaround for that is an alias with the name of the account I'm
alias randi '(setenv HOME ~randi; setenv USER randi; su randi)'
There's another problem that can happen on any version of UNIX:
the account you su
doesn't have permission (22.2
to access the current directory where you ran the su
Then, you may get an error like
getwd: can't stat .
from the C shell
on the account you su
Or you may get no error but the su
fix for both problems is to cd
to a world-access directory like
before you run su
An alias can make that easy:
alias su '(cd /; \su \!*)'
You can also add the
command to this shell script if you want.
If the account you su
to runs the C shell (and you don't use the
option--see below), it will read the .cshrc
If that .cshrc
has hardcoded pathnames or commands that only the
other account can run, the commands might fail.
That can cause a variety of "fun" problems.
Try replacing hardcoded pathnames like
with paths that use account-specific
, and so on.
doesn't read a
C shell user's .login
file or a Bourne shell user's .profile
(see the section "Other su
Features" at the end of this chapter) solves
but you can't suspend an su -
shell (at least not on my systems).
Finally, because the su
command runs in a
environment variables (6.1
set in the account you su
from will be passed into
That can be good or bad.
For instance, your favorite
, Emacs, or whatever)
can be passed to the account you su
But that account might also set a variable that you want to use.
If you're wondering what's set after you su
list of shell variables, and either
to see environment variables.
If your system is busy, it can take time to run through all the commands
in the other user's .cshrc
command can pass arguments to the subshell it starts, though.
If the other account uses C shell, the -f
option tells it not to read
file (for example,
You won't get that account's .cshrc
setup, but you will start to work on
If logging in on your system takes a long time and you want to switch to
another account permanently, you can
That makes a weird situation where the
command will show you logged
on as your original account, but you'll be running as
command will tell you that).
Also, because the su
shell isn't a
login shell (2.8
won't work; you'll need to type
So, exec su
is a little tricky-but it's fast.
The command su -e
, which may also be called su -m
, switches to the other user's account but keeps
the environment you have now.
That's handy when you're having trouble with the other user's environment
or want to keep your own.
s do -m
by default, more or less.
to see what you get.)
The command su -
simulates a full login to the other account.
If the other account runs the Bourne shell, the .profile
will be read.
For the C shell, both .cshrc
will be read.
You can't suspend
a su -
When you log out though, you'll be back in your original account's shell.