Set up SSH keys without losing access to your VPS
Create an SSH key, install the public key and test a new login before changing password access.
Read the guideVPS security · Practical guide
List required ports, allow SSH first and test the guest and Control Panel firewall rules.
Archive placement: April 2023

A useful firewall allows the traffic your services need and blocks the rest. Start with a short port list, preserve an administration route, and test each change from outside the VPS instead of assuming an “enabled” status means the setup is correct.
Make a simple list before adding rules: SSH for administration, HTTP and HTTPS for a public website, and any other application ports you deliberately expose. A database used only by the website normally does not need a public port. Keep monitoring, VPNs and administrator connections in the list so they are not accidentally blocked.
sudo ss -lntupAn address of 127.0.0.1 or ::1 is local to the server. 0.0.0.0 or [::] generally indicates a listener that can accept connections on network interfaces, subject to firewall rules. A listening port is not automatically safe to expose. Identify the process before allowing it.
The Control Panel firewall filters traffic outside the guest operating system. UFW, nftables or another guest firewall filters traffic inside it. Both must allow a required connection. A guest allow rule cannot cancel an upstream block. Existing Docker, VPN or control-panel software can also manage networking; do not replace its rules with an unrelated tutorial.
For Docker-published ports, ordinary UFW incoming rules may not filter traffic the way you expect. Check Docker’s documented firewall behaviour and test externally before treating a database or admin interface as private. Use one understood guest firewall manager rather than mixing manual iptables commands with UFW.
Confirm the browser console works, then leave a current SSH session open. In the Control Panel, allow your actual incoming SSH TCP port before tightening rules. The UFW example uses port 22. Replace it if SSH uses a custom port; the OpenSSH UFW profile may still refer to 22.
sudo ufw allow 22/tcp
sudo ufw status numberedIf your public administrator address is stable, a source-restricted rule is preferable. For example, allow from your real public IP to the SSH port rather than entering an example address verbatim. Keep a documented recovery route for when an office connection or VPN changes. Do not remove a broad working rule until the restricted replacement is tested.
A normal public web server usually needs incoming TCP 80 and 443. Add these only if this VPS serves a website. Do not copy a mail server or database port list onto a server that does not provide those services.
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw status verboseFor a fresh general-purpose web VPS, denying unsolicited incoming connections and allowing outgoing connections is a common starting point. It is not appropriate for every routed/VPN host. If adopting it, set default deny incoming and default allow outgoing only after the required incoming rules are in place. Review what those defaults mean for your actual application.
sudo ufw enable
sudo ufw status verboseRead the confirmation before accepting it. If UFW was already active, your rules generally took effect when added; there is no reason to disable it first. Make a new SSH connection, load the website from another network, and check any deliberately restricted admin interface. An existing SSH connection may remain open even when new logins are blocked.
Then make the corresponding Control Panel changes and repeat the tests. Keep IPv6 enabled or disabled according to your actual network design, and check that the guest and upstream rules cover the addresses your DNS publishes. A working IPv4 test does not test an AAAA record’s IPv6 route.
Use the console to restore the specific last rule if access is lost. Disabling the guest firewall can be a brief console-only recovery measure, but re-enable the corrected rules promptly; do not leave the whole server exposed as the permanent fix.
Keep a short description of why each rule exists. UFW’s numbered view is useful when removing a specific old rule with ufw delete followed by its number, but the numbers change after a deletion, so refresh the list each time. Test your public services afterward. Provider-managed restrictions, including outbound SMTP restrictions where applied, are separate from customer firewall rules and cannot be removed with a guest allow rule.
Usually no. Keep a website’s database on loopback or a private network. If remote access is required, restrict the source, use encryption and strong authentication, and verify the restriction externally.
A firewall can allow established connections while rejecting new ones. Always test a new connection before treating a rule change as successful.