Linux Command Line · Lesson 35 of 43
Working on remote machines with ssh, scp and rsync
Log in to remote Linux machines with ssh, create and use key pairs, copy files with scp and rsync, and set up a config file for easy connections.
UNDERSTAND IT. THEN TRY IT.
What you can do after this lesson
You will connect to a server, log in without typing a password each time, and move files in both directions. Almost every server you ever manage is reached with ssh, and scp and rsync ride on top of it.
Most of this lesson needs a remote server, which our practice machine does not have, so those commands are shown as text. Creating a key pair works locally, and we run that part for real.
ssh: a shell on another machine
ssh (secure shell) opens an encrypted session on a remote machine. Give it a user name and a host:
ssh asha@203.0.113.10If the server listens on a different port, add -p: ssh -p 2222 asha@203.0.113.10. The first time you connect, ssh asks whether you trust the server's fingerprint:
The authenticity of host '203.0.113.10' can't be established.
ED25519 key fingerprint is SHA256:abc...
Are you sure you want to continue connecting (yes/no)?Check the fingerprint against one given by the provider, if you can, and type yes. ssh remembers it in ~/.ssh/known_hosts. If it later changes unexpectedly, ssh warns you loudly: that can mean a rebuilt server, or someone intercepting the connection. Do not just delete the warning; find out why.
You can also run one command and return immediately:
ssh asha@203.0.113.10 'df -h /'Leave an interactive session with exit or Ctrl+D.
Key pairs instead of passwords
Passwords can be guessed and are tiresome to type. An SSH key pair is better. It has two parts: a private key, which stays secret on your computer, and a public key, which you place on every server you want to enter. The server uses the public key to challenge your client; only the matching private key can answer, and the private key never travels over the network.
Create a pair with ssh-keygen. We make one here in the practice folder, without a passphrase only because it is a throwaway demonstration:
ssh-keygen -q -t ed25519 -N "" -f demo_key -C "demo"
ls demo_key demo_key.pub
stat -c '%a %n' demo_key demo_key.pub
cut -d' ' -f1 demo_key.pubdemo_key
demo_key.pub
600 demo_key
644 demo_key.pub
ssh-ed25519-t ed25519 selects a modern, fast and secure algorithm, -f names the file, and -C adds a comment. The private key demo_key got mode 600 automatically, and the public one is readable by others, which is fine. On your real key, always set a passphrase when ssh-keygen asks for one, so a stolen file is useless. An agent, started with ssh-add, remembers the passphrase for your session so you do not retype it. On macOS, ssh-add --apple-use-keychain stores it in the keychain.
Installing the public key on a server
The public key goes into ~/.ssh/authorized_keys on the server. The helper ssh-copy-id does this, asking for your password one last time:
ssh-copy-id -i ~/.ssh/id_ed25519.pub asha@203.0.113.10After that, ssh asha@203.0.113.10 logs you in without a password. Once keys work, administrators often turn password login off (PasswordAuthentication no in /etc/ssh/sshd_config) so that bots cannot guess their way in. Test the key login in a second terminal before changing that, so you cannot lock yourself out.
Never share your private key, never paste it into chats or commit it to Git. If it leaks, remove its public half from every server and make a new pair. ssh refuses to use a private key readable by others; fix that with chmod 600 ~/.ssh/id_ed25519, and keep the folder private with chmod 700 ~/.ssh.
scp: copy files
scp (secure copy) works like cp, but either side can be remote, written as user@host:path:
scp report.pdf asha@203.0.113.10:/home/asha/
scp asha@203.0.113.10:/var/log/app.log .
scp -r project asha@203.0.113.10:/home/asha/backup/The first command uploads a file, the second downloads one into the current folder, and -r copies a whole folder. The port option for scp is a capital -P, unlike the lowercase -p of ssh, which is an easy mistake. Mind the overwrite behaviour: like cp, scp replaces existing files without asking.
rsync: copy only the differences
rsync is the better tool for repeated copies and backups. It compares both sides and transfers only what changed, it can resume an interrupted copy and it preserves permissions and times with -a. First, a safe local demonstration with -n (dry run), which shows what would happen without copying anything:
cd ~/project
mkdir -p mirror
rsync -avn docs/ mirror/ | sed -n '2,4p'./
notes.txt
tasks.txtTake note of the trailing slash on docs/: it means "the contents of docs", while docs without a slash would create mirror/docs. This detail is the most common rsync surprise. To a remote server, the syntax is the same as scp:
rsync -avz --progress project/ asha@203.0.113.10:/home/asha/backup/-a is archive mode (recursive, keeps permissions and times), -v is verbose and -z compresses on the way. Add --delete to make the destination an exact mirror by removing files that no longer exist in the source, but preview it with -n first, because that option deletes.
The config file
Typing long commands every time is tiresome. ~/.ssh/config gives each server an alias:
Host web
HostName 203.0.113.10
User asha
Port 2222
IdentityFile ~/.ssh/id_ed25519Now ssh web, scp file web:/tmp/ and rsync -a dir/ web:backup/ all use those settings. Keep this file private (chmod 600 ~/.ssh/config).
Tunnels and jumps, briefly
ssh -L 8080:localhost:80 web forwards port 8080 on your machine to port 80 on the server, which is a safe way to look at an internal web page. ssh -J bastion web connects to web through a jump host. Both are built on the same connection and need no extra software.
Common mistakes
Copying the private key. Only the .pub file goes to servers.
Wrong permissions. A private key that others can read makes ssh refuse to use it, and a ~/.ssh folder on the server that others can write to makes sshd ignore your authorized_keys file.
Disabling passwords before testing keys. Keep one session open until a second one works.
Forgetting the trailing slash in rsync. docs/ and docs mean different things.
Using --delete without a dry run. Check with -n first.
Exercise
Create a key pair named lab_key with a passphrase, and check its permissions with stat. If you have access to a test server, copy the public key with ssh-copy-id -i and log in. Write a ~/.ssh/config entry for it, then upload a file with scp and mirror a folder with rsync -avn before running it for real.
KNOWLEDGE CHECK
A quick check. You’ve got this.
Try each question. Every answer comes with an explanation.
Ready for the next small win?
Mark the lesson complete when you are ready.