SSH & key-based authentication
Log in with SSH keys, copy files securely and use an SSH config file.
By Invicly Tech, , in Linux Administration (RHCSA-style). About 11 minutes to read.
In short
SSH gives you an encrypted remote shell and file transfer. With key-based authentication, the server stores your public key and you prove you hold the matching private key, so no password crosses the network. A ~/.ssh/config file saves typing for hosts you use often.
Explained simply
SSH (Secure Shell) lets you type commands on a server far away as if you sat in front of it. Everything that travels between your laptop and the server is encrypted, so people on the same network cannot read your commands or passwords.
You can log in with a password, but keys are safer and faster. A key pair is two linked files. The private key stays on your laptop and must never be shared. The public key is like a lock you can hand out freely: you put it on every server you want to enter.
When you connect, the server checks that you hold the private key that matches a public key it trusts. It does this with a math puzzle, so the private key itself never leaves your machine. Even if the server is fake, it learns nothing it can reuse.
SSH also moves files. scp copies a file, and rsync copies whole folders and only sends the parts that changed. A small file called ~/.ssh/config lets you give servers short names, so ssh web01 replaces a long command full of options.
Press Play on a diagram to walk through it one step at a time.
- Client to Server: TCP connect to port 22, key exchange1TCP connect to port 22, key exchange
- Server to Client: Host key: client checks known_hosts2Host key: client checks known_hosts
- Client to Server: Offer public key for user ana3Offer public key for user ana
- Server to Client: Key is in authorized_keys: prove it4Key is in authorized_keys: prove it
- Client to Server: Signature made with the private key5Signature made with the private key
- Server to Client: Signature verified, session opened6Signature verified, session opened
- Laptop ~/.ssh
- id_ed25519 (600, private)
- id_ed25519.pub (public)
- known_hosts (trusted servers)
- config (host shortcuts)
- Server ~/.ssh
- Directory mode 700
- authorized_keys mode 600
- Home not writable by others
- Server /etc/ssh
- ssh_host_*_key (host keys)
- sshd_config and sshd_config.d
In depth
Two kinds of keys: host keys and user keys
SSH uses keys in both directions. First the server proves who it is with its host key, stored in /etc/ssh/ssh_host_*_key. The first time you connect, ssh shows the key fingerprint and asks if you trust it. If you say yes, it saves the key in ~/.ssh/known_hosts. Next time, a different key triggers a loud warning, because someone may be pretending to be the server.
Then you prove who you are with your user key. The server looks in ~/.ssh/authorized_keys of the account you log in as. Each line there is one public key, optionally with options in front, such as from="10.0.0.0/8" to accept it only from one network.
- ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub shows a host key fingerprint
- ssh-keygen -R web01 removes an old entry after a server is rebuilt
- Compare fingerprints out of band before you type yes
- Proves the server is real
- Lives in /etc/ssh on the server
- Client saves it in known_hosts
- Mismatch: possible attack or rebuild
- Proves who you are
- Private key lives in ~/.ssh on your laptop
- Server lists it in authorized_keys
- Mismatch: falls back to the next method
Choosing a key type and protecting it
Use ed25519 for new keys. It is short, fast and supported by every current OpenSSH. Only use RSA (at least 3072 bits) when an old device cannot handle ed25519. ssh-keygen -t ed25519 writes ~/.ssh/id_ed25519 (private, mode 600) and id_ed25519.pub (public).
Always set a passphrase. It encrypts the private key on disk, so a stolen laptop or backup does not give away server access. To avoid typing it all day, ssh-agent keeps the unlocked key in memory: run ssh-add once per session. The -C option only adds a comment, such as your name and machine, to help you recognise the key later in authorized_keys.
- ssh-keygen -p -f ~/.ssh/id_ed25519 changes or adds a passphrase
- ssh-add -l lists the keys the agent currently holds
- Use one key per laptop, so you can revoke one device alone
What ssh-copy-id actually does
ssh-copy-id logs in once with a method that already works, usually a password, and appends your public key to ~/.ssh/authorized_keys on the server. It creates ~/.ssh with mode 700 and the file with mode 600 if they are missing, and it skips keys that are already there.
Use -i to choose which key to send, for example ssh-copy-id -i ~/.ssh/id_ed25519.pub ana@web01. If password login is already disabled, you cannot use ssh-copy-id; an admin must add the line for you, or a cloud provider injects it at first boot. Doing it by hand is simply: append the .pub line, then fix the modes.
scp, sftp and rsync
All three ride on SSH, so they use the same keys and config. scp copies files like cp: scp report.pdf web01:/tmp/. Since OpenSSH 9.0, scp uses the SFTP protocol underneath, which fixed old quoting surprises. sftp gives an interactive file session. rsync is the tool for folders: it compares both sides and sends only what changed, so a second run is fast.
Watch the port flag: ssh and sftp use -p (small p) but scp uses -P (capital). For rsync, pass the SSH options with -e, for example rsync -av -e "ssh -p 2222" src/ host:dst/. A trailing slash on the source means copy the contents, not the folder itself.
- scp -r dir web01:/srv/ copies a folder
- rsync -avP resumes big transfers and shows progress
- rsync -avn ... is a dry run that changes nothing
How ~/.ssh/config is read
ssh reads your config from top to bottom and, for each option, keeps the first value it finds. So put specific Host blocks first and a general Host * block at the end. Host patterns can use wildcards, such as Host web-*.
Useful options: HostName (the real address), User, Port, IdentityFile (which key) and IdentitiesOnly yes, which stops ssh offering every key in the agent. Too many offered keys can hit the server's MaxAuthTries limit and fail. ProxyJump bastion reaches an internal host through a jump server in one step. The file must not be writable by other users, or ssh refuses to use it.
- 1Match Host web01
Take HostName, User, IdentityFile
- 2Match Host *
Fill in options not set yet
- 3Jump if needed
ProxyJump connects via the bastion first
- 4Check host key
Against known_hosts
- 5Offer the chosen key
Session opens on success
Learn it in 4 short steps
About 40m each. Track them with a timer in your plan.
- 1Generate an ed25519 key pair~40m
- 2Install keys with ssh-copy-id~40m
- 3Copy files with scp and rsync~40m
- 4Write ~/.ssh/config host entries~40m
Key points
- ssh-keygen -t ed25519 creates a modern key pair; protect it with a passphrase.
- ssh-copy-id user@host appends your public key to the server's authorized_keys.
- ~/.ssh must be 700 and authorized_keys 600, or sshd ignores the key.
- ssh-agent holds unlocked keys so you type the passphrase once.
Key terms
Tap a card to flip it. 0/5 known
Examples
ssh-keygen -t ed25519 -C "ana@laptop"
ssh-copy-id ana@web01
cat >> ~/.ssh/config <<EOF
Host web01
HostName 192.168.1.50
User ana
EOF
ssh web01 'hostname; uptime'
rsync -av -e ssh ./site/ web01:/srv/web/Create a key, install it, add a config alias and copy files.
Reach a private server through a bastion
The database host db01 (10.0.2.15) has no public IP. Only bastion.example.com is reachable from the internet. Step 1: put both hosts in ~/.ssh/config, with db01 using ProxyJump. Step 2: run ssh db01; ssh first connects to the bastion, then opens a tunnel from there to db01. Your private key never gets copied to the bastion. Step 3: scp and rsync use the same config, so file copies to db01 just work too.
cat >> ~/.ssh/config <<'EOF'
Host bastion
HostName bastion.example.com
User ana
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
Host db01
HostName 10.0.2.15
User ana
ProxyJump bastion
EOF
chmod 600 ~/.ssh/config
ssh db01 hostname
rsync -av ./dumps/ db01:/var/tmp/dumps/A server was rebuilt and SSH shouts a warning
web01 was reinstalled, so it has new host keys. ssh now prints REMOTE HOST IDENTIFICATION HAS CHANGED and refuses to connect. Step 1: confirm with the person who rebuilt it, or the cloud console, that the new fingerprint is expected. Step 2: remove the old entry. Step 3: connect again, check the fingerprint shown matches, and accept. Never just delete all of known_hosts; that throws away protection for every other server.
# on the server console: get the real fingerprint
ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub
# on your laptop
ssh-keygen -R web01
ssh-keygen -R 192.168.1.50
ssh web01 # compare the fingerprint, then type yesQuick reference
- ssh-keygen -t ed25519 -C "ana@laptop" creates a modern key pair
- ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host installs one specific key
- ssh -p 2222 host but scp -P 2222 file host: (capital P for scp)
- eval "$(ssh-agent -s)" then ssh-add starts an agent and loads your key
- ssh -v host shows which keys are offered and why login fails
- ssh-keygen -R host removes a stale known_hosts entry
- In ~/.ssh/config the first value wins: specific Host blocks first, Host * last
- rsync -avn src/ host:dst/ is a dry run; drop n to copy for real
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Create an ed25519 key with a passphrase and print its fingerprint and public key.
- Exercise 2medium
Install your key on a test server, add a config alias called lab, and prove password login is no longer needed.
- Exercise 3hard
Allow a backup key to log in from 10.0.0.5 only and run just one command, with no shell and no forwarding.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Is ed25519 better than RSA?
- For new keys, yes. ed25519 keys are short, fast and secure, and every current OpenSSH supports them. Use RSA with 3072 or more bits only for old systems that lack ed25519.
- Can I use the same key on many servers?
- Yes, that is normal: the public key is not secret. Use a separate key per laptop or person, so you can revoke one without touching the others.
- Why does SSH still ask for my password after I copied my key?
- Usually the server ignores the key because ~/.ssh or authorized_keys is too open, or the wrong key is offered. Run ssh -v to see which keys are tried, and check the server log with journalctl -u sshd.
- What do I do if my private key is stolen?
- Remove its public key line from authorized_keys on every server, create a new key pair, and install the new public key. A passphrase gives you time, but do not rely on it.