22.22 The su Command Isn't Just for the SuperuserSystem administrators use the su command to become the superuser (1.24 ) . But you can use it for lots more:
When you type: youraccount% UNIX starts a
subshell (38.4
)
that runs as the user 22.22.1 Ending or SuspendingEnd the subshell and go back to the account where you typed 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
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 tty (3.8 ) 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
Here's a demo. I'm logged in to the account jerry on the computer wheeze . I've su ed to the superuser, sarah , and manuals accounts, too. I'm using job control to switch users: jerry@wheeze% I use that so much that I've made a single-letter alias (10.2 ) named z that does a suspend . 22.22.2 Who Are You Now?
22.22.3 Problems You Might HaveSome System V versions don't change the environment variable
HOME
(LOGDIR
) (14.11
)
to 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 su
ed 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
C shell
Another workaround for that is an alias with the name of the account I'm su ing to:
There's another problem that can happen on any version of UNIX:
the account you su
to
doesn't have permission (22.2
)
to access the current directory where you ran the su
command.
Then, you may get an error like
You can also add the 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
Plain Finally, because the su
command runs in a
subshell (38.4
)
,
environment variables (6.1
)
set in the account you su
from will be passed into
the subshell.
That can be good or bad.
For instance, your favorite
EDITOR (6.3
)
(vi
, Emacs, or whatever)
can be passed to the account you su
to.
But that account might also set a variable that you want to use.
If you're wondering what's set after you su
, type 22.22.4 Quick Changes to Other AccountsIf 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, If logging in on your system takes a long time and you want to switch to another account permanently, you can exec (45.7 ) the su command: % That makes a weird situation where the
who
(51.4
)
command will show you logged
on as your original account, but you'll be running as 22.22.5 Other su FeaturesThe 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 su s do -m by default, more or less. Use env or printenv (6.1 ) 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 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. - | ||||||||||
|