SSH remote access¶
Practical 6.1 ssh-remote-access-annex.pdf · PDF
A real server lives in a locked room, or in a datacentre in another city. Nobody walks up to it with a keyboard. So how do you install a program on it, or read its logs, or fix a typo in its configuration?
You log in through the network, with SSH. You met SSH in Unit 3: it encrypts the whole conversation, so nobody in the middle can read your password. But here is the uncomfortable truth: any server on the Internet receives login attempts all day long, with user names like root, admin and test, from addresses all over the world. Programs try every address on the Internet with lists of common passwords. SSH encrypts the conversation. It does not make a bad password good.
What you will learn
- Why a key pair is safer than a password, with the words public key and private key
- Check a server's fingerprint before you trust it the first time
- Create an Ed25519 key pair with
ssh-keygenand install it withssh-copy-id - Write a
~/.ssh/configentry and copy files in both directions withscp - Turn off password and root login without locking yourself out
- Why the public key is not a secret, and the one file that is
The padlock and the key¶
SSH (Secure Shell) is a protocol to get a shell on another machine, encrypted, on TCP port 22.
Instead of a password, SSH can use a key pair: two files that only work together. Think of a padlock and its metal key.
- The private key is the metal key. It never leaves your computer.
- The public key is the padlock. You can give copies to anyone.
- You hang your padlock on the server: one line in the file
~/.ssh/authorized_keysof your account there. - To log in, the server asks you to prove you hold the key that fits that padlock. Your computer does the proof with the private key, and the key itself is never sent.
That proof is a simplification of a digital signature: a mathematical seal that only the private key can make and anyone with the public key can check.
flowchart LR
subgraph CLIENT ["deb-lab (your laptop)"]
PRIV["<b>Private key</b><br>id_ed25519<br>the metal key"]
end
subgraph SERVER ["ubu-srv"]
PUB["<b>authorized_keys</b><br>a copy of your public key<br>the padlock on the door"]
end
SERVER -->|"1. prove you have the key"| CLIENT
PRIV -->|"2. a signature, never the key"| PUB
PUB -->|"3. it fits: come in"| CLIENT
| Password | Key pair | |
|---|---|---|
| What travels to the server | The password itself (encrypted on the way) | A proof; the private key stays at home |
| Can it be guessed? | Yes: people choose Tramuntana2027 |
No: a random number far too large to try |
| If the server is hacked | The attacker may learn your password | The attacker gets a padlock: useless |
| Same one on ten servers | Bad idea | Fine: copy the same padlock ten times |
Ed25519¶
The kind of key you will make is Ed25519, published in 2011 by Daniel J. Bernstein and four colleagues, and part of OpenSSH since version 6.5 (January 2014). The whole public key fits in one short line:
Three parts: the type, the key, and a comment so you remember which device it belongs to.
Careful: the public key is made to be given away
You will paste your public key in Classroom today, and that is fine. The only secret is the private key, the file id_ed25519 without .pub. Never send it, never copy it to another machine, and protect it with a passphrase. If you ever paste a block starting with -----BEGIN OPENSSH PRIVATE KEY-----, that key is burnt: make a new pair.
The first connection: the fingerprint¶
The very first time you connect to a server, ssh shows you a string like SHA256:... and asks if you want to continue. That string is the server's fingerprint: a short summary of the server's own key, its identity card.
It is the only moment you can catch an impostor pretending to be your server. So compare before you say yes. On the server's console (the virt-manager window):
Then, from deb-lab, connect with ssh marta@ubu-srv and check that the fingerprint it shows is the same. Only then type yes. The client remembers it in ~/.ssh/known_hosts and will warn you loudly if it ever changes.
Host key verification failed
After restoring a snapshot or reinstalling ubu-srv, the server may have a new identity and ssh refuses to connect. If you know why it changed, remove the old entry with ssh-keygen -R ubu-srv and compare the fingerprint again.
Make your key pair and hang the padlock¶
On the client (deb-lab), not on the server:
$ ssh-keygen -t ed25519 -C "marta@deb-lab"
Generating public/private ed25519 key pair.
Your identification has been saved in /home/marta/.ssh/id_ed25519
Your public key has been saved in /home/marta/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:hGGGUivesqVqFM21bsLdZaLt0oKpvlzIES035W5YoBc marta@deb-lab
$ ls -l ~/.ssh
-rw------- 1 marta marta 399 Oct 5 20:49 id_ed25519
-rw-r--r-- 1 marta marta 95 Oct 5 20:49 id_ed25519.pub
Look at the permissions: the private key is -rw------- (600), only the owner reads it. The .pub is readable by everyone, and that is correct. When it asks, type a passphrase: without one, whoever steals your laptop gets your server too.
Now copy the padlock to the server:
$ ssh-copy-id marta@ubu-srv
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'marta@ubu-srv'"
ssh-copy-id asks for the server password one last time, and appends your public key to ~/.ssh/authorized_keys on the server. From now on, the login asks for the passphrase of your key (your desktop usually remembers it for the session), not for the server password.
Working comfortably: config and scp¶
How many things do you type to connect? A user, a name, sometimes a port and a key file. Write them once in ~/.ssh/config:
Now ssh srv is enough, and every tool that uses SSH understands srv. If it complains Bad owner or permissions on ~/.ssh/config, the chmod 600 is missing.
scp (secure copy) is cp with a machine name in front of one of the paths. The colon is what makes it remote.
| Command | What it does |
|---|---|
scp notes.txt srv: |
Local file to your home folder on the server |
scp srv:/etc/hostname . |
Server file to the folder where you are |
scp -r project srv:backup/ |
A whole folder (-r) |
ssh srv uptime |
Run one command there and come back |
Look for the colon
scp notes.txt srv (no colon) is a normal local copy: it makes a file called srv in your folder. And check your prompt: if you are inside the server, exit first before copying from the client.
Note that keys log you in; they do not make you root. ssh srv "sudo apt update" still asks for sudo's password.
Locking the door without locking yourself out¶
Your key works. But the server still accepts passwords, so the guessing programs can keep trying. Time to close that door.
Do not edit the big /etc/ssh/sshd_config. Its first active line is Include /etc/ssh/sshd_config.d/*.conf: the small files in that folder are read first, and for each option the first value found wins. Create /etc/ssh/sshd_config.d/10-tramuntana.conf:
The 10- matters: some installations already have a 50-cloud-init.conf that says PasswordAuthentication yes, and 10 is read before 50. PermitRootLogin no does not mean nobody can be root: you log in as yourself and use sudo, so the log says who did it.
Three safety steps¶
- Keep one SSH session open in a second terminal, and do not close it until the end. Sessions that are open survive the restart.
-
Test the file before restarting.
sudo sshd -tprints nothing when the file is right. With a typo: -
Restart, then test from a new terminal. If the new login fails, the old session is still there to fix it.
On a server in another city there is no console to walk to. If you close your only session and the new configuration is wrong, the server is gone. One session stays open. Always.
Check the result:
$ sudo sshd -T | grep -E "^(passwordauthentication|permitrootlogin)"
permitrootlogin no
passwordauthentication no
$ ssh -o PubkeyAuthentication=no srv
marta@ubu-srv: Permission denied (publickey).
Without the key, there is no password question at all: the list in brackets is what the server still accepts. With the key, ssh srv still gets in.
Read the log¶
Every refused login leaves a trace. On the server:
You will find your refused attempt, with the client's address (192.168.122.x) and the time. On Ubuntu the service is called ssh, not sshd. On a server facing the Internet, journalctl -u ssh | grep "Failed password" shows the guessing programs at work.
The Practical 6.1 sheet ends with a real server on the Internet: you paste your public key in Classroom, the teacher hangs it on the server, and you log in without ever having a password for it.
Key takeaways¶
- SSH gives you an encrypted shell on another machine (port 22). Encryption does not stop password guessing.
- A key pair is a padlock (public key, in
authorized_keyson the server) and a metal key (private key,id_ed25519, never leaves the client, protected by a passphrase). - Check the server's fingerprint on the first connection;
known_hostsremembers it. ssh-keygenmakes the pair,ssh-copy-idinstalls it,~/.ssh/configgives a short name,scpcopies files (the colon makes it remote).- Turn off
PasswordAuthenticationandPermitRootLoginin a drop-in file, test withsshd -t, and keep one session open while you restart.
Quick check¶
1. Which of your files goes onto the server when you set up key login?
- The content of id_ed25519.pub
- The file id_ed25519
- The file known_hosts
- The file config
The public key (.pub) is the padlock: it goes into ~/.ssh/authorized_keys on the server. The private key id_ed25519 never leaves the client.
2. The first time you connect, ssh shows a fingerprint and asks a question. What is the fingerprint for?
- To check that the server is the real one
- To choose which encryption to use
- To check that your password is strong
- To create your own key pair
The fingerprint is a short summary of the server's identity. Comparing it with the one shown on the server's console is how you know you are not talking to an impostor.
3. You type scp notes.txt srv. What happens?
- A local copy of the file named srv is made
- The file is copied to your home on the server
- The file is copied to /tmp on the server
- It fails with an error saying no such host
Without the colon after the host, scp behaves like cp. scp notes.txt srv: (with the colon) would copy it to your home folder on the server.
4. What must you do before restarting SSH with a new configuration on a remote server?
- Keep one SSH session open and test the file with sshd -t
- Close every SSH session so they pick up the change
- Delete authorized_keys so the new rules apply
- Reboot the client so it forgets the old settings
Open sessions survive the restart. If the new configuration locks you out, the open session is your way back. sshd -t catches typos before they hurt.
5. Why is a key pair safer than a password against the login attempts every Internet server receives?
- The private key is a random number far too large to guess
- Keys make the connection travel on a hidden port
- Keys hide the server's address from the attackers
- Keys are sent to the server encrypted twice
People choose guessable passwords; a private key is a huge random number and it never even travels. The server only receives a proof that you hold it.
6. After the change, ssh -o PubkeyAuthentication=no srv prints Permission denied (publickey). What does that prove?
- The server no longer accepts passwords, only keys
- Your public key was not copied to the server
- The SSH service on the server has stopped
- Root login is still allowed on the server
The list in brackets is what the server still accepts: only publickey. With the key, ssh srv still works, so the service is running.
7. Marta's laptop is stolen. Her private key had no passphrase. Select the two that are true.
- The thief can log in to every server that has her public key
- She should remove that public key line from authorized_keys
- The thief needs the server password before the key works
- Nothing changes, because private keys never travel
Without a passphrase, the private key file alone opens the door. One key pair per device makes the fix easy: delete that device's line from authorized_keys on each server.
SSH gives you a shell. But what if the thing you must fix is a window with a button? Next: Remote desktop.