Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 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… 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
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.

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

                        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

You must log in to answer this question.

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