3

I'm used to use sudo -i and don't have a root password set, i.e. su - doesn't work. I'm looking for a way to share bash aliases between a user shell (e.g. the one opened when I start gnome-terminal started as the user who logged into the display manager) and the shell opened by sudo -i.

According to man sudo it's a login shell and afaik it should source ~/.profile, i.e. /root/.profile, but putting aliases in /root/.profile doesn't work. Only if I run bash in the shell opened by sudo -i the alias is available. According to /etc/passwd root's shell is bash and SHELL variable confirms that (in the shell started by sudo -i).

I'd prefer to not put the aliases in /etc/profile because that would expose them to any user and I only want to have them for the display manager user and root.

2 Answers 2

3

The related Serverfault post suggests using /etc/bashrc, but I personally would recommend creating /etc/shell_aliases file ( apparently /etc/aliases by sendmail package is already reserved ) , and source it from your user's and root users .bashrc file. That way you can re-define aliases globally.

Alternative approach as shown in this AskUbuntu post is to use /etc/profile.d/00-aliases.sh.

$ sudo bash -c 'echo alias print_hello=\"echo 'HELLO\ ASKUBUNTU'\" > /etc/profile.d/00-aliases.sh  ' 
$ sudo -i
$ print_hello
HELLO ASKUBUNTU

Side note: for GUI shells , you will have to log out and log in, because .profile is sourced each time you log in, and once you sing into the system via graphical login screen, it will be sourced , but only once. All terminal emulators opened are considered interactive shells after that.

1
  • I just figured that neither /etc/bashrc nor /root/.profile is read, i.e. alias isn't available which I think is a bug. Adding to /etc/profile.d/00-aliases.sh works. Thank you. Aug 11, 2016 at 2:51
0

This worked for me. Open your root .bashrc file and edit the bash_aliases function.

Change:

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

to

if [ -f /home/[username]/.bash_aliases ]; then
    . /home/[username]/.bash_aliases 
fi

You must log in to answer this question.

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