Skip to content

SSH, scp & rsync

This page is the quick reference for daily work on a remote machine: a university cluster, a lab server, or a cloud instance. The SSH & Remote Servers guide explains each step from the start.

Task Command
Connect ssh user@host
Connect with an alias ssh hpc
Connect through a bastion ssh -J bastion worker
Create a key ssh-keygen -t ed25519 -C "comment"
Install a key on a server ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host
Forget a changed host key ssh-keygen -R host
Forward Jupyter ssh -N -L 8888:localhost:8888 hpc
Upload with scp scp file hpc:/path/
Download with scp scp hpc:/path/file .
Sync a directory rsync -avh src/ hpc:/path/
Dry run rsync -avhn src/ dest/
Resume with progress rsync -avhP src/ dest/
Exclude a pattern rsync -avh --exclude='*.bam' src/ dest/
Mirror with deletion rsync -avh --delete src/ dest/
Terminal window
# Connect as a user
ssh user@hostname
# Connect on another port
ssh -p 2222 user@hostname
# Connect with a specific private key
ssh -i ~/.ssh/cluster_key user@hostname
# Print each step of the connection, to debug a failure
ssh -v user@hostname

Add more v letters, up to -vvv, for more detail.

Terminal window
# Create a key pair; ed25519 is the current default choice
ssh-keygen -t ed25519 -C "you@lab.example"
# Install the public key on the server
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@hostname
# Lock down the directory and the private key
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
# Load the key into an agent, so you type the passphrase once
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
# List the keys the agent holds
ssh-add -l

SSH ignores a private key that other users can read and prints UNPROTECTED PRIVATE KEY FILE. The chmod lines above fix that. Give the private key a passphrase; the agent keeps the unlocked key in memory for the session.

Define each host once and connect with its short name. ssh hpc, scp file hpc:, and rsync -a dir/ hpc: all read the same entry.

Host hpc
HostName hpc.example.edu
User jdoe
IdentityFile ~/.ssh/id_ed25519
ServerAliveInterval 15
ServerAliveCountMax 4
# Reuse one connection, so a two-factor prompt appears only once
ControlMaster auto
ControlPath ~/.ssh/cm-%C
ControlPersist 4h
Host bastion
HostName bastion.example.org
User ec2-user
IdentityFile ~/.ssh/aws.pem
Host worker
HostName 10.0.0.5
User ec2-user
IdentityFile ~/.ssh/aws.pem
ProxyJump bastion

ServerAliveInterval makes a dead connection fail after about a minute instead of hanging. ProxyJump connects to worker through bastion in one step, and it replaces the older ProxyCommand recipe.

Run Jupyter or RStudio Server on the remote machine and open it in the browser on your laptop.

Terminal window
# Laptop port 8888 reaches port 8888 on the login node
ssh -N -L 8888:localhost:8888 hpc
# Laptop port 8888 reaches port 8888 on compute node node042, through hpc
ssh -N -L 8888:node042:8888 hpc
# RStudio Server
ssh -N -L 8787:localhost:8787 hpc
# Reverse: port 9000 on the server reaches port 8080 on the laptop
ssh -N -R 9000:localhost:8080 hpc

-N opens the tunnel without a remote shell, and the tunnel stays up until you press Ctrl+C. Then open http://localhost:8888 on the laptop. A notebook started inside a batch job runs on a compute node, so forward to that node’s name, as in the second line.

Terminal window
# Your jobs in the queue
ssh hpc 'squeue -u $USER'
# Disk use of each item in your scratch directory
ssh hpc 'du -sh /scratch/$USER/*'

Single quotes send $USER to the remote shell, which expands it to the remote user name. In double quotes, the laptop’s shell expands it first, and the two names often differ. For a command that must survive a dropped connection, start it inside tmux on the remote.

scp copies a few files. It always sends every byte again.

Terminal window
# Upload a file
scp sample_001.fastq.gz hpc:/scratch/jdoe/raw/
# Download a file to the current directory
scp hpc:/scratch/jdoe/results/counts.tsv .
# Copy a directory
scp -r results/ hpc:/scratch/jdoe/
# Copy through a jump host
scp -J bastion report.html worker:/tmp/

rsync sends only the files, and the parts of files, that differ between source and destination. That makes it the tool for sequencing data, for a re-run after an interruption, and for keeping a backup current. It uses SSH for a remote path without any extra flag.

Terminal window
# Copy a directory to the cluster
rsync -avh raw_fastq/ hpc:/scratch/jdoe/raw_fastq/
# Dry run: list what would be copied, copy nothing
rsync -avhn raw_fastq/ hpc:/scratch/jdoe/raw_fastq/
# Show progress and keep partial files, so a re-run resumes
rsync -avhP raw_fastq/ hpc:/scratch/jdoe/raw_fastq/
# Leave out BAM files
rsync -avh --exclude='*.bam' results/ hpc:/scratch/jdoe/results/
# Compare files by checksum, not by size and time
rsync -avhc results/ /mnt/backup/results/
# Mirror: also delete destination files that the source does not have
rsync -avh --delete results/ /mnt/backup/results/
Flag Meaning
-a Archive: recursive, keeps permissions, times, and symlinks; keeps owners only when the receiver runs as root
-v Name each file as it is copied
-h Print sizes in K, M, and G
-n Dry run
-P --partial --progress: keep partial files and show progress
-z Compress in transit; helps on a slow link, not on gzipped FASTQ
-c Decide what to copy by checksum
--exclude=PATTERN Skip matching files
--delete Delete destination files that are not in the source
Terminal window
# Copies the contents of src into dest
rsync -av src/ dest/
# Copies the directory src itself, giving dest/src/
rsync -av src dest/

A slash on the destination changes nothing. A dry run with -n shows the layout before a large copy.

Symptom Fix
Permission denied (publickey) The server does not have your public key, or the config points to another key; run ssh-copy-id or check User and IdentityFile
UNPROTECTED PRIVATE KEY FILE Run chmod 600 on the private key and chmod 700 on ~/.ssh
The session freezes after a pause Add ServerAliveInterval and ServerAliveCountMax for the host
REMOTE HOST IDENTIFICATION HAS CHANGED Confirm with the administrator that the server was rebuilt, then remove the old key with ssh-keygen -R hostname
A copy of many small files is slow Use rsync, or pack the directory with tar first
An rsync stopped halfway Run the same command again; with -P it reuses the partial file
  • SSH & Remote Servers teaches keys, the config file, scp, and rsync step by step.
  • tmux keeps a remote shell alive after the connection drops.
  • SLURM covers submitting jobs once you are on the cluster.
  • Setting Up AWS launches an EC2 instance and connects to it over SSH with a key pair.

The OpenSSH configuration manual defines identity files, jump hosts, connection reuse, and keepalive settings. The ssh manual explains local and reverse forwarding, including which machine opens the destination connection.

The scp manual describes copy operands and jump-host options. The rsync manual distinguishes archive preservation, partial files, checksum comparison, and source trailing slashes. These options control different parts of a transfer.