[sudo-users] sudo target user's alias

Todd C. Miller Todd.Miller at courtesan.com
Fri Aug 23 15:54:46 MDT 2013


The stackoverflow posting you are referring to has to do with
expanding the invoking user's aliases sudo is used in an interactive
shell.  In other words, they are for expanding the invoking user's
aliases, not the user the command will be run as.

The problem you are seeing is that bash does not expand aliases for
non-interactive sessions.  You will see the same behavior if you
run:

$ bash -c "ll"
bash: ll: command not found

In fact, if you do:

$ bash -c "alias"

You will see that there are no aliases defined.  It is only if we
force bash into interactive mode that aliases show up.  E.g.

$ bash -ic "alias"

alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

>From the bash manual:

    Aliases are not expanded when the shell is not interactive,
    unless  the expand_aliases  shell option is set using shopt
    (see the description of shopt under SHELL BUILTIN COMMANDS).

You can get the behavior you want by adding:

shopt -s expand_aliases

to root's .bashrc but beware that many .bashrc files have something
like:

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

which will cause anything after that line to be ignored in a
non-interactive shell.

 - todd


More information about the sudo-users mailing list