2

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.

3 Answers 3

5

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.

1
  • 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, 2012 at 7:38
0

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

1
  • 1
    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. Oct 17, 2014 at 5:51
0

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 "

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .