Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was playing around with aliases today and I noticed that aliases don't seem to be available whilst using sudo:

danny@kaon:~$ alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

danny@kaon:~$ ll -d /
drwxr-xr-x 23 root root 4096 2011-01-06 20:29 //

danny@kaon:~$ sudo -i
root@kaon:~# ll -d /
drwxr-xr-x 23 root root 4096 2011-01-06 20:29 //
root@kaon:~# exit
logout

danny@kaon:~$ sudo ll -d /
sudo: ll: command not found

Is there any reason why you cannot use aliases whilst using sudo?

share|improve this question
    
Did you declare those alias in .bashrc on /root/ – Luciano Facchinelli Jan 17 '11 at 22:52
    
@LucianoFacchinelli: /root/.bashrc does not get read when running a command through sudo. – Flimm May 16 '13 at 11:25
    
up vote 123 down vote accepted

Add the following line to your ~/.bashrc:

alias sudo='sudo '

From the bash manual:

Aliases allow a string to be substituted for a word when it is used as the first word of a simple command. The shell maintains a list of aliases that may be set and unset with the alias and unalias builtin commands.

The first word of each simple command, if unquoted, is checked to see if it has an alias. If so, that word is replaced by the text of the alias. The characters ‘/’, ‘$’, ‘`’, ‘=’ and any of the shell metacharacters or quoting characters listed above may not appear in an alias name. The replacement text may contain any valid shell input, including shell metacharacters. The first word of the replacement text is tested for aliases, but a word that is identical to an alias being expanded is not expanded a second time. This means that one may alias ls to "ls -F", for instance, and Bash does not try to recursively expand the replacement text. If the last character of the alias value is a space or tab character, then the next command word following the alias is also checked for alias expansion.

(Emphasis mine).
Bash only checks the first word of a command for an alias, any words after that are not checked. That means in a command like sudo ll, only the first word (sudo) is checked by bash for an alias, ll is ignored. We can tell bash to check the next word after the alias (i.e sudo) by adding a space to the end of the alias value.

share|improve this answer
9  
Pretty nice solution. – ulidtko Jan 17 '11 at 23:23
    
So basically the issue boils down to the fact that sudo is simply programmed in such a way that it does not check alias's therefore ll is not found? – kemra102 Jan 18 '11 at 20:55
    
@DannyRoberts See my updated answer. – Alvin Row Jan 22 '11 at 4:55
    
The emphasized quote doesn't show up too well with askubuntu.com's current styling. For the curious, here is the key line: "If the last character of the alias value is a space or tab character, then the next command word following the alias is also checked for alias expansion." – 40XUserNotFound Feb 7 '15 at 11:15
2  
Reading the documentation obviously benefits a whole lot :-) – biocyberman Feb 10 at 20:48

The aliases are user specific - you need to define them in /root/.bashrc

share|improve this answer
    
They are already under /root/.bashrc so that's not an issue – kemra102 Jan 18 '11 at 20:55

@Alvins answer is the shortest one. No doubt! :-)

However I thought of a command line solution to execute an aliased command in sudo where there is no need to redefine sudo with an alias command.

Here is my proposal for those to whom it may interest:

Solution

type -a <YOUR COMMAND HERE> | grep -o -P "(?<=\`).*(?=')" | xargs sudo

Example

In the case of the ll command

type -a ll | grep -o -P "(?<=\`).*(?=')" | xargs sudo

Explanation

when you have an alias (such as: ll) the command type -a returns the aliased expression:

$type -a ll
ll is aliased to `ls -l'

with grep you select the text between the accent ` and apostrophe ' in that case ls -l

And xargs executes the selected text ls -l as parameter of sudo.

Yes, a bit longer but completely clean ;-) No need to redefine sudo as alias.

share|improve this answer
2  
Might be simpler if you used the alias builtin, since its output is cleaner. alias ll outputs alias ll='ls -ahlF'. – muru Feb 15 '15 at 19:41
    
Here's the version incorporating @muru's suggestion: alias ll | sed "s/.*'\(.*\)'.*/\1/g" | xargs sudo. – ACK_stoverflow 13 hours ago

I have a different solution whereby you do not need to add sudo as an alias. I run Linux Mint 17.3 but it should be pretty similar to Ubuntu.

When you are root, then the .profile is run from its home directory. If you do not know what the home directory under root is, then you can check with:

sudo su
echo $HOME

As you can see, the home of root is /root/. Check its contents:

cd $HOME
ls -al

There should be a .profile file. Open the file and add the following lines:

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

Basically, what this bash script does is to check for a file called .bash_aliases. If the files is present, it executes the file.

Save the .profile file and create your aliases in .bash_aliases. If you already have the aliases file ready, then copy the file to this location

Re-launch the terminal and you are good to go!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.