6

On my Fedora 14 developer machine I can add a sharedAliases.sh file to /etc/profile.d -- both my user and root user then have access to the shared aliases.

Switch over to remote CentOS 5.7 machine and what appears to be the exact same config, no dice, root user does not have access to the shared aliases.

This may be due to the fact that I SSH into the CentOS box, not sure. At any rate, the lame workaround has been to copy shared aliases into root user's .bashrc as it's the only way I've been able to get desired prefs into root (yes, I know I should sudo, but have been rolling with su for years).

Ideas appreciated, would prefer to not have to duplicate aliases if possible.

Wesley
  • 33,060

1 Answers1

12

Must have to do with diff between su-ing over ssh vs when logged directly into machine

I suspect that you execute the su command without dash (-), and if so, it will invoke an interactive non-login shell. Combine with you have only following in /root/.bashrc:

# Test for an interactive shell.  There is no need to set anything
# past this point for scp and rcp, and it's important to refrain from
# outputting anything in those cases.
if [[ $- != *i* ]] ; then
    # Shell is non-interactive.  Be done now!
    return
fi

(not source /etc/bashrc)

whereas if you directly login or use su -, it will invoke a login shell, /etc/profile is read and sharedAliases.sh will be execute.

To see which file is read with different shells, adding logs to all these files by executing the following commands as root:

echo "echo 'running /etc/bashrc'" >> /etc/bashrc
echo "echo 'running /etc/profile'" >> /etc/profile
echo "echo 'running /root/.bashrc'" >> ~/.bashrc

Create an test alias:

# echo "alias list='ls'" > /etc/profile.d/test.sh

Now, login as normal user and use su, you will see something like this:

$ su
Password: 
running /root/.bashrc
bash-3.2# list
bash: list: command not found

and with su -:

$ su -
Password: 
running /etc/profile
running /root/.bashrc
# list
total 226540
-rw-rw-r-- 1 root root     60148987 Apr  1  2011 3241948.flv
quanta
  • 52,423