Recently I was asked how I’d managed to set up alias’s for all of the development servers I use on a day to day basis on my work laptop (a MacBook Pro), and how I had managed to set a default username for all outgoing connections. To which I replied I’d just set up my most commonly used connections in my ssh config file. What follows is a sample of my ssh config, which will hopefully help you to write your own.
If you do want to set up your own ssh config then first things first, let’s make sure you have the correct directories set up with the correct permissions.
$ mkdir ~/.ssh/ $ touch ~/.ssh/config
Make sure that only your user can read your .ssh directory and the config file.
$ chmod -R 600 ~/.ssh
Now that you have your config file, you can put some values in it. The main things you’ll need to put in the file are:
- Host – The alias(s) you wish to use
- User – The usename you connect with
- HostName – The fully qualified domain name of the server
Host carlcasbolt blog User ccasbolt HostName 18.104.22.168 Host dev User ccasbolt HostName dev.example.com Host * User ccasbolt
The example above shows that the value for Host can contain more then one option. This is for people who want to have more then one alias for a connection. I find this useful at times as I sometimes forget what alias I’ve set up at work because we have so many machines. Once you’ve set up your config you can go from using this:
$ ssh dev.example.com -l ccasbolt
To using something far nicer like:
$ ssh dev
Using scp becomes a lot easier with these alias’s set up.
Without ssh config
$ scp filename email@example.com:./path/to/put/file
With ssh config
$ scp filename dev:./path/to/put/file
The wildcard ‘*’ also comes in handy when I’m using my work machine as it has a different username to my hosting. If I was to use just ssh dev.example.com then it would use the same username I use on my laptop. To fix this you can set the default username with Host * and it will use that rather the username on your local machine.
Host * User ccasbolt