Scanners sweep the whole internet in minutes looking for open databases, Redis, the Docker API, RDP and control panels. A database without a password that faces the internet usually falls into attackers' hands the same day. Here is which ports to close first, how to find out what is open right now and how to close the rest without losing access to the server.
The main rule
From outside, only the ports of the services the server exists for should be open: 80 and 443 for a site, 25, 465 and 587 for mail. Open SSH and control panels only to your own addresses. Everything else is closed by a “deny everything not allowed” policy.
Even better than a firewall is a service that does not listen on an external address at all. Databases and caches should listen on 127.0.0.1, and the firewall stays as the second line of defence in case of a configuration mistake.
What is open right now
On the server itself, see which services listen on which addresses:
sudo ss -tulpn
The addresses 0.0.0.0 and [::] mean “on all interfaces”, that is reachable from outside unless the firewall closes it. 127.0.0.1 and [::1] are local only. Then check the server from outside, from another computer:
nmap -Pn -p- --open server-address sudo nmap -Pn -sU --top-ports 50 server-address # UDP
A summary report with open ports, users and SSH settings is produced by the server audit.
Ports that must not face the internet
| Port | Service | Why it is dangerous |
|---|---|---|
| 3306, 5432, 1433, 27017 | MySQL, PostgreSQL, MS SQL, MongoDB | Password guessing, data theft and encryption |
| 6379, 11211 | Redis, Memcached | Often without a password; Redis is used to take over the server, Memcached's UDP for DDoS attacks |
| 9200 | Elasticsearch | Leaks of whole databases with personal data |
| 2375 | Docker API | Without a password it means root on the server |
| 2379, 10250 | etcd, kubelet | Secrets and control of the whole Kubernetes cluster |
| 135, 139, 445 | Windows services, SMB | WannaCry and NotPetya spread through them |
| 3389, 5900 | RDP, VNC | Password guessing around the clock; your addresses only |
| 161/udp, 623/udp | SNMP, IPMI | Device configuration, hardware-level server management |
| 23, 21, 111 | Telnet, FTP, rpcbind | Passwords in clear text, reconnaissance and DDoS attacks |
The full list of a hundred ports with explanations and ready commands is in the port reference.
How to close them without losing access
- First allow SSH from your address, and only then turn on denying the rest. Do not close the current session until you have checked logging in from a new window.
- Move services to the local address:
bind-address = 127.0.0.1in MySQL,listen_addresses = 'localhost'in PostgreSQL,bind 127.0.0.1 ::1in Redis. - Turn on a default-deny policy. For ufw:
sudo ufw default deny incoming, then allow what is needed. A full rule set for ufw, nftables or firewalld with a safeguard against losing access is built by the firewall rules generator. - Do not forget IPv6: if the server has an IPv6 address, it needs rules too.
- With a cloud provider, close the ports in security groups as well — it is a separate firewall in front of the server.
-p 5432:5432 are opened by Docker's own rules, and ufw does not see them. Publish such ports only on the local address — -p 127.0.0.1:5432:5432 — or restrict access in the DOCKER-USER chain.A router is a server too
On MikroTik routers Winbox, the web interface, DNS and forwarded ports to servers in the network are often open from outside. Close management on external interfaces — MikroTik hardening does this — and check that forwards lead only where they should. Repeat the external nmap check after every change: new services and containers sometimes open ports by themselves.