7

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.

5 Answers 5

4

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
2

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.

0

You can do following steps to get completion for sudo in bash:

  1. Open your .bashrc.
  2. Insert the following lines and save the file
    if [ "$PS1" ]; then
    complete -cf sudo
    fi
    The complete command generates lists of completions for command names (-c) and files name (-f) for sudo.
  3. Open a new bash and type sudo -sTab or any other command you want to use.
0

I had the same problem with root not auto-completing. I unsuccessfully tried a couple other ways including messing around with the .bashrc and installing a couple packages. What finally worked for me was as simple as just running bash. So you do your sudo su and tab just lists (in /root)

root@xxxxxxxx:~# service<tab>
root@xxxxxxxx:~# service .<tab><tab>
./             .aptitude/     .bashrc        .viminfo  
../            .bash_history  .profile  

echo $SHELL replies /bin/bash so I'm running bash, but it's not working. Now I run bash and my shell looks the same still.

root@xxxxxxxx:~# bash
root@xxxxxxxx:~# service <tab><tab>
acpid                        plymouth-upstart-bridge  
apache2                      portmap  
apparmor                     portmap-wait  

Now it's working. Just keep in mind that you'll have to double exit to exit the bash you just launched and then exit the sudo su session so you don't leave a root terminal open.

root@xxxxxxxx:~# uname -mrs
Linux 3.8.0-33-generic x86_64  
root@xxxxxxxx:~# lsb_release -d
Description:    Ubuntu 12.04.3 LTS
-1

I had the same problem. What I tried is:

  1. Run /usr/sbin/visudo as root user
  2. Add this line to it then save and exit:

    Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin

  3. Open new session and try to run command now

That's it; you have done it.

1
  • How does this solve the problem? AFAIK those paths are already in the defaults anyway. May 1, 2014 at 8:47

You must log in to answer this question.

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