Posted by chantra on April 12th, 2008
SSH is great. There is so many thing you can do with it other than just a remote secure shell like X forwarding, port forwarding, authenticate using a private/public key, compress the transmitted stream....
If you have different account that you use on an every day basis, it becomes quickly cumbersome to type those lengthly command lines.
One could work around this by using aliases, the right way would be to use ~/.ssh/config
This tutorial will show some customization examples that should cover most ssh use cases.
All along this tutorial, the changes will be made in ~/.ssh/config and will therefore be available only for your user.
If you wish to make changes server wide, you will have to edit /etc/ssh/ssh_config .
1. A simple example
Most of your ssh connection might look like:
$ ssh firstname.lastname@example.org
By adding the following to your ~/.ssh/config :
Host server1 Hostname server1.example.com User user1
You can shorten the typing to:
$ ssh server1
2. Adding Public/Private key authentication
If you identity file is named ~/.ssh/id_rsa or ~/.ssh/id_dsa , those file will be tried automatically by ssh.
In case you have different key files, you have to inform ssh about it with -i , which look like:
$ ssh -i ~/.ssh/id_rsa_server2 email@example.com
Adding the following to ~/.ssh/config :
Host server2 Hostname server2.example.com User user2 IdentityFile ~/.ssh/id_rsa_server2
will allow you to type:
$ ssh server2
to get the same result.
3. Port Forwarding
Forwarding port with ssh requires a command line like:
$ ssh -L 8888:127.0.0.1:7777 -i .ssh/id_rsa_server3 firstname.lastname@example.org
The following in ~/.ssh/config :
Host server3 Hostname server3.example.com User user3 IdentityFile ~/.ssh/id_rsa_server3 Localforward 8888 127.0.0.1:7777
will shorten the command to:
$ ssh server3
4. More options
The 3 examples above cover my needs with for my day to day use of ssh. There is plenty of other options that you might find useful.
You can find more info from :
$ man ssh_config