SSH Keys
How to generate and configure SSH keys for secure password-less access
SSH keys are more secure than passwords and more convenient: no more credential entry at each access.
How It Works
You generate a key pair:
- Private key: stays on your computer (never share)
- Public key: goes on server
1. Generate Key Pair
On Linux / macOS / Windows (PowerShell):
ssh-keygen -t ed25519 -C "my-server-key"You'll be asked where to save key (press Enter for default ~/.ssh/id_ed25519) and optional passphrase.
2. Copy Public Key to Server
ssh-copy-id root@SERVER_IPIf SSH port is different from 22:
ssh-copy-id -p PORT root@SERVER_IPManual alternative (if ssh-copy-id not available):
cat ~/.ssh/id_ed25519.pub | ssh root@SERVER_IP "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"3. Verify It Works
ssh root@SERVER_IPIf connection works without password, keys are configured correctly.
4. Disable Password Access (optional but recommended)
Once verified keys work, disable login via password:
nano /etc/ssh/sshd_configFind and set:
PasswordAuthentication no
PermitRootLogin prohibit-passwordRestart SSH:
systemctl restart sshdBefore disabling password access, ensure SSH keys work correctly. Otherwise you risk locking yourself out.
Manage Multiple Keys (SSH Config)
If you have multiple servers, create ~/.ssh/config to manage them easily:
Host my-vps
HostName SERVER_IP
User root
Port 22
IdentityFile ~/.ssh/id_ed25519
Host other-server
HostName OTHER_SERVER_IP
User ubuntu
Port 2222
IdentityFile ~/.ssh/id_ed25519Then access simply with:
ssh my-vps