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).
When you type:
UNIX starts a
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
su with the
command or a
Or, on systems with
whoever's shell has job control
(most shells do); otherwise, enter
at the shell prompt.
NOTE: If the su subshell doesn't have job control but your starting shell does, entering CTRL-z to any command you run from the subshell will stop the command and the subshell.
You can use suspend to start multiple su sessions from the same shell. 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 windows can. This is helpful on busy machines with lots of users.
On any UNIX system, you can type
exit (or use
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 (and password).
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. I've sued to the superuser, sarah, and manuals accounts, too. I'm using job control to switch users:
jobs Stopped su  - Stopped su sarah  + Stopped su manuals jerry@wheeze%
fgsu manuals ...Do stuff as manuals... manuals@wheeze%
fg %1su wheeze# ...Do stuff as root... wheeze#
I use that so much that I've made a single-letternamed z 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 or id 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
Some System V versions don't change the environment variableto the right value for the account you su to. That means a cd command will take you to the home directory of your original login, not the home directory of your sued account. Also, a C shell you start on the other account won't read your .cshrc file. The best fix for that is a shell script named su that sets the variable for you. The script is run by the because csh has the for finding the account's home directory. or make an alias or shell function that runs the script instead of the standard su.
#!/bin/csh -f # 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 endif end echo "$0 ERROR: can't find username." exit 1
Another workaround for that is an alias with the name of the account I'm suing to:
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 to
getwd: can't stat . from the C shell
on the account you su to.
Or you may get no error but the su will fail.
fix for both problems is to cd to a world-access directory like
/ or /tmp before you run su.
An alias can make that easy:
alias su '(cd /; \su \!*)'
You can also add the
cd / command to this shell script if you want.
If the account you su to runs the C shell (and you don't use the
-f option--see below), it will read the .cshrc file.
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
/bin with paths that use account-specific
~/bin, and so on.
whoever doesn't read a
C shell user's .login file or a Bourne shell user's .profile.
whoever (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
set for a
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 file.
The su 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
the .cshrc 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 canthe su command:
That makes a weird situation where the
(the whoami or id command will tell you that).
Also, because the su shell isn't a
the logout command
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 or su -p, 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. (Some sus do -m by default, more or less. Useto 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 and .login will be read. You can't suspend a su - subshell. When you log out though, you'll be back in your original account's shell.