How to set up SSH Key Authentication and disable Password Login on Linux

Configurare noua (How To)

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

  1. 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).

  1. Copy the public key to the server:
ssh-copy-id user@server_address
  1. 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.

  1. On the server, open the SSH configuration file:
sudo nano /etc/ssh/sshd_config
  1. Set or change the following lines:
PubkeyAuthentication yes
PasswordAuthentication no
  1. Save the file (Ctrl+O, Enter, Ctrl+X) and check the syntax:
sudo sshd -t
  1. Restart the SSH service:
sudo systemctl restart ssh

(on CentOS/RHEL/Fedora the service is called sshd)

  1. 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.

Tip solutie

Permanent

Voteaza

(4 din 6 persoane apreciaza acest articol)

Despre Autor

Cel Mai Apreciat Autor

Leave A Comment?