How to set custom variables like bash prompt,aliases,etc after becoming root from normal users using sudo -i

Login via Putty (key based) with "nir" username -> Become root using sudo -i

After root i can see echo $SUDO_USER gives my username "nir"

I don't want this changes to be permanent so not thinking of /root/.bashrc

One way which i think is of creating a script and putting in my home directory and putting entry in /root/.bashrc (working on this)

Any comments.


1 Answer 1


Create a script at /root/.bash_nir with the custom settings/commands such as:

export SOME_VAR=user_nir

then add to /root/.bashrc the following:

[[ -n "$SUDO_USER" ]] && [[ -f "$HOME/.bash_$SUDO_USER" ]] && source "$HOME/.bash_$SUDO_USER"

Now when you sudo -i as the nir user bash will execute the extra script, but not if you log into root via other means. This will also work for any other user simply by creating a script for them as /root/.bash_$USERNAME.

You must log in to answer this question.

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