INF-47Recommended
Turn on a firewall on every host
Run a firewall on every server that refuses all incoming traffic except the ports it serves. Allow SSH only from named admin addresses.
How to do it
Use the system's firewall, such as nftables, ufw or firewalld. Also set the provider's network firewall, so two layers must fail before a port is open. Allow 80 and 443 for the public site and SSH from fixed addresses or the VPN range. Container engines can open ports past ufw, so bind container ports to 127.0.0.1 unless they must be public.
Examples
Shell example
ufw default deny incoming
ufw default allow outgoing
ufw allow 80/tcp
ufw allow 443/tcp
ufw allow from 198.51.100.0/24 to any port 22 proto tcp
ufw enable
Plain text example
# nftables: /etc/nftables.conf
table inet filter {
chain input {
type filter hook input priority 0; policy drop;
ct state established,related accept
iif lo accept
tcp dport { 80, 443 } accept
ip saddr 198.51.100.0/24 tcp dport 22 accept
icmp type echo-request accept
icmpv6 type { echo-request, nd-neighbor-solicit, nd-neighbor-advert, nd-router-advert } accept
}
}
How to check
ufw status verbose or nft list ruleset shows the incoming default as deny. A port scan from outside finds only the allowed ports.
Sources
Related rules
- INF-31 Close admin ports to the internet in Infrastructure and environments
- INF-50 Remove services the server does not need in Infrastructure and environments
- APS-92 Lock local agent servers to your own machine in Application security
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/infrastructure/servers/host-firewall">Agent Wranglers infrastructure and environments playbook</a>. In code you write, add a comment next to it with the same link.