0

I'm on a multi-user system. I have sudo b/c I'm responsible I guess. Sometimes, I want to switch to another user to help them do something, but I have a tricked-out bashrc/vimrc and I'd like to keep those.

For root, it's pretty easy. In my .bashrc:

alias sudo='sudo '
alias -- -i='bash --rcfile $HOME/.bashrc'
export HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"

Basically, this means sudo -i makes me root, sources my .bashrc, and keeps my $HOME as /home/jeremy instead of /root, meaning vim looks in the proper place for colors and rcfiles and things.

I want to do something similar with becoming other users. The ideal:

  • source my .bashrc; get vim to use my .vim/ stuff like vimrc, colors/, etc.

  • doesn't hardcode paths -- on some systems I'm jeremy; on others I'm jdr, etc.

  • requires minimal extra typing for all of this to work by aliasing or function-ing the crap out of things.

For root, I was able to alias -i to another command entirely that did what I wanted. man bash doesn't list any options to do things as another user. man sudo does, but then I wouldn't have any options to source a .bashrc. su seemed promising, as man su says that su -m preserves environment and doesn't set USER or HOME, but I'd have to sudo su -m which sets my USER to root. I can't add in commands using && or something because I would need the commands to run in the new shell instead of after that shell exits. In short, none of the commands I know of can do what I want.

The closest I was able to come was sudo -Esu user, which makes me user and keeps any vars I've exported. I could put something like export x=". $HOME/.bashrc" in my .bashrc and then eval $x when I become user, but that gives permissions errors, and setting +x on .bashrc isn't sufficient because my home directory's default permissions prevent access by other users and I'm not sure I'd want to change that unless there's no other option. Not all users I'm becoming have privileges so I can't just sudo my way in.

TL;DR: become another user while retaining my current user's aliases, functions, PS1, PROMPT_COMMAND, and vim configuration. Bash 4.2

  • If you want to help your users, perhaps share your .vimrc and .bashrc settings with them? – wurtel Aug 13 '18 at 16:08
  • Meaning just shove a copy to their home directories? My settings are in flux as I learn and experiment more, and I don't want to have to remember to update it as I change it, or push it to new users as they are added. It feels like there should be a better option than making my .bashrc and .vimrc symlinks to files that everyone has at least execute access to. – jeremysprofile Aug 13 '18 at 16:37

Your Answer

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

Browse other questions tagged or ask your own question.