Become Another User, with His Environment Variables


su -l

The su command only works if you know the password of the user. No password, no transformation. If it does work, you switch to the shell that the user has specified in the /etc/passwd file: sh, tcsh, or bash, for instance. Most Linux users just use the default bash shell, so you probably won't see any differences there. Notice also in the previous example that you didn't change directories when you changed users. In essence, you've become gromit, but you're still using scott's environment variables. It's as if you found Superman's suit and put it on. You might look like Superman (yeah, right!), but you wouldn't have any of his powers.

The way to fix that is to use the -l option (or --login).

$ ls /home/scott/libby $ whoami scott $ su -l gromit Password: $ whoami gromit $ ls /home/gromit 


Things look mostly the same as the "Become Another User" example, but things are very different behind the scenes. The fact that you're now in gromit's home directory should demonstrate that something has changed. The -l option tells su to use a login shell, as though gromit actually logged in to the machine. You're now gromit in name, but you're using gromit's environment variables, and you're in gromit's home directory (where gromit would find himself when he first logged in to this machine). It's as though putting on Superman's skivvies also gave you the ability to actually leap tall buildings in a single bound!



Linux Phrasebook
Linux Phrasebook
ISBN: 0672328380
EAN: 2147483647
Year: 2007
Pages: 288

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net