Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have big list of servers which I normally ssh to all the time. Is there any way using bash or zsh so that I can keep the list of hostname and bash auto-completion goes through the file and gives me suggestion for the boxes starting with those letters whenever I type the first few letters of the hostname.

share|improve this question
add comment

3 Answers 3

up vote 6 down vote accepted

Zsh completion works with so called ssh bookmarks. These are per host configurations in ~/.ssh/config.

For example,

  host baz
    hostname 192.168.1.2
    port 22
    user warrick

  host bar
    hostname example.com
    port 2200
    user kevin
    identityfile /home/warrick/.ssh/ec2.pem

man ssh_config to see a full list of options.

EDIT

I am using the completion script from ohmyzsh:

https://raw.github.com/robbyrussell/oh-my-zsh/master/lib/completion.zsh

In similiar style to ohmyzsh, I placed the above script into ~/.zsh/libs/completion.zsh and added this to ~/.zshrc.

# ~/.zshrc
for f in ~/.zsh/libs/*; do
  source $f
done
share|improve this answer
    
I don't use zsh but bash... –  sputnick Oct 5 '12 at 21:12
    
Right, the OPs question is for bash or zsh, my comment on your post was simply to favor ~/.ssh/config to /etc/hosts if at all possible, which as per his question is possible when using zsh. –  kwarrick Oct 5 '12 at 21:17
add comment

The bash completion package includes completions for ssh commands, including:

  • ssh
  • ssh-add
  • ssh-copy-id
  • sshfs

You can browse the source here: https://alioth.debian.org/scm/browser.php?group_id=100114

share|improve this answer
add comment

For this to work, put any hosts you want to complete in your /etc/hosts file.

You need also bash-completion package (containing file /etc/bash_completion and directory /etc/bash_completion.d)

and source it in ~/.bashrc (. /etc/bash_completion - sometimes it's commented out in /etc/bash.bashrc or in ~/.bashrc).

share|improve this answer
2  
I'd recommend against using /etc/hosts. Instead add so called ssh bookmarks in ~/.ssh/config. man ssh_config for more details, but it is far more versatile allowing you to specify multiple aliases, username, port, and much much more. –  kwarrick Oct 5 '12 at 20:52
    
With archlinux, when I put something in /etc/ssh/ssh_config, I have no completion working for the configured hosts. –  sputnick Oct 5 '12 at 20:56
    
See my extended answer, I am using completion from ohmyzsh. –  kwarrick Oct 5 '12 at 21:05
1  
If you look at the source, you will see that the script checks known_hosts as well as the other config files, eg., ~/.ssh/config –  jasonwryan Oct 5 '12 at 21:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.