View Single Post
Old November 16th, 2008   #4
unutbu
Extra Roomy Joe
 
unutbu's Avatar
 
Join Date: Mar 2008
Beans: 4,715
Ubuntu 9.10 Karmic Koala
Re: Difference between sudo su and sudo -s

I did the following experiment:
Code:
applic@ion:~% sudo su
[sudo] password for applic:
root@ion:/home/applic# env > /tmp/sudo_su_env
root@ion:/home/applic# exit
exit
applic@ion:~% sudo -s
applic .bashrc read...
root@ion:~% env >/tmp/sudo_s
Here are the differences I found:
  • "sudo -s"
    HOME=/home/applic
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin
    reads $USER's ~/.bashrc
  • "sudo su"
    HOME=/root
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
    reads /etc/environment
    reads /root/.bashrc

Notice the difference in $HOME. Being root and having $HOME set to the normal user's home can cause problems. For example, if you run a graphical app, the normal user's ~/.Xauthority can get overwritten by root. This causes the normal user problems later on such as not being able to run certain graphical apps through cron.

Here is a summary:
Code:
				                     corrupted by user's 
		HOME=/root	uses root's PATH     env vars
sudo -i		Y		Y[2]                 N
sudo -s		N		Y[2]                 Y
sudo bash	N		Y[2]                 Y
sudo su		Y		N[1]                 Y


[1] PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
    probably set by /etc/environment
[2] PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin
The bottom line, is "sudo -i" is the proper command to run when you want a root shell that is untainted by the user's environment.
unutbu is offline   Reply With Quote