SSH'ing Into a New Computer

Every so often you may be given a username and password to login to a new machine. You could simply type in the username and password every time you want to ssh into a new machine, or you could automate it with ssh-copy-id and a ~/.ssh/config file. The great part about this tutorial is all commands are done from the client. Only at the very end do you issue commands on the remote to make sure it is working.

Terminology

Step 1: Generate a SSH Keypair

Run ssh-keygen. I usually use the name of the client, the username, and the YYYY-MM-DD date as the name of the keypair. eg: ~/.ssh/mbp13-dbernadett-2019-09-03 (having an iterating version number instead of a date is also a good choice)

I usually leave the password blank. Having a password can make scripting/automation with ssh a nightmare (allowing a cron job to git-pull). However, if you think someone will get access to the key (laptop left open/stolen), it is better to have a password.


david$ cd ~/.ssh
david$ ssh-keygen

This will output both a private key <name> and a public key <name>.pub

Step 2: Add new machine to ~/.ssh/config

david$ <your_favorite_text_editor> ~/.ssh/config

This is an example of my config file.

Host csil
    Hostname csil-10.cs.ucsb.edu
    User helique
    Port 22
    ServerAliveInterval 180
IdentityFile ~/.ssh/mbp13

Host github.com
    Hostname github.com
    User git
    Port 22
IdentityFile ~/.ssh/mbp13

Step 3: SSH-COPY-ID your Public

ssh-copy-id is a convenient tool to copy your public key to another computers authorized_keys file

david$ ssh-copy-id -i ~/.ssh/<name>.pub <host>

This should prompt you for a password, tell you the key has been updated, and then encourage you to ssh into the host to make sure it works

david$ ssh <host>

If the computer you are ssh'ing into does not allow passwords, you will either have to already have ssh access via a key, or will have to ask the admin to install your public key for you.