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.

On a stock CentOS 6.1 installation, if I start in my user shell (tcsh) with all my usual command aliases and do what I normally do (in other versions of Linux, and Mac OS X) to get a root shell,

sudo tcsh

my command aliases are not preserved. This is not the behavior I see with sudo on other operating systems, including older versions of RHEL (from which CentOS is derived). I've tried different variations in CentOS 6.1, such as

sudo -i tcsh
sudo tcsh -m
sudo -i tcsh -m

which by my reading of the sudo and tcsh man pages, should make it behave more like a login shell and presumably do things like interpret my .cshrc file, but nothing has worked. My default shell is /bin/tcsh in /etc/passwd; I've tried switching root's entry in /etc/passwd to also use /bin/tcsh (the default was /bin/bash), but it didn't make any difference.

I guess this behavior could be controlled by something in /etc/sudoers, but my /etc/sudoers is the stock file installed by CentOS 6.1, and comparing it to (e.g.) Mac OS X 10.6's /etc/sudoers file doesn't immediately reveal an obvious setting that would control this particular behavior.

I'm stumped at this point and would appreciate help.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

In tcsh, aliases are local to the shell instance that they're defined in. You have to arrange for them to be redefined in each subshell. When tcsh starts it reads commands from $HOME/.tcshrc (or $HOME/.cshrc if that doesn't exist); presumably you have your aliases set here.

The command sudo -i "simulates an initial login" by setting $HOME (and various other environment variables) to that of the target user.

Further, recent versions of sudo remove $HOME from the environment if the option env_reset is present in /etc/sudoers.

So, to make the root shell source the .tcshrc of the user invoking sudo, use sudo -s, and add the following line to /etc/sudoers:

Defaults env_keep += HOME

Alternatively, you could add the following to /root/.tcshrc:

if ( $?SUDO_USER ) then
    source ~$SUDO_USER/.tcshrc
endif
share|improve this answer
    
Sorry to take so long to get back to this. I tested the first solution (env_keep) but it did not seem to work on my CentOS system. The second solution did, however. Thanks for this. –  mhucka Jan 22 at 22:36

When you sudo, you are entering a root shell, rather than your own. You could add your aliases to /root/.cshrc in order for the aliases to be available when you sudo.

share|improve this answer
    
Yes, I know, but sudo on other systems do cause aliases (and perhaps other aspects of the user environment) to be read. I edited my question to clarify this point; sorry I wasn't clear enough about the key issue. –  mhucka Mar 3 '12 at 15:43

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.