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.

Is there any way to make sudo use the user's .bashrc file instead of the root one? I would like to use my own .bashrc while using sudo instead of the one of root or /etc/bashrc (I would have to force other users to use it).

The man page doesn't help much, but I'm sure there is a way to do so.

share|improve this question

3 Answers 3

up vote 2 down vote accepted

I don't know if that is a very good idea, although I can imagine it to be handy: Inside your sudo environment, you will have the SUDO_USER environment variable set to the name of the user who called sudo. That variable can be used in your /root/.bashrc to achieve what you want.

Beware, however, that security-wise this can be quite delicate. You should think twice if this cannot somehow be exploited by an untrusted user.

share|improve this answer
    
I would prefer avoiding to modify the /root/.bashrc, but I still can't find any other solution than yours. Ok, then, I will proceed this way. Thanks for your answer! –  philippe Jun 6 '12 at 7:38

I know this is old, but in case anyone googles this and finds it.... Configure your own .bashrc as you like (~/.bashrc) Then when you need to use sudo, do it like...

sudo -E command

-E tells the superuser to preserve the current environment

share|improve this answer
    
Preserving current environment is not really a good thing to do. There are dozens of things that may go wrong with specially crafted env variables. –  Deer Hunter Oct 17 at 5:51

Depending on what you want to do, it may be sufficient to set an environment variable in an alias for sudo. In my case, I was trying to set a different prompt for when I use sudo -s to get a root shell. The following alias in my .bashrc seems to work perfectly:

alias sudo="PS1='\[\e[0;31m\]\u@\h:\w\[\e[0m\]\\$ ' sudo "
share|improve this answer

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.