sudo command doesn't autocomplete anymore on files

11,845

Solution 1

Try firstly reinstalling the bash-completion package with:

sudo apt-get install --reinstall bash-completion

Then backing up your .bashrc file:

mv ~/.bashrc ~/.bashrc.bak

Finally, replace your .bashrc file with the default from /etc/skel/.bashrc by using:

cp /etc/skel/.bashrc ~/

This will ensure the following 'complete' config is restored within the file:

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

Also check both the containing folder and file permissions of /etc/bash.bashrc and ~/.bashrc - ownership should be root:root and yourusername:yourusergroup respectively. The permissions of the files are important as the shell only uses your own privilege level to auto-complete commands. Elevated permissions such as when sudo is invoked are only used after the command is committed by pressing enter.

Solution 2

Add

complete -cf sudo

to your .bashrc and .bash_profile. Autocompletion will work on next login

Solution 3

I find out that the problem wasn't about autocomplete, but just a matter of permissions. The folder is own by root:mail instead of root:root as most of the directories in /etc and my user wasn't in the mail group. Once I added the user to the right group it worked. I just don't understand why the command with sudo in front of it didn't superseded the group...

Share:
11,845

Related videos on Youtube

effemmeffe
Author by

effemmeffe

none of the above

Updated on September 18, 2022

Comments

  • effemmeffe
    effemmeffe almost 2 years

    Ubuntu 16.04.1 LTS

    I don't know what I did, everything was working fine, but since today my bash doesn't autocomplete anymore when I use sudo. I can use the autocompletion as normal user, I can use autocompletion as root, I can use autocompletion for directories with sudo, but I can't use autocompletion for files if I use sudo.

    If I write:

    $sudo vim /etc/ssm[TAB]
    

    it will autocomplete to:

    $sudo vim /etc/ssmtp/
    

    but then if I hit [TAB] again, even twice like I was used to list the files in the directory, nothing happens. I already checked and I have the autocompletion enabled in the user .bashrc and also in /etc/bash.bashrc

  • effemmeffe
    effemmeffe over 7 years
    Done. The problem is still there.
  • Betty Von Schmartenhausen
    Betty Von Schmartenhausen over 7 years
    It still appears you have some problem with bash-completion. Maybe try uninstalling with the 'purge' option. As a workaround and test, you can use the native completion of bash with alt + / rather than tab, which should complete file names for you.
  • effemmeffe
    effemmeffe over 7 years
    I already purged and reinstalled. The strange thing is that I can use autocomplete as user and as root, but not as user using sudo.
  • Betty Von Schmartenhausen
    Betty Von Schmartenhausen over 7 years
    @effemmeffe I have updated the answer to include your solution to give a fully rounded answer.
  • marosg
    marosg over 7 years
    Because elevated permission from sudo start to work only after you press enter, before pressing enter shell uses just your permissions.
  • effemmeffe
    effemmeffe over 7 years
    I think that you could also add the comment from user596137 about the fact that sudo doesn't autocomplete because it uses user permission before pressing enter.
  • Betty Von Schmartenhausen
    Betty Von Schmartenhausen over 7 years
    Added, credit to @user596137
  • baltasvejas
    baltasvejas about 5 years
    Reinstalling bash-completion worked for me. Thanks.
  • Dolan Antenucci
    Dolan Antenucci almost 4 years
    For commands like sudo su this does not add username tab completing, so not what I was looking for at least