Shell tab-completion for service command broken as root

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

Ok so I've figured it out, sort of.

Things that work:

  • sudo -i
  • sudo su -


Things that don't work:

  • sudo -s
  • sudo su
  • sudo bash

So it appears to me that in the former case, you're actually creating a new login shell, so bashrc, profile, etc are loaded correctly, either from ~ or from /etc/skel. In the latter case you're just "executing bash as root" and not actually loading all those critical functions into the shell for autocomplete and the like.

Caveat: My explanation might be 100% wrong. But the top group does work.