Want to preserve bashrc and vimrc when switching to non-root users

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite
1












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










share|improve this question





















  • If you want to help your users, perhaps share your .vimrc and .bashrc settings with them?
    – wurtel
    Aug 13 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 at 16:37














up vote
0
down vote

favorite
1












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










share|improve this question





















  • If you want to help your users, perhaps share your .vimrc and .bashrc settings with them?
    – wurtel
    Aug 13 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 at 16:37












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





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










share|improve this question













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







bash sudo users su






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 13 at 15:45









jeremysprofile

21611




21611











  • If you want to help your users, perhaps share your .vimrc and .bashrc settings with them?
    – wurtel
    Aug 13 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 at 16:37
















  • If you want to help your users, perhaps share your .vimrc and .bashrc settings with them?
    – wurtel
    Aug 13 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 at 16:37















If you want to help your users, perhaps share your .vimrc and .bashrc settings with them?
– wurtel
Aug 13 at 16:08




If you want to help your users, perhaps share your .vimrc and .bashrc settings with them?
– wurtel
Aug 13 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 at 16:37




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 at 16:37















active

oldest

votes











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f462335%2fwant-to-preserve-bashrc-and-vimrc-when-switching-to-non-root-users%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f462335%2fwant-to-preserve-bashrc-and-vimrc-when-switching-to-non-root-users%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay