How to secure SSH on a new VPS without lockouts
Secure SSH on a new VPS by adding a key, testing a second login, and checking firewall access before disabling password authentication or root login. Keep a rollback path open while you apply changes.
Add an SSH key, prove it works from a second terminal, check firewall access, then change authentication settings one at a time. Keep your current session open until the new login succeeds.
The order matters more than the settings.
Keep a recovery path open
Before editing SSH, make sure you can get back in if a setting or firewall rule is wrong. Keep the current SSH session open, and confirm you have a console or another recovery path available for this VPS.
- Keep the original SSH session open while you test every change in a new session.
- On Ekire, the server's browser console (VNC) in the control panel and rescue mode reach the machine even when SSH is locked out. Open the console once before you start, so you know where it is.
- Change one setting at a time so you can identify what caused a failed login and reverse that change.
Add and test SSH key authentication
Create a key on your own device, install its public half on the VPS, then test it from a separate terminal. Keep the private key on your device; do not copy it to the server.
Generate a key
On your device, run
ssh-keygen -t ed25519 -a 64 -C "you@device". Keep the default file location if you do not already have a key there. Set a passphrase to protect the private key on your device.Install the public key
If
ssh-copy-idis available, runssh-copy-id -i ~/.ssh/id_ed25519.pub user@SERVER_IPand replace the account name and address with yours. This step may ask for the account's current password.Test from a second terminal
Open a new terminal and run
ssh -o IdentitiesOnly=yes -i ~/.ssh/id_ed25519 user@SERVER_IP. Use the same account and address. Confirm you reach a shell without relying on the original session.
If the key login fails, check the client output with ssh -vvv -i ~/.ssh/id_ed25519 user@SERVER_IP. The OpenSSH client manual (opens in a new tab) describes its debug options. Check that you used the matching public key, the right account and address, and the expected private-key file.
Allow SSH through the firewall before tightening access
A host firewall rule can block SSH even when the daemon is configured correctly. Check the operating-system firewall and any provider-side network rules before enabling or changing firewall policy. On Ekire, turning a server's firewall on in the control panel is default-deny: add an inbound allow rule for your SSH port before you switch it on.
- Find the port SSH currently uses and inspect the active firewall rules. For UFW,
sudo ufw status numberedshows its rules. - Before enabling the firewall, allow the current SSH port. For UFW on port 22, use
sudo ufw allow 22/tcp. Replace 22 if SSH uses another port, and check the rule before enabling UFW. See Ubuntu's firewall documentation (opens in a new tab) for UFW guidance. - Make one firewall change, then test a new SSH connection. Leave the old session open until that test succeeds.
- Review Ekire's network information for provider-side context. It does not replace checking or configuring the VPS's own firewall.
Disable password and root login only after the key test
First confirm the key works for an account with the access you need, including administrative access if required. Check the effective SSH daemon settings before editing: included files and Match blocks can affect which values apply.
- Inspect effective settings with
sudo sshd -T. If the configuration containsMatchblocks, usesudo sshd -T -C user=ACCOUNT,host=HOST,addr=CLIENT_IPwith values for the connection you are checking. Review the OpenSSH sshd_config manual (opens in a new tab) for the meaning ofPasswordAuthenticationandPermitRootLogin. - Set
PasswordAuthentication noto disable password authentication, andPermitRootLogin noto prevent direct root login over SSH. - Validate the edited configuration with
sudo sshd -tbefore reloading or restarting the daemon. - Apply the settings using the SSH service name for your operating system, then open a new connection. Keep the existing session until the new login succeeds.
If the new connection fails, restore the previous configuration through the open session or another recovery path you have confirmed is available. Run sudo sshd -t again, then reload the service. Do not close the session until you have tested access.
Treat port changes and Fail2ban as secondary controls
Changing the default port can reduce routine connection noise, but it does not replace key authentication, access rules, or operating-system updates. Fail2ban can block repeated authentication attempts, but it is optional and does not make weak credentials or exposed services safe.
If you change the port
Allow the new port through each relevant firewall before changing the SSH daemon setting. Then validate the daemon configuration, apply it, and test a new connection on that port before closing the old session.
If you add Fail2ban
Treat it as an extra control, not a replacement for key authentication, access rules, or updates. Check that its rules do not block your own address during testing.
Know what SSH hardening cannot guarantee
SSH settings cannot protect a compromised private key, an unpatched operating system, or an application exposed through another service. No port choice or blocking tool proves that a VPS is secure; keep updates and access checks in your routine.
For product-specific guidance, check Ekire's documentation (opens in a new tab). It is documentation, not a security audit of your VPS.
Frequently asked questions
Should I change the default SSH port on my VPS?
You can, but it is optional. A different port can reduce routine connection noise; it does not replace key authentication, firewall rules, or updates. Allow the new port through relevant firewalls before changing the daemon setting, then test a new login.
How do I disable password login for SSH?
After confirming key login in a separate session, set PasswordAuthentication no in the active SSH daemon configuration. Run sudo sshd -t before reloading the service, then test a new connection while keeping the old session open.
Is it safe to disable root login over SSH?
It is safe to disable direct root login once you have tested an account with the administrative access you need. Set PermitRootLogin no and confirm the alternate account can perform required administration.
Do I need Fail2ban to protect SSH on a VPS?
No. Fail2ban is optional and can block repeated authentication attempts, but it does not replace key authentication, firewall rules, or updates.
How can I avoid locking myself out while changing SSH settings?
Keep the current session open, confirm a recovery path, change one thing at a time, and test each change in a new connection. Validate the SSH configuration before applying daemon changes.
How do I test that my VPS SSH configuration is secure?
There is no single command that proves a VPS is secure. Validate the SSH daemon configuration, confirm key login in a new session, check firewall rules, and keep the operating system and exposed services updated.
See pricingReview current VPS plans and billing details.