Ubuntu – Shell tab-completion for service command broken as root

12.04auto-completionbashroot

On most of our servers, tab completion of service names after typing in the service command no longer works, as root. It works just fine as non-root. This is on Ubuntu 12.04. I'm not sure how to approach troubleshooting this.

Demonstrating:

Tab completion works as the normal user zachary, who pressed tab to list completions in the first image:

Tab completion works

But as user root, it doesn't list services as completions (only files):

Now it does not

Any help would be greatly appreciated.

Best Answer

This is disabled by default on Ubuntu.

Read your /root/.bashrc:

# 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 [ -f /etc/bash_completion ] && ! shopt -oq posix; then
#    . /etc/bash_completion
#fi

Bash completion is all commented out. Apparently there's a reason for it I'm not aware of (recovery single user mode perhaps?).

The reason for why it does work when you do sudo su, sudo -s, sudo bash is that it doesn't really run login to make you completely root. When running sudo su - or sudo -i it really does completely logs you in as that user. Here, by example of the environment variable $HOME, more of this difference:

gert@gert-laptop:~$ id
uid=1000(gert) gid=1000(gert) groups=1000(gert),4(adm),7(lp),24(cdrom),27(sudo),[...]
gert@gert-laptop:~$ echo $HOME
/home/gert
gert@gert-laptop:~$ sudo -s
root@gert-laptop:~# id
uid=0(root) gid=0(root) groups=0(root)
root@gert-laptop:~# echo $HOME
/home/gert
root@gert-laptop:~# exit
gert@gert-laptop:~$ sudo su -
root@gert-laptop:~# id
uid=0(root) gid=0(root) groups=0(root)
root@gert-laptop:~# echo $HOME
/root
Related Question