1

I have two aliases set up in my .bash_profile. I want sudo to honor both of them:

$ cat ~/.bash_profile
export EDITOR=emacs
...
alias yum="dnf"
alias ssh="ssh -2"
...

I know how to tell sudo to honor my environment (-E), but I can't find the option for aliases. Searching the man page for alias results in Pattern not found.

How do I tell sudo to honor my aliases?

Thanks in advance.


I'm trying to avoid these annoying messages due to Fedora changing commands:

$ sudo yum update
[sudo] password for: 
Yum command has been deprecated, redirecting to '/usr/bin/dnf update'.
See 'man dnf' and 'man yum2dnf' for more information.
...

2 Answers 2

1

sudo can't really "honor" aliases, as they're a shell-specific feature – the only way sudo could know about them if it was taught how to read ~/.bashrc, ~/.zshrc, ~/.cshrc... by itself.

For ordinary aliases, you can trick the shell itself into expanding them when followed by sudo:

alias sudo="sudo "

(It's sort-of documented somewhere.)

But for functions, there is nothing similar; you'll have to use the interactive sudo -sE mode instead.

1
  • Thanks Grawity. I'll need some time to get back to Fedora for testing.
    – jww
    Oct 3, 2015 at 14:30
1

Another possible solution to your problem is to put your aliases, as usual, in your file /home/YourName/.bash_aliases, and then to link that file into root's home directory:

      sudo ln -s /home/YourName/.bash_aliases /root/.bash_aliases

This should work automatically because most distros have these lines in the .bashrc file you are given by default,

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

If you do not have them, it is easy to add them.

You must log in to answer this question.

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