ssh
April 16, 2025
ssh
, short for “secure shell”, is a protocol to make an encrypted connection to a remote server. Per the name, the most common use case is to connect via shell, but it is also used by applications like git
, scp
, etc. to make secure connections and transfer data.
Using SSH
You initiate a connection to a server over ssh
by opening a terminal and running
ssh <username>@<server>
for example
ssh mike@example.com
Depending on the server configuration, you will be prompted for your password for the account you have on the server. Upon successfuly authentication, a shell session on the remote server will appear in your terminal, and you can interact as you would in a normal terminal session.
To exit an ssh
session, simply type
exit
Generating a Key
ssh
uses public key cryptography. Generating an ssh
key is as simple as running
ssh-keygen
This will put a public key at ~/.ssh/id_rsa.pub
and a private key under ~/.ssh/id_rsa
. You can choose when you create the key to add a passphrase. The public key is the one shared with the server you want to connect to. The private key should always remain private.
Connecting to a Server Using Your Key
To connect to a server with an ssh key for the first time, run
ssh-copy-id <username>@<server>
e.g.
ssh-copy-id mike@example.com
This will log you into the remote server and put your public key fingerprint in ~/.ssh/authorized_keys
. From then on, you can simply log in without being prompted for a password.
To remove a known host, simply run ssh-keygen -R <domain>
.