How to Setup SSH Key Authentication on VPS (Ubuntu/Debian)
Secure your VPS by logging in with SSH Keys instead of passwords. This method works on Ubuntu, Debian, and similar Linux distributions.
Prerequisites
-
VPS with root SSH access.
-
A local Linux, macOS, or Windows with SSH client (e.g., PuTTY/Terminal).
Steps to Setup SSH Key Authentication
1. Generate SSH Key on Local Machine
Run the following on your local system:
ssh-keygen -t rsa -b 4096 -C "[email protected]"
-
Press Enter to accept default file location.
-
Optionally, set a passphrase.
The key will be saved in:
~/.ssh/id_rsa (private key)
~/.ssh/id_rsa.pub (public key)
2. Copy Public Key to VPS
Use ssh-copy-id
(Linux/macOS) to upload the key:
ssh-copy-id root@your_server_ip
If ssh-copy-id is unavailable (Windows):
Manually copy the contents of id_rsa.pub
and paste it into your VPS:
ssh root@your_server_ip
mkdir -p ~/.ssh
nano ~/.ssh/authorized_keys
# Paste the public key here, save, and exit.
chmod 600 ~/.ssh/authorized_keys
3. Test SSH Key Login
Disconnect and reconnect:
ssh root@your_server_ip
If successful, you won't be asked for a password.
4. (Optional) Disable Password Login
For enhanced security, follow this guide: Disable Password Login on VPS (Ubuntu/Debian)
Notes
-
Never share your private key (
id_rsa
). -
Always keep a backup of your SSH key.