Imarc

After your SSH Key: Configuring SSH for Casual Users on OS X or Linux Kevin Hamer

Written on: January 12th, 2015 in engineering

Lots of people use SSH (or SFTP) to copy files around, or occasionally login to servers. On Windows, PuTTY has a nice settings window where you can set all kinds of things, but on OS X and Linux, we're working with good old OpenSSH, which just looks for a regular text file for configuration.

Configuring SSH is simple. Create a text file called 'config' in your .ssh folder. (Usually in your home folder.) If you use SSH keys, this is the same folder you put those too.

Set a default username

Let's start with just two lines in your config file (.ssh/config):

Host *
    User web

This lets you set the default username you'd like to use when you SSH or SFTP into servers: in my example, 'web'. Technically the first line is optional, but I think it's clearer to use.

Set another default username

Have one server that you login with a different username for? Let's add a couple more lines:

Host *.amazonaws.com
    User ubuntu

Host *
    User web

Now, SSH will default to login as 'ubuntu' when connecting to amazonaws.com servers and 'web' anywhere else. SSH uses the first set of settings where Host matches the username, so always put more specific things first.

Pick a different port

Every once in a while you might run into a server where you need to SSH into a different port than usual. That's easy to specify too:

Host strangeserver.net
    Port 8022

Host *.amazonaws.com
    User ubuntu

Host *
    User web

Now when I connect to strangeserver.net, it'll automatically connect to port 8022 instead of 22. One less thing I need to remember whenever I work on strangeserver.net.

Use shorter names for servers

It's even simple to setup short names for yourself for servers you connect to frequently:

Host pwi
    HostName prestige-worldwide-inc.com

Host strangeserver.net
    Port 8022

Host *.amazonaws.com
    User ubuntu

Host *
    User web

Now I can type just 'ssh pwi' instead of 'ssh prestige-worldwide-inc.com'.

SSH has more options too (persistent connections and ssh agent forwarding are both great things to look up if you're interested) but nearly everyone can benefit from at least setting up a default username for themselves.

Share:

Let's Talk.