Using sudo with an alias
Typically sudo will ignore any aliased commands from your .bashrc, .bash_aliases, or the alias command. For example, I use "ll" as an alias for "ls -lh". Typing "ll" will give me a long-listing of a directory's contents, while typing "sudo ll" will give me:
sudo: ll: command not found
I learned this when I tried to create an alias for "shutdown" that would refuse to shutdown if rtorrent was running. Unfortunately you need root privileges to use /sbin/shutdown, and sudo would completely ignore the clever script I aliased as "shutdown". The solution is an additional alias:
alias sudo='sudo '
The space following "sudo" tells bash to check if the command that follows the space is also an alias. From the bash man page:
man bash ... If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion. ...
Now my aliased shutdown script is called even though it's being run with sudo. I've heard of another solution (also an alias), but I haven't tried it (1) because this one works just fine, and (2) because I don't understand why it works (if it does work). Here it is:
alias sudo='A=`alias` sudo '
I'm not sure if it's just the hard way of doing things, or if it's somehow better, worse, or different.
Here's a copy of my current .bash_aliases file. This is a fresh install, so I'll probably add a lot to it in the future.
# Shortcuts alias ll='ls -lh' alias la='ls -lhA' alias l='ls' alias c='clear' alias x='exit' alias q='exit' # Don't run shutdown if rtorrent is running - as long as there's a screen with "tor" in its name, shutdown won't run (unless you call /sbin/shutdown, or unalias it) alias shutdown='/home/james/scripts/safe.shutdown.sh' # When using sudo, use alias expansion (otherwise sudo ignores your aliases) alias sudo='sudo '
August 4th, 2010 - 10:39
Thanks you for the: alias sudo=’sudo ‘
It helped a lot.
August 5th, 2010 - 15:22
Glad it helped.
August 26th, 2010 - 04:46
Both work great, but I’m really puzzled by the “A=” part, anyone has any idea about it ?
September 11th, 2010 - 01:32
Big, Big, thanks for that space!
IT IS INEXCUSABLE THAT THIS ALIAS IS NOT PRESENT AS DEFAULT ON SUDO-based DISTROS !!!
GRRR
(Was forced to use sudo-i fo half a year because of this stupidity.)
September 11th, 2010 - 10:46
Completely agree. Also it’s annoying how hard it is to use sudo with functions (instead of aliases) (someone let me know if there’s an easy way).
November 6th, 2010 - 04:57
saved my time, thanks a lot!
November 6th, 2010 - 15:23
Glad to help.
December 8th, 2010 - 00:25
thanks, but thanks.
Very intelligent: alias sudo=’sudo ‘
January 4th, 2011 - 18:31
In Response to:
shell.person:
Completely agree. Also it’s annoying how hard it is to use sudo with functions (instead of aliases) (someone let me know if there’s an easy way).
What is hard about using sudo with functions compared to aliases? Functions can actually be exported “export -f function” and then used with sudo without any fuss. I’m a big fan of exporting function though it’s not very popular and many people are unaware it’s even possible.
October 3rd, 2012 - 07:26
Awesome!
October 8th, 2012 - 23:22
What a handy alias tip. Thanks!
November 28th, 2012 - 21:50
This trick works great if you just use sudo but fails if I use sudo -i. Once I issued a ‘sudo -i’ I wound up losing all my nice .profile aliases and customizations. I’m on a Mac running 10.8.2 so your mileage may vary but the trick for me was to use two .profile files. The problem comes down to what happens when you sudo -i. It shifts you to the root user and in doing so looks for a .profile for that user. I just duplicated the .profile from my /Users/’username’ to /var/root/ so that it could be found properly. There is no need for an ‘export SUDO_PS1…’ line in your .profile with this option since the standard PS1 prompt is used automatically. It works a charm and keeps all my aliases and color customizations intact.
This is my .profile file for reference.
=========
export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced
export PS1=”\[33[0;37m\]\u\[33[0m\]@\[33[1;35m\]\h\[33[0m\]:\[33[1;36m\]\w\[33[0m\]\$ ”
# Aliases
alias sudo=”sudo ”
alias grep=”grep –color”
alias locate=”locate -i”
alias ls=”ls -lh”
alias pg=”ping google.com”
# Path
export PATH=”/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin”
# Setting for the new UTF-8 terminal support in Lion / Mountain Lion
export LC_CTYPE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
December 13th, 2012 - 07:02
This is perfect! I aliased sudo to ‘sudo -i ‘, to have my environment vars set up as well. Thank you so much!
December 30th, 2012 - 20:18
Hot damn, dude. Great tip. I may never have to use `sudo -s` again! :)
December 30th, 2012 - 20:33
Also, I too was curious about the A=`alias` solution, and while I can’t really find the reason that was originally suggested (nor can I find any other reference to such a suggestion), I believe it works simply because the more concise way works. That is, bash stores the output of `alias` (a listing of all current aliases) in the variable $A, then performs the rest of the aliasing normally.
Proof: It also works if you do
alias sudo=’X=”y” sudo ‘
The real question is why this was suggested in the first place.
May 7th, 2013 - 21:00
Not surprising you’re #1 on my Google search for “sudo alias”. Thanks, man.