Why
Using SSH public/private keys is more secure than using a password. It also makes it easier and faster to connect to your server because you don’t have to enter a password.How It Works
Public/private keys work by using a pair of keys to verify identity:- One key, the public key, can only encrypt data, not decrypt it
- The other key, the private key, can decrypt the data
~/.ssh/authorized_keys on the server you’re connecting to. Notice the file is in the home folder of the ID you’re trying to connect to.
After the keys have been created and the public key has been appended to ~/.ssh/authorized_keys on the host, SSH uses the public and private keys to verify identity and then establish a secure connection. Identity is verified by the server encrypting a challenge message with the public key, then sending it to the client. If the client cannot decrypt the challenge message with the private key, the identity can’t be verified and a connection will not be established.
They are considered more secure because you need the private key to establish an SSH connection. If you set PasswordAuthentication no in /etc/ssh/sshd_config, then SSH won’t let you connect without the private key.
Why Ed25519?
We will be using Ed25519 keys which, according to linux-audit.com:It is using an elliptic curve signature scheme, which offers better security than ECDSA and DSA. At the same time, it also has good performance.
Goals
- Ed25519 public/private SSH keys:
- Private key on your client
- Public key on your server
You’ll need to do this step for every computer and account you’ll be connecting to your server from/as.
Steps
Generate the SSH key pair
From the computer you’re going to use to connect to your server (the client, not the server itself), create an Ed25519 key with You’ll see output like:
ssh-keygen:About Passphrases: If you set a passphrase, you’ll need to enter it every time you connect to your server using this key, unless you’re using
ssh-agent.ssh-agent is a program that is shipped in many Linux distros (and usually already running) that will allow you to hold your unencrypted private key in memory for a configurable duration. Simply run ssh-add and it will prompt you for your passphrase. You will not be prompted for your passphrase again until the configurable duration has passed.Copy the public key to your server
Now you need to append the public key You’ll see output like:
~/.ssh/id_ed25519.pub from your client to the ~/.ssh/authorized_keys file on your server.Since we’re presumable still at home on the LAN, we’re probably safe from MIM (Man-in-the-middle) attacks, so we will use ssh-copy-id to transfer and append the public key:References
- SSH Public Key Authentication
- Ubuntu SSH Keys Guide
- Using Ed25519 OpenSSH Keys
- Understanding SSH Encryption and Connection Process
- Arch Linux SSH Keys Wiki
- ssh-copy-id Documentation
man ssh-keygenman ssh-copy-idman ssh-add