Jeff Thomas

My Reflections while on the pale blue dot

Simplify ssh connection strings

October 07, 2014

Internet hosting providers sometimes allow ssh access to update files without using a web interface. If the hosting account resides on a VPS, often the naming conventions are not friendly as you may be one of hundreds of accounts on that server.

Even if you have your own server, the username and hostname may be long or unfriendly. Perhaps you changed the default port for ssh.

This post will show you how to simplify your ssh connection strings.

For example, here's a ssh string using a non-default ssh key and port number.

ssh -i ~/.ssh/a_public.key user489c@some.server01.com -p 5486

Using the ssh config file we can easily abstract away the details to something easier to remember.

ssh my_blog_site

Enter the ssh config file

Assuming the ssh keys are already configured to login to the server, let's take this one step at a time.

First, create the ssh config file if it doesn't exist

touch ~/.ssh/config

Open and add a entry for the ssh connection string parameters above:

# ~/.ssh/config
Host myblog

Next abstract the location of the public key to use for this connection.

# ~/.ssh/config
Host myblog

IdentityFile .ssh/hosting_public.key

So no we have:

ssh 7605.us.34@jungleserver01.34j.com -p 5486

Let's tackle the port number next.

# ~/.ssh/config 
Host myblog

IdentityFile .ssh/hosting_public.key Port 5486

Easier...

ssh 7605.us.34@jungleserver01.34j.com

Add finally the username and hostname

# ~/.ssh/config
Host myblog

IdentityFile .ssh/hosting_public.key Port 5486 User 7605.us.34 HostName jungleserver01.34j.com

The final simplified version of the ssh connection string.

ssh myblog

Using the ssh config file and alias together

Why not just create an alias you ask? This is definitely an option. In fact using a combination of aliases and the ssh config file can give you even more leverage.

For example, let's say I have 5 servers each with 10 separate unix user accounts. This is 50 unique ssh strings to manage.

server1
user1
user2
...
user10

server2
user1
user2
...
user10

...

Using the ssh config file I can abstrast the server hostname.

~/.ssh/config

Host production1
    HostName server1.somewhere.com
    
Host production2
    HostName server2.somewhere.com
    
Host staging
    HostName server5.somewwhere.com
    
Host development
    HostName server7.somewhere.com

Using an alias file I can further simplify the command

#.zshrc or .bashrc or etc.
alias p1='ssh user1@production1'
alias p2='ssh user2@production2'
alias s1='ssh user1
...
etc

Now I abstract my all my server locations. If the server hostname or something else changes, it's arguably easier to refactor using these files.

This is obviously a contrived example, but hopefully this spawns some creativity.

One final bonus is if you use a gui client like Filezilla or Transmit. You can use the values from your ssh config file.