SSH, or "secure shell protocol," is the most common method by which a local computer is used to connect to -- and give instructions to -- a remote computer. There are a number of different circumstances in which SSH can be useful, among them:
- when you need to control a computer that is located in a different building, or within a data center;
- when you need to control a computer that does not have a screen (e.g. a headless server or Raspberry Pi);
- when you need to control a computer whose operating system does not have a graphical user interface (GUI)
This guide will explain how you can use the command line on your computer to connect to a computer running SSH.
Prerequisites
- You will need an SSH-capable client on your local computer, from which to initiate the connection
- In macOS you can either use the Terminal application, which is preinstalled, or, alternatively, you can use Home of the Future’s recommended equivalent, iTerm2;
- In Linux, you can use Terminal, which is pre-installed;
- On Windows, you can use Putty, which is not pre-installed but is free to download
- You will need to ensure that SSH is enabled on the remote computer — the one to which you are trying to connect — and to know on which port it is listening (typically, this is port
22
) - If you are connecting using a username and password, you will need know that username and password. If you are connecting using an SSH key, you will need to have that key installed on your local machine
- You will need to know the IP address, hostname, or domain name of the remote computer to which you are trying to connect
Connecting using a username and password
In this example, let’s assume the following:
- The remote computer to which you are trying to connect is located at IP address
1.2.3.4
; - The username you will use to connect is
charles
; - The password you will use to connect is
hotf1
; - The remote computer is listening for SSH connections on port
22
, which is the default
To initiate a connection, you’d type:
ssh charles@1.2.3.4
Having pressed Enter
, you’d see a password prompt that looks something like this:
~ ssh charles@1.2.3.4
Password:
If you entered your password correctly (in this example, hotf1
), you’d be connected.
Alternative ports
Sometimes, the remote computer listens for connections on a different port than the default. If this is the case you can add a -p
flag to your string. Using the example above, but changing the port to 18166
, that would mean you’d run:
ssh charles@1.2.3.4 -p 18166
Connecting using an SSH key
In some circumstances, the remote machine will be accessible via a set of SSH keys rather than (or in addition to) a username and password. This is the case, for example, with Amazon Web Services’ cloud-based EC2 instances.
In this example, let’s assume the following:
- The remote computer to which you are trying to connect is located at IP address
1.2.3.4
; - The username you will use to connect is
charles
; - The private key you will use to connect is located at
/Users/charles/Key.pem
- The remote computer is listening for SSH connections on port
22
To initiate a connection, you’d run:
ssh -i /Users/charles/Key.pem charles@1.2.3.4
Because you are using a key, there should be no password prompt. If there is, one of your variables is incorrect.
NOTE If you are using the default private key on your computer — i.e. the one saved at ~/.ssh/id_rsa
— you do not need to reference it in the string. Instead, you would simply run:
ssh charles@1.2.3.4
Set up shortcuts
On macOS, Linux, and Raspberry Pi, you can set up SSH shortcuts so that you do not need to remember these strings each time you connect. A full guide explaining how to do this can be found here.
For Windows users looking for something a little more polished than Putty, I recommend the Bitvise SSH client. It's free, although not open source.