Vu Long Tran

How to create an SSH Alias to access your remote machines

Written by Vu Long Tran | Dec 11, 2020 8:28:32 AM

If wondered if you can shorten the command you use when you ssh to the same machine all the time.

Well, you can pre-configure these settings in a configuration file and have it reference that when you are SSHing into a machine.

That way you can just SSH using this shorter command: $ ssh master

Instead of the long command: $ ssh username@ipaddressofmachine -i location/of/ssh/key/file

Here's how you do it.

How to create an SSH Alias to access your remote machines

I am using a MacPro and iTerm2 in this example. You should be able to follow the steps using your computer as well.

Step 1 - Open your ssh configuration file

There is a default SSH configuration file stored at ~/.ssh/config.

If it is not there, just create a SSH configuration file.

$ cat ~/.ssh/config

You can also use .ssh/config if you are already in your home directory.

You should see the configuration file now.

Step 2 - Add in your remote hosts to your ssh configuration file

When you are ready, you can add in the configuration you want. 

$ vi ~/.ssh/config

Here is some sample text you can use. 


Host <name for your host>
HostName <ip address of your host>
User <username for your machine>
IdentityFile <your key location


Optional

Optional, you can add in the SetEnv variable here as this will preset your server to hwith an English locale.

Host *
SetEnv LC_ALL=C
AddKeysToAgent yes
UseKeychain yes

Host <name for your host>
HostName <ip address of your host>
User <username for your machine>
IdentityFile <your key location

Step 3 - Test that it is working by SSHing into the machine/s

Now you can test that it is working by SSHing into your machine.

$ ssh <host name you created earlier>

For example, I can just log in with the following:

$ ssh master

Instead of the long way:

Step 4 - Enjoy SSHing the fast way!