Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

The Background

I am working in a CentOS environment with numerous CentOS machines. Currently there are multiple developers that each have their own login/home directory and then for various admin tasks we all share a single super user account.

The problem

I have a number of aliases, variables, functions, and settings that exist in my personal login's .bash_profile. None of these are available in the shared super user's .bash_profile. My current work around is that everytime I sudo in as the super user and I re-execute my .bash_profile from my personal user's home directory. I am not allowed to edit the init stuff for the super user

The Question

Is there any way I can automate my sudo sequence such that it will execute my personal .bash_profile after I've executed sudo without requiring me to edit the super user's bash init stuff?

share|improve this question

migrated from stackoverflow.com May 4 '11 at 19:40

This question came from our site for professional and enthusiast programmers.

    
How is this off topic? Its about as objective and related to software development as anything gets. –  nsfyn55 May 4 '11 at 18:15
    
Votes to close are alway subjective but this seems like a pretty general environment setup question. It may seem development related to you because that is your immediate objective but it could just as easily be asked by someone looking to setup any admin tasks. –  Duck May 4 '11 at 18:39
    
ummm.... "seem development related" . How is it different than say this post stackoverflow.com/questions/853451/… where the user is asking a question about "find" like I am asking a question about "sudo" –  nsfyn55 May 4 '11 at 18:54
    
As I recall that question preceded the existence of superuser, serverfault, etc. –  Duck May 4 '11 at 18:57

3 Answers 3

up vote 1 down vote accepted

You can keep aliases by this neat little trick:

alias sudo='sudo '

Which allows for the next argument to be checked for alias expansion (from your current profile!)

From man alias:

A trailing space in value causes the next word to be checked for alias substitution when the alias is expanded.

YMMV for the functions, etc.

The env_reset in /etc/sudoers is the other workaround I've found.

http://blog.edwards-research.com/2010/07/keeping-aliases-with-sudo-sort-of/

share|improve this answer

sudo has an option to retain the environment of the person invoking it albeit with some caveats. From the manual page

-E The -E (preserve environment) option will override the env_reset option in sudoers(5)). It is only available when either the matching command has the SETENV tag or the setenv option is set in sudoers(5).

share|improve this answer
    
No dice - user is not allowed to execute '/bin/su - top -E' as root on <host> –  nsfyn55 May 4 '11 at 18:01
    
It should be sudo -E top, not su - top -E. –  Paŭlo Ebermann May 4 '11 at 18:17
    
Not sure if its a version difference but the man page for sudo includes no -E switch in the environment I'm using –  nsfyn55 May 4 '11 at 18:45
    
I think you're out of luck then. –  Noufal Ibrahim May 4 '11 at 18:48

edit /etc/sudoers (visudo)

create or modify the Defaults env_keep variable

Defaults    env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR \
                        LS_COLORS MAIL PS1 PS2 QTDIR"

This will keep the specified environment variables in your environment after the sudo has executed.

Or comment out Defaults env_reset, but note that this is dangerous!

#Defaults    env_reset

This keeps the entire environemt from the shell before the sudo executed.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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