SSHing efficiently   6 comments

I personally have a numerous number of hosts which I sometimes have to SSH to. It can get rather confusing and inefficient if you get lost among them.

I’m going to show you here how you can get your SSHing to be heaps more efficient with just 5 minutes of your time.


In $HOME/.ssh/config I usually store all my hosts in such a way:

Host host1
    Port 1234
    User root

Host host2
    Port 5678
    User root

Host host3
    Port 9012
    User root

You obviously got the idea. So if I’d like to ssh to host2, all I have to do is:

ssh host2

That will ssh to – saves a bit of time.

I usually manage all of my hosts in that file. Makes life simpler, even use git if you feel like it…

Auto complete

I’ve added to my .bashrc the following:

_ssh_hosts() {
    local cur="${COMP_WORDS[COMP_CWORD]}"
    local ssh_hosts=`grep ^Host ~/.ssh/config | cut -d' ' -f2 | xargs`
    [[ ! ${cur} == -* ]] && COMPREPLY=( $(compgen -W "${ssh_hosts}" -- ${cur}) )

complete -o bashdefault -o default -o nospace -F _ssh_hosts ssh 2>/dev/null \
    || complete -o default -o nospace -F _ssh_hosts ssh
complete -o bashdefault -o default -o nospace -F _ssh_hosts scp 2>/dev/null \
    || complete -o default -o nospace -F _ssh_hosts scp

Sweet. All that you have to do now is:

$ ssh TAB TAB
host1 host2 host3

We are a bit more efficient today.


Posted March 31, 2013 by malkodan in Bash, Linux, System Administration

Tagged with , , ,

6 responses to “SSHing efficiently

Subscribe to comments with RSS.

  1. Hey, the auto completer is really nice. Thx. Regarding the main focus about ssh config file, I didn’t use it much in the past, but you are right, it is saving time. There is only one questio. If the host name from ssh config is same as some record in /etc/hosts, is it workng a way that it use ssh co fig instead of hosts record?
    Thx, Ladislav.

  2. The latter (ssh autocomplete) is part of the standard bash-autocomplete package.

  3. Hey Tzafrir, that I guess depends on your Linux distro. Just checked on Fedora 18 and couldn’t find it.

    Ladislav – if it’s in your /etc/hosts then it just means it’ll try to resolve it using /etc/hosts, but I generally don’t like to trash my /etc/hosts. Prefer to keep it clean. Way better to keep your SSH hosts in ~/.ssh/config as you can control their parameters such as port, username and more.

  4. I see, as I expected. I agree that keeping the trash in /etc/hosts isn’t a good way. On the other hand it helps me in situations when I have many VPN accesses active in same time (OpenVPN, pptp, l2tp, etc.) in one time, In these cases I rather forgot DNS set up at all and set up all machines from all the networks manually in hosts file. Of course, this is a security issue at all 🙂 Anyway setting the host records in ssh to something what cannot be resolved by dns or host is the way. Thanks

  5. Pingback: Stupid Bash Tricks for SSH | The Practical Administrator

  6. Reblogged this on Black Hole Of My Memory.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: