su and sudo behave in subtly different ways by design, as you can see in the examples below.
Look at the path and default directory when I am logged in under my own user name (fitzcarraldo):
- Code: Select all
fitzcarraldo@meshedgedx ~ $ echo $SHELL
/bin/bash
fitzcarraldo@meshedgedx ~ $ whoami
fitzcarraldo
fitzcarraldo@meshedgedx ~ $ echo $PATH
/usr/local/bin:/usr/bin:/bin:/opt/bin:/usr/x86_64-pc-linux-gnu/gcc-bin/4.4.2:/opt/blackdown-jdk-1.4.2.03/bin:/opt/blackdown-jdk-1.4.2.03/jre/bin:/usr/games/bin
fitzcarraldo@meshedgedx ~ $ cd ~
fitzcarraldo@meshedgedx ~ $ pwd
/home/fitzcarraldo
fitzcarraldo@meshedgedx ~ $
Now see what the path and default directory are when I use the sudo command:
- Code: Select all
fitzcarraldo@meshedgedx ~ $ echo $SHELL
/bin/bash
fitzcarraldo@meshedgedx ~ $ sudo -s whoami
Password: <---- Here I have to enter user fitzcarraldo's password
root
fitzcarraldo@meshedgedx ~ $ sudo -s echo $PATH
/usr/local/bin:/usr/bin:/bin:/opt/bin:/usr/x86_64-pc-linux-gnu/gcc-bin/4.4.2:/opt/blackdown-jdk-1.4.2.03/bin:/opt/blackdown-jdk-1.4.2.03/jre/bin:/usr/games/bin
fitzcarraldo@meshedgedx ~ $ sudo -s cd ~
fitzcarraldo@meshedgedx ~ $ pwd
/home/fitzcarraldo
fitzcarraldo@meshedgedx ~ $
N.B. The sudo man page tells you that the -s (shell) option runs the shell specified by the SHELL environment variable if it is set or the shell as specified in passwd. If a command is specified, it is passed to the shell for execution. Otherwise, an interactive shell is executed.
Now see what the path and default directory are when I am the root user:
- Code: Select all
fitzcarraldo@meshedgedx ~ $ su
Password: <---- Here I have to enter the root user's password
meshedgedx fitzcarraldo # echo $SHELL
/bin/bash
meshedgedx fitzcarraldo # whoami
root
meshedgedx fitzcarraldo # echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin
meshedgedx fitzcarraldo # cd ~
meshedgedx ~ # pwd
/root
meshedgedx ~ # exit
exit
fitzcarraldo@meshedgedx ~ $
As you can see above, the path and default directory are not the same if you use the sudo and su commands.