SSH

What Is SSH?

Connecting with a Password

$ ssh tut@remote.example.com
tut@remote.example.com's password:
Last login: Mon Mar  3 09:14:22 2026
tut@remote:~$

Key-Pair Authentication

Generating a Key Pair

$ ssh-keygen -t ed25519 -C "tut@example.com"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/tut/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/tut/.ssh/id_ed25519
Your public key has been saved in /Users/tut/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:abc123xyz456... tut@example.com

Copying the Public Key to a Server

$ ssh-copy-id tut@remote.example.com
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
tut@remote.example.com's password:
Number of key(s) added: 1

Host Key Verification

$ ssh tut@remote.example.com
The authenticity of host 'remote.example.com (203.0.113.5)' can't be established.
ED25519 key fingerprint is SHA256:abc123xyz456...
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'remote.example.com' to the list of known hosts.

ssh-agent

$ eval "$(ssh-agent -s)"
Agent pid 12345

$ ssh-add ~/.ssh/id_ed25519
Enter passphrase for /Users/tut/.ssh/id_ed25519:
Identity added: /Users/tut/.ssh/id_ed25519 (tut@example.com)

The SSH Config File

Host myserver
    HostName remote.example.com
    User tut
    IdentityFile ~/.ssh/id_ed25519
    ForwardAgent yes

Copying Files

$ scp local_data.csv tut@remote.example.com:~/data/

$ scp tut@remote.example.com:~/results/output.csv .
$ rsync -avz data/ tut@remote.example.com:~/data/

SSH Tunnels

$ ssh -L 5432:localhost:5432 tut@remote.example.com

Programmatic SSH with paramiko

import paramiko

client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect("remote.example.com", username="tut")

stdin, stdout, stderr = client.exec_command("ls ~/data")
for line in stdout:
    print(line.strip())

client.close()

Exercise: Keys and Config

  1. Generate an ed25519 key pair. What are the permissions on ~/.ssh/id_ed25519? Why are those permissions necessary?

  2. Add an entry to ~/.ssh/config for a machine you have access to and verify that ssh <alias> connects without specifying a username or hostname.

  3. Use scp to copy a small file to a remote machine and back. Then do the same with rsync. What differences do you notice?