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.
Quick reference
Section titled “Quick reference”| 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/ |
Connect
Section titled “Connect”# Connect as a userssh user@hostname
# Connect on another portssh -p 2222 user@hostname
# Connect with a specific private keyssh -i ~/.ssh/cluster_key user@hostname
# Print each step of the connection, to debug a failuressh -v user@hostnameAdd more v letters, up to -vvv, for more detail.
# Create a key pair; ed25519 is the current default choicessh-keygen -t ed25519 -C "you@lab.example"
# Install the public key on the serverssh-copy-id -i ~/.ssh/id_ed25519.pub user@hostname
# Lock down the directory and the private keychmod 700 ~/.sshchmod 600 ~/.ssh/id_ed25519
# Load the key into an agent, so you type the passphrase onceeval "$(ssh-agent -s)"ssh-add ~/.ssh/id_ed25519
# List the keys the agent holdsssh-add -lSSH 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.
The ~/.ssh/config file
Section titled “The ~/.ssh/config file”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 bastionServerAliveInterval 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.
Port forwarding
Section titled “Port forwarding”Run Jupyter or RStudio Server on the remote machine and open it in the browser on your laptop.
# Laptop port 8888 reaches port 8888 on the login nodessh -N -L 8888:localhost:8888 hpc
# Laptop port 8888 reaches port 8888 on compute node node042, through hpcssh -N -L 8888:node042:8888 hpc
# RStudio Serverssh -N -L 8787:localhost:8787 hpc
# Reverse: port 9000 on the server reaches port 8080 on the laptopssh -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.
Run a command on the remote
Section titled “Run a command on the remote”# Your jobs in the queuessh hpc 'squeue -u $USER'
# Disk use of each item in your scratch directoryssh 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.
# Upload a filescp sample_001.fastq.gz hpc:/scratch/jdoe/raw/
# Download a file to the current directoryscp hpc:/scratch/jdoe/results/counts.tsv .
# Copy a directoryscp -r results/ hpc:/scratch/jdoe/
# Copy through a jump hostscp -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.
# Copy a directory to the clusterrsync -avh raw_fastq/ hpc:/scratch/jdoe/raw_fastq/
# Dry run: list what would be copied, copy nothingrsync -avhn raw_fastq/ hpc:/scratch/jdoe/raw_fastq/
# Show progress and keep partial files, so a re-run resumesrsync -avhP raw_fastq/ hpc:/scratch/jdoe/raw_fastq/
# Leave out BAM filesrsync -avh --exclude='*.bam' results/ hpc:/scratch/jdoe/results/
# Compare files by checksum, not by size and timersync -avhc results/ /mnt/backup/results/
# Mirror: also delete destination files that the source does not haversync -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 |
The trailing slash on the source
Section titled “The trailing slash on the source”# Copies the contents of src into destrsync -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.
Common problems
Section titled “Common problems”| 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 |
See also
Section titled “See also”- 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.
Further reading
Section titled “Further reading”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.