On this page

This chapter covers connecting to other machines and moving files between them, all over the SSH protocol. If you self-host a server, these are the commands you will use every day.

ssh — connect to a remote machine

ssh opens an encrypted terminal session on a remote computer.

$ ssh [email protected]
user@server:~$

If the username is the same locally and remotely, you can omit it:

$ ssh server.example.com

To run a single command on the remote machine without opening a session:

$ ssh [email protected] "uptime"

The first time you connect you are asked to confirm the server’s host key; then you log in with your password.

Keys are better than passwords. A key pair lets you log in without typing a password:

$ ssh-keygen -t ed25519
$ ssh-copy-id [email protected]

After ssh-copy-id, this logs you straight in:

Want the full picture — fingerprints, passphrases, the agent, config nicknames? Read the SSH — From Passwords to Keys guide. Running the server yourself? The companion guide SSH — Hardening the Server secures the other end.

scp — copy files over SSH

scp copies files between machines, in either direction.

Copy a file to a remote machine:

$ scp local.txt [email protected]:/home/user/

Copy a file from a remote machine:

$ scp [email protected]:/home/user/remote.txt .

Directories need -r:

$ scp -r project/ [email protected]:/home/user/

scp uses SSH, so keys work here too — no password prompt with ssh-copy-id set up.

rsync — smart file sync

rsync copies files efficiently: it only transfers what changed, and can mirror entire directory trees. It is the best tool for backups and for deploying a website.

The basic pattern — sync a local directory into a remote one:

$ rsync -av project/ [email protected]:/var/www/
Flag Meaning
-a archive mode: recurse and preserve metadata
-v verbose: show what is transferred
-z compress during transfer
-n dry run: show what would happen, change nothing
--delete remove remote files that no longer exist locally

Always do a dry run first for --delete:

$ rsync -avn --delete project/ [email protected]:/var/www/

The trailing / on project/ matters: with it, rsync copies the contents of the directory; without it, it copies the directory itself.

The deploy pattern: building a static site locally and pushing the output to a server is exactly this:

$ rsync -avz --delete public/ [email protected]:/var/www/html/

sftp — interactive file transfer

sftp is an interactive FTP-style session over SSH — useful when you want to browse before transferring.

$ sftp [email protected]
Connected to server.example.com.
sftp> ls
sftp> put local.txt
sftp> get remote.txt
sftp> quit

Common commands inside sftp: ls, cd, put (upload), get (download), quit/exit.

A note on passwords: the old ftp and telnet sent every password unencrypted — which is exactly why the tools in this chapter replace them. ssh, scp, rsync and sftp all encrypt traffic. Type your passwords into these, never into their ancestors, and keep your SSH keys to yourself.