Why SSH Autocomplete Works on Some Servers But Not Others: A Technical Deep Dive


2 views

When working with multiple SSH servers, you might notice that tab completion behaves differently across sessions. This isn't random - it's determined by several technical factors configured on the server side.

SSH autocomplete primarily depends on:

  • Shell configuration (bash, zsh, etc.)
  • Readline library settings
  • Terminal type and capabilities
  • User permissions and environment variables

The most common reason for autocomplete discrepancies is different shell configurations. Let's examine a typical bash configuration that enables autocomplete:

# Sample .bashrc configuration for proper autocomplete
if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi

# Enable programmable completion features
if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
fi

To diagnose why autocomplete isn't working, first check your shell type and configuration files:

# Check current shell
echo $SHELL

# Verify bash completion is loaded
type _completion_loader

If you can't modify server configurations, try these client-side solutions:

# Force bash completion in your SSH session
ssh user@server -t "bash --rcfile ~/.bashrc"

# Alternative using screen/tmux
ssh user@server -t "tmux new-session 'exec bash --rcfile ~/.bashrc'"

Sometimes the problem lies in terminal emulation. Check and set proper terminal type:

# Check current terminal type
echo $TERM

# Set to a common terminal type if needed
export TERM=xterm-256color

File permissions can prevent proper sourcing of completion scripts. Verify with:

# Check permission on bashrc and completion files
ls -la ~/.bashrc /etc/bash_completion

For servers where you can't modify system files, consider a personal completion setup:

# Create personal completion directory
mkdir -p ~/.bash_completion.d

# Add to your .bashrc
echo 'for file in ~/.bash_completion.d/*; do source "$file"; done' >> ~/.bashrc

When working with multiple SSH servers, you might notice inconsistent behavior in tab autocompletion. This typically manifests when:

  • Tab completion works flawlessly on Server A
  • Pressing Tab does nothing on Server B
  • Both servers are otherwise identical (same OS, same AWS cluster)

The disparity stems from how shell completion is configured on each server. Key factors include:

# Check if bash-completion package is installed
dpkg -l | grep bash-completion  # Debian/Ubuntu
rpm -qa | grep bash-completion  # RHEL/CentOS

Common configuration files affecting completion:

~/.bashrc
/etc/bash.bashrc
/etc/profile.d/bash_completion.sh

Administrators might intentionally disable completion for:

  • Security reasons (restricted shells)
  • Performance optimization on resource-constrained systems
  • Custom shell environments (like busybox)

To investigate the difference between your two servers:

# Compare bash completion setup
ssh user@working-server "cat /etc/profile.d/bash_completion.sh" > working.txt
ssh user@broken-server "cat /etc/profile.d/bash_completion.sh" > broken.txt
diff working.txt broken.txt

If you can't modify server configuration, try these client-side solutions:

# Force enable basic completion
ssh -t user@server "bash --norc --noprofile -i"

# Use ssh config with remote command
Host myserver
  HostName server.example.com
  RequestTTY force
  RemoteCommand bash -l

For servers you control, ensure proper completion setup:

# Ubuntu/Debian
sudo apt install bash-completion

# RHEL/CentOS
sudo yum install bash-completion
sudo ln -s /etc/profile.d/bash_completion.sh /etc/bash_completion

For specialized environments, you can create custom completion rules:

# Example custom completion for app commands
complete -F _app_completion app

_app_completion() {
  local cur=${COMP_WORDS[COMP_CWORD]}
  COMPREPLY=( $(compgen -W "start stop restart status" -- $cur) )
}
  • Verify $SHELL environment variable matches your expected shell
  • Check for restrictive umask settings (should be at least 0022)
  • Look for missing read permissions on completion scripts