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.



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.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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