Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

There is a recent question regarding multiple sysadmins working as root, and sudo bash -l was referenced.

I searched for this on google and SE, but I don't understand the difference between sudo bash -l and sudo -i.

From the man pages for bash and sudo it seems that the difference may be that sudo bash -l runs ~/.bash_profile, ~/.bash_login, and ~/.profile, and ~/.bash_logout of the root user, but from testing myself it looks like it runs the normal user's .bashrc and not the root one. I may have misunderstood which user the ~ expression is referencing in the man pages. Clarification of the difference and usage scenarios would be appreciated.

share|improve this question
add comment

2 Answers

up vote 9 down vote accepted

They differ in that if the root user login shell specified in /etc/passwd is not bash, then the second command will get you a bash shell as root while the first command will use whatever the interactive shell the root user has.

share|improve this answer
    
Hi @johnshen64, would you be able to clarify the different usage scenarios? –  Rqomey May 25 '12 at 15:59
2  
people like the shell they are using, so if you want to be sure to use bash, then the second form should be used. the the second form also simulates an actual login (that is what -l means). of course, you could also just type bash when you find that the user (defaults to root) does not have bash as the interactive shell in using sudo -i. otherwise the difference is very minor. as the other answer shows, there are also some minor environment differences. in most cases the difference does not matter and the first one is shorter, as most common distros use bash. i rarely use the 2nd form. –  johnshen64 May 25 '12 at 16:09
add comment

Also to add to @johnshen64 answer I noticed that the sudo -i invocation updates the $USERNAME env variable to root and cd ~ to the user home like so;

$ sudo -i
[root@workstation001 ~]# id
uid=0(root) gid=0(root) groups=0(root)
[root@workstation001 ~]# echo $USERNAME
root
[root@workstation001 userXXX]# echo $PWD
/root

However for sudo bash -l $USERNAME is unmolested, and the working directory is not changed. (though this is not consistent across other users, see comments...)

$ sudo bash -l
[root@workstation001 userXXX]# id
uid=0(root) gid=0(root) groups=0(root)
[root@workstation001 userXXX]# echo $USERNAME
userXXX
[root@workstation001 userXXX]# echo $PWD
/home/userXXX

So presumably you are not getting exactly the same set of ~/.bashrc, or not in the same order etc.

(my box might have been messed up a little by my .bashrc tinkerings, so YMMV)

share|improve this answer
    
Thanks @Tom H: echo $USERNAME returns root both times for me (on my ubuntu desktop and centos 5.2). PWD is as you report however. –  Rqomey May 25 '12 at 12:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.