DEV Community

Unrent
Unrent

Posted on

Close SSH on your VPS without locking yourself out

Most VPS images start with SSH open to the internet on port 22. That is convenient on day one, and it is also the port that gets scanned all day. Closing it is a good idea. Doing it in the wrong order is how people lock themselves out of a box they still need.

You can close SSH safely. You just need another way to reach the server, a firewall that matches what you think is open, and a way to deploy and debug that does not depend on an inbound port.

What "closing SSH" actually means

Two things have to be true at once:

  1. sshd is not reachable from the internet. Either it is not listening on a public address, or the firewall (on the host and at your cloud provider) drops port 22.
  2. You still have a path to run commands when something breaks: the provider's serial or VNC console, SSH only on a private network (for example Tailscale), or a management agent that opens a terminal only when you ask.

If you only change sshd_config and leave port 22 open, scanners still find a listener. If you only close the port and have no other path in, the next kernel update or bad firewall rule becomes an outage you cannot fix.

Look at what is actually listening

Before you change anything, see what the server already exposes:

sudo ss -tulpn
sudo iptables -L -n
# Ubuntu/Debian, if ufw is in use:
sudo ufw status verbose
Enter fullscreen mode Exit fullscreen mode

On a typical new VPS you will see sshd on 22, and maybe nothing else. After you put an app in production you should see 80 and 443, and nothing unexpected. A forgotten docker run -p 5432:5432, or a database published to 0.0.0.0, is more common than people think.

Do the same check in your cloud console. The host firewall and the provider's security group are two separate doors. If either one is open, the port is open.

Harden SSH before you depend on closing it

Keep SSH available while you set up the rest, but stop it accepting passwords. On OpenSSH this is the usual baseline:

# in /etc/ssh/sshd_config.d/hardening.conf
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin prohibit-password
Enter fullscreen mode Exit fullscreen mode

Then test and reload:

sudo sshd -t && sudo systemctl reload ssh
Enter fullscreen mode Exit fullscreen mode

Use a second session to confirm you can still sign in with a key before you close the first one. A change that drops your only session is the classic lock-out.

If you plan to keep SSH at all, restrict it further: only from your own address, or only on a tailnet. Binding SSH to a private interface is stronger than hoping a long key is enough on a public port.

Set a default-deny firewall

On Ubuntu or Debian with ufw:

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 22/tcp   # temporary, until the new path in is proven
sudo ufw enable
sudo ufw status verbose
Enter fullscreen mode Exit fullscreen mode

On a RHEL-family server, the same idea is a default-deny nftables or firewalld zone, with only the ports you chose. Allow outgoing traffic: an outbound-only agent, package updates and certificate issuance all need it.

Do not delete port 22 in this step. Prove the next path first.

The outbound-only path

The reason SSH stays open on so many app servers is that the deploy tool needs a way in. CI holds a key, a panel shells in, or you ssh to run docker pull. That means a listening port, and a key that has to live somewhere other than the server.

The other pattern is an agent on the server that dials out, usually over a websocket, and only accepts instructions that are signed and narrowly typed. Nothing listens for the control plane. If you disconnect the agent, the app containers, the database and the proxy keep running; you have only lost the remote panel.

That is the path you want to prove before you close port 22:

  • The agent is connected.
  • You can deploy, read logs and, if you need a shell, open a terminal through that path.
  • ss -tulpn shows no listening port for the agent itself.

Only when those are true is it safe to take SSH off the public internet.

Then close port 22

Close SSH in both places: the host firewall and the cloud security group. If you still want a break-glass key, leave sshd running but bound to the tailnet, or allowed only from your address, and confirm that a scan of the public IP no longer shows 22.

# ufw: drop the temporary SSH rule
sudo ufw delete allow 22/tcp
sudo ufw status verbose

# confirm nothing is listening on the public address
sudo ss -tulpn | grep ':22'
Enter fullscreen mode Exit fullscreen mode

Then, from a network that is not on any allow-list, check that the public IP refuses 22 and still serves 80 and 443. The provider console stays your last resort.

How Unrent runs this

Unrent is a hosted control plane that deploys apps and Postgres onto servers you already own. The agent on each server connects out. Unrent does not hold an SSH key, and the agent does not open a listening port.

Servers Unrent creates have SSH closed unless you choose otherwise. You can leave it off, open it only from your address with your key, or only over Tailscale. Web traffic can be public on 80 and 443, or only through a Cloudflare Tunnel, with no inbound port at all.

A terminal session is not a standing login. It is a recorded session that your approvers sign with a passkey, and the server checks that approval itself before a shell opens. Opening a shell on the server itself takes two approvers.

The same set of open ports is enforced twice: in your cloud firewall, where Unrent puts the rules back if someone changes them in the cloud console, and in the server's own firewall. Every night the server compares what is listening with that set and reports, in plain words, a port open to the internet, a missing firewall, SSH accepting passwords, or a cloud firewall changed outside Unrent.

If you bring a server you already have, the install command ends by offering the same choice: pick the profile, review the hardening plan, then apply it. The plan is shown first; nothing is applied until you accept it. On the server, sudo unrent doctor checks the agent, its outbound connection, the clock, Docker and the firewall, and prints a one-line fix for anything that fails. It only reads; it does not change the server.

A checklist you can reuse

  • Do you have a path in that is not port 22: provider console, private SSH, or a connected outbound agent?
  • Have you confirmed that path with a real command, not just a "connected" light?
  • Do the host firewall and the cloud security group allow the same set of ports?
  • Does ss -tulpn match that set?
  • If SSH still runs, does it accept keys only, and only from a network you intend?
  • After you close 22, did you test from an address that should be blocked?

Close the port last, not first. The goal is a server that has nothing inbound it does not need, and a way back in that you have already used.

More at unrent.dev.

Top comments (0)