13

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

3 Answers 3

11

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.

8
  • Done. The problem is still there.
    – effemmeffe
    Commented Jan 20, 2017 at 19:46
  • 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. Commented Jan 21, 2017 at 10:01
  • 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.
    – effemmeffe
    Commented Jan 21, 2017 at 13:19
  • @effemmeffe I have updated the answer to include your solution to give a fully rounded answer. Commented Jan 25, 2017 at 7:47
  • 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.
    – effemmeffe
    Commented Jan 25, 2017 at 11:17
7

Add

complete -cf sudo

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

1
  • For commands like sudo su this does not add username tab completing, so not what I was looking for at least Commented Sep 25, 2020 at 12:21
2

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

1
  • 3
    Because elevated permission from sudo start to work only after you press enter, before pressing enter shell uses just your permissions.
    – marosg
    Commented Jan 25, 2017 at 8:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .