Occasionally, you may need to log in with a userid other than your own or need to relog in with your own userid. For example, you might want to check configuration settings that you’ve changed before logging out to make sure that they work. Or, if you change your shell, you might want to check it before you log out (and you should do that, by the way).
You can use the su (substitute user) command to either log in as another user (Code Listing 3.15) or to start a new login shell.
[ejr@hobbes asr]$ ls Projects testing [ejr@hobbes asr]$ su asr Password: [asr@hobbes asr]$ ls Projects testing [asr@hobbes asr]$ su - ejr Password: [ejr@hobbes ejr]$ ls Mail editme script2.sed Projects fortunes.copy scriptextra.sed Xrootenv.0 fortunes1.txt sedtest above.htm fortunes2.txt sorted.address.temp address.book groups temp.htm address.temp history.txt tempsort axhome html.htm test bogus html.html test2 chmod.txt mail testing.gif clean manipulate testing.wp compression nsmail typescript [ejr@hobbes ejr]$ exit [asr@hobbes asr]$ exit [ejr@hobbes ejr]$ exit |
At the shell prompt, type su plus the userid of the user you’re logging in as. You’ll be prompted for a password just as though you were logging in to the system for the first time (Code Listing 3.15).
If you do not specify a username, the system will assume you mean the root user.
If you’re logged in as root to begin with, you won’t be prompted to give a password.
You will now be logged in as the new user and be able to work just as if you were that user, though you’ll be in the same directory with the same settings that you had before you issued the su command.
su - yourid
At the shell prompt, type su - yourid (of course, use your own userid or that of the user you want to change to). The addition of the hyphen (-) will force a new login shell and set all of the environment variables and defaults according to the settings for the user.
exit
Type exit at the shell prompt to leave the current shell and return to the previous one. If you use exit from the original login shell, you’ll log completely out of the Unix system.
✓ Tips
If you have root access and you ssh to the system to administer it, you should use su to provide a little extra security. Rather than log in directly as root and leave the remote possibility of having your password stolen (or sniffed) off your local system, log in as yourself, then use su (with no other information) to change to root.
If you su to another user with su user (no hyphen) and the new user doesn’t have read and execute permissions for the current directory, you will see shell error messages. You can disregard these. See Chapter 5 for more about read and execute permissions.