WAIT This guide is for users who are using SSH keys. If, instead, you are using a username and password to connect to your remote machine, make sure you follow one of our guides to setting up keys before you start. We have how-to-switch guides for macOS, Linux, and Raspberry Pi.
Setting up shortcuts
This guide explains how you can set up SSH shortcuts on your Mac, Linux, or Raspberry Pi computer, and thereby avoid having to remember long strings of information each time you connect to a remote computer using SSH.
Typically, to "SSH in" to a given server, a user needs to type a string similar this one from the command line:
ssh -i /Users/ExampleUser/Keys/sshkeys/key.pem ubuntu@123.456.789.101
This string tells your computer:
- that you want to connect using SSH
- that you want to use an SSH key rather than a password
- that the SSH key you want to use is located at
/Users/ExampleUser/Keys/sshkeys/key.pem
- that the username is
ubuntu
, and - that the server is at IP address
123.456.789.101
— quite a lot of information for you to remember! If SSH is, say, set up on a custom port, this string can be even more complicated. By setting up a reference file in ~/.ssh/config
you can bypass this headache completely and, instead, simply type ssh servername
from the command line each time you want to log in.
Here's how you do it.
Step 1
This guide assumes that you have already generated a set of SSH keys on your computer. If you have, you can skip this step. If you do not have a set of SSH keys installed on your local computer, you can generate one by running the following (replacing [note]
with your own note, of course):
ssh-keygen -t rsa -C "[note]"
There is no need to add a password to the key. This guide assumes that you will save the new keys in the suggested directory, which is ~/.ssh
.
Step 2
Now, we can create some SSH shortcuts. To set up a new shortcut open the file at ~/.ssh/config
. If there is no config
file in the ~/.ssh
folder, the act of opening it will create a blank one:
sudo nano ~/.ssh/config
Step 3
Insert the variables you need into the ~/.ssh/config
file.
The basic variables are:
Host
This is the shortcut variable you will type after ssh
, and should be something descriptive and memorable, e.g. webserver
HostName
This is the IP address of the server to which you want to connect
User
This is the username you will use to connect
Identity File
This is the path of any SSH key you need (if you’re using your machine’s default key, rather than a key saved elsewhere within your folder structure, use ~/.ssh/id_rsa
instead)
Port
If you’re not using port 22, which is default for ssh, put in the alternative port number here
Advanced users can find the full list of config
variables here.
Here is an example line:
Host example-server
HostName 123.456.789.101
User ubuntu
IdentityFile /Users/ExampleUser/sshkeys/serverkey.pem
Port 3400
Step 4
Add any other servers you want to be able to access quickly. You can add as many lines as you need within the config
file. For example:
Host example-server
HostName 123.456.789.101
User ubuntu
IdentityFile /Users/macuser/sshkeys/serverkey.pem
Port 18166
Host example-server2
HostName 123.456.789.102
User root
IdentityFile /Users/macuser/sshkeys/serverkey2.pem
In the above example, the example-server
entry would connect to port 18166, while example-server2
would connect to the default port, which is 22.
Step 5
Save the config
file.
Step 6
Before trying it out, make sure that the config
file has the correct permissions:
chmod 600 ~/.ssh/config
Step 7
Now, to connect to your server you can just type:
ssh example-server
Much better.