SSH & Remote Access
Connect to remote machines securely with SSH keys, and copy files between them.
SSH (Secure Shell) gives you an encrypted terminal session on a remote
machine over port 22. The secure, password-free way to authenticate is with a
key pair: a private key stays on your machine; the matching public key
goes into ~/.ssh/authorized_keys on the server.
When you connect, the server challenges you using your public key, and only your
private key can answer — so no password crosses the wire. Use ~/.ssh/config to
save host aliases, users, and keys so connecting is a short, memorable command.
ssh-keygen -t ed25519 -C "you@example.com" # generate a key pair
ssh-copy-id alex@server # install your public key
ssh alex@server # connect
ssh -i ~/.ssh/id_ed25519 alex@10.0.0.5 # specify a key explicitly
scp file.txt alex@server:/tmp/ # copy a file up
scp alex@server:/var/log/app.log . # copy a file down
# ~/.ssh/config
# Host prod
# HostName 10.0.0.5
# User alex
# IdentityFile ~/.ssh/id_ed25519
ssh prod # now just this - Generate an ed25519 key pair and inspect the two files created.
- Add a
Hostalias to~/.ssh/configand connect using only the alias. - Copy a file to a remote server with
scp, then copy one back. - Explain why key-based auth is more secure than passwords.
Cheat Sheet▾
| Task | Command |
|---|---|
| Generate key | ssh-keygen -t ed25519 |
| Install public key | ssh-copy-id user@host |
| Connect | ssh user@host |
| Use specific key | ssh -i key user@host |
| Copy up / down | scp src user@host:dst |
| Sync directories | rsync -av src/ user@host:dst/ |
| Config aliases | ~/.ssh/config |
Common Interview Questions▾
How does SSH key-based authentication work?
You hold a private key; its public key is on the server. The server issues a challenge only your private key can answer, proving identity without sending a password over the network.
Which key is safe to share — public or private?
The public key. It only verifies signatures. The private key must stay secret and ideally be protected with a passphrase.
How do you copy a file to a remote server from the CLI?
scp localfile user@host:/path/ (or rsync for larger/incremental transfers).