Shell Person Help me keep the shell people alive.

19Apr/1017

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 '

Comments (17) Trackbacks (4)
  1. Thanks you for the: alias sudo=’sudo ‘

    It helped a lot.

  2. Glad it helped.

  3. Both work great, but I’m really puzzled by the “A=” part, anyone has any idea about it ?

  4. 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.)

  5. 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).

  6. saved my time, thanks a lot!

  7. Glad to help.

  8. thanks, but thanks.

    Very intelligent: alias sudo=’sudo ‘

  9. 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.

  10. What a handy alias tip. Thanks!

  11. 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

  12. This is perfect! I aliased sudo to ‘sudo -i ‘, to have my environment vars set up as well. Thank you so much!

  13. Hot damn, dude. Great tip. I may never have to use `sudo -s` again! :)

  14. 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.

  15. Not surprising you’re #1 on my Google search for “sudo alias”. Thanks, man.

  16. Thx for alias sudo=”sudo “


Leave a comment