What is a firewall? Rules, ports and network access
A firewall decides which network connections are allowed and which are blocked, according to rules you or a vendor have set. It can run on a computer, in a home router or at the edge of a cloud network, and often on several of these at once. A rule only controls access. Letting an application through does not make that application secure.
What a rule looks at
A rule can consider the direction of a connection, the source and destination addresses, the protocol, the port and sometimes the application. A home router typically blocks unsolicited incoming traffic. The firewall in the operating system controls what reaches the device itself.
Most firewalls are stateful: they remember connections you opened. Replies to your own outgoing connections are let back in automatically, so nobody has to open an inbound port for every website you visit.
Several firewalls sit on one path
Traffic may pass through several layers, and each can stop it. A cloud security group can allow a port while the host firewall blocks it. A local allow rule can be useless when the router does not forward the port, or when the Internet provider uses carrier-grade NAT and you have no public address of your own. Identify each layer before making broad changes.
IPv6 needs its own check. Look at the actual interface address and the IPv6 firewall policy; do not assume that your IPv4 NAT rules apply. An application can also listen on one address family while the client tries the other.
Why your service cannot be reached from outside
Start with the application. Confirm that it is running and listening on the expected protocol, port and network address. A service bound only to loopback, the address a machine uses to talk to itself, works locally and is invisible to everyone else. Then inspect the host rule, the router’s port forwarding and any provider restrictions.
Pay attention to how the attempt fails. A refused connection, a timeout and an application-level error suggest different points of failure, although filtering can blur the picture. Test from a network that is really separate, because some routers handle connections to their own public address differently from outside traffic. The 2ip port checker tries a TCP port from outside your network. It shows reachability from its own vantage point only, so a closed result still leaves the local firewall, NAT and the listener to check.
How to open a port without overexposing the device
Allow only the access the service needs, and from limited source addresses where that is practical. Keep the service updated and require proper authentication. Write down why the rule exists and remove it when the service is retired. After the change, verify two things: that the required connection works, and that nothing else became reachable.
Be equally careful in the other direction. Blocking every outbound connection can break updates or DNS, so understand what the device depends on before you apply such a rule.
Why blocking all ICMP backfires
ICMP carries the Echo messages used by ping, and it also carries control and error information that diagnostics and other mechanisms rely on. Path MTU Discovery, for example, depends on ICMP messages to learn that a packet is too large for some link. Blocking ICMP indiscriminately can cause failures that are hard to trace, such as connections that open and then stall. The articles on ICMP and MTU go into detail.
What a firewall will not stop
A firewall does not prevent phishing, unsafe downloads or an attack that arrives through a service you have allowed. Use it together with updates, backups and account security.