SSH Config Files

Ethan Robish //

Here’s a short intro for anyone not familiar with ssh config files, which are usually located at ~/.ssh/config

As an example, you have ssh running on port 2222 on a system you refer to as “linux” with the username of “root”. You might have an ssh config entry that looks like this:

Host linux
  HostName 192.168.1.100
  Port 2222
  User root

In this example “linux” is simply any name you want to use when connecting to the remote system. It has nothing to do with the actual hostname or any other configuration of the remote system.

This lets you shorten your ssh command to:

ssh linux

instead of:

ssh -p 2222 root@192.168.1.100

It also works for scp like this:

scp some_file linux:/root/some_file

This is just scratching the surface for what you can do but it is definitely made my life much more convenient.

Other common directives I use are LocalForward, DynamicForward, and IdentityFile which correspond to the -L, -D, and -i ssh command line options respectively.

This was a quick introduction, but here is an excellent article that goes into more depth if you’re  interested in learning more.

And of course you can check the man page for ssh_config if you really want to geek out.

Bonus Tip:

Have you ever wanted to add a port forward to an existing SSH connection? Maybe you just decided to kill the connection and start it again with the additional command line options.

If for some reason you can’t restart your connection, fret not!  In OpenSSH you can add a port forward by entering the correct EscapeChar and then starting an OpenSSH command line. By default the escape character is tilde (~) and a capital C is used to enter a command. After that you can specify a port forward just like you would if you called ssh from the command line. So instead of running this:

ssh linux -L 8000:127.0.0.1:8000

You could stay in your ssh session and use the ~C sequence instead.

Reference: https://coderwall.com/p/5wp2wg/start-port-forwarding-over-an-existing-ssh-connection-instead-of-creating-a-new-one