Introduction
Logging into a VPS using a password is simple but it is not secure. Password-based login can be attacked using brute-force methods.
A better and more secure approach is using SSH keys.
In this guide, you will learn how to log in to your VPS using an SSH key instead of a password in a simple step-by-step way.
Table of Contents
- What is an SSH Key?
- Generating SSH Key
- Copying SSH Key to VPS
- Logging in Using SSH Key
- Disabling Password Login (Optional but Recommended)
- Conclusion
What is an SSH Key?
An SSH key is a pair of files used to securely connect to a server without a password.
It consists of:
- Private key (kept on your local machine)
- Public key (added to your VPS)
When you connect:
- The server checks your public key
- Your system proves identity using the private key
If both matches, access is granted.
Generating SSH Key
Open the Run this command on your local machine:
ssh-keygenIt will ask you to enter the file name to save the key. You can press Enter for the default file or you can provide your own file path. If you have not created any ssh key before, it is recommended to keep the default file.
It also asks you for the password, you can skip it for now and just press Enter.
This will create:
- Private Key – inside
~/.ssh/id_ed25519folder - Public Key – inside
~/.ssh/id_ed25519.pub
Copying SSH Key to VPS
After creating the SSH key, we need to copy the public key we created from our local machine.
Run this command to copy the public key:
cat ~/.ssh/id_ed25519.pubNow copy the entire output.
Note: If you did not choose the default file, you can replace id_ed25519.pub with your actual file name.
After copying, log into your vps.
Run the following command:
nano ~/.ssh/authorized_keysNow paste the key here. Log out from your VPS.
Logging in Using SSH Key
Now connect to your vps using:
ssh user@your_server_ipNote: Replace user with actual username and your_server_ip with the actual IP.
If everything is correct, you will log in without entering the password.
Disabling Password Login (Optional but Recommended)
After setting up the SSH key, it is recommended to disable password login. Password login can be breached using brute force methods.
Log in to your VPS and edit SSH config:
sudo nano /etc/ssh/sshd_configNow find:
PasswordAuthentication yesChange it to:
PasswordAuthentication noNow restart SSH:
sudo systemctl restart sshNote: Make sure SSH key login works before disabling password login. Otherwise, you may lock yourself out.
Conclusion
Using SSH keys makes your VPS login more secure and eliminates the need for passwords.
In this guide you learned how to:
- Generate SSH keys
- Add them to your VPS
- Log in securely without a password
This is a simple but important step in securing your server. If this guide solves your problem, share it with others and subscribe to our newsletter for more blogs. If you still have any issue, you can contact us for help.