Situatie
Key-based authentication is more secure than passwords and blocks brute-force attacks against your server.
Requirements: SSH access to the server with an account that has sudo privileges.
Solutie
- On your local computer (the client), generate a key pair:
ssh-keygen -t ed25519 -C "email@example.com"
Press Enter to accept the default location and set a passphrase (recommended).
- Copy the public key to the server:
ssh-copy-id user@server_address
- Test the key-based login in a new terminal window:
ssh user@server_address
If you connect without being asked for the account password (only the key passphrase, if you set one), the key works.
- On the server, open the SSH configuration file:
sudo nano /etc/ssh/sshd_config
- Set or change the following lines:
PubkeyAuthentication yes
PasswordAuthentication no
- Save the file (Ctrl+O, Enter, Ctrl+X) and check the syntax:
sudo sshd -t
- Restart the SSH service:
sudo systemctl restart ssh
(on CentOS/RHEL/Fedora the service is called sshd)
- Open a new session and verify. A connection attempt without the key must be rejected.
Important: do not close your current SSH session until you have confirmed that the new key-based login works, otherwise you may lock yourself out of the server.
Leave A Comment?