Specify Port Ranges in Network and Firewall Rules
Learn to define, validate, secure, test, and troubleshoot contiguous TCP and UDP port ranges in firewalls, ACLs, NAT, forwarding, and cloud security rules.
A port range is an inclusive sequence of adjacent transport-layer port numbers. If a rule specifies 5000-5010, it applies to every port from 5000 through 5010, including both endpoints.
This lesson explains how to define ranges for firewall policies, access-control lists (ACLs), port forwarding, destination NAT (NAT), service objects, and security groups. It assumes basic knowledge of IP addressing, client-server communication, TCP, UDP, and inbound versus outbound traffic.
What Is a Port?
A port is a transport-layer endpoint number used with an IP address and a protocol to identify a service or application. TCP and UDP each have their own port namespace, and each namespace contains port numbers from 0 through 65535.
For example, a server might listen for TCP connections on port 443. A client connecting to that service uses the server's IP address and destination port 443, together with TCP, to identify the intended service.
Single Ports, Ranges, and Lists
Choose the narrowest representation that matches the application's requirements.
Inclusive Endpoints
The endpoints of a range are included. A range of 10000-10010 contains 11 ports: 10000, 10001, and every value through 10010. It does not include 9999 or 10011.
A useful way to verify the size of a range is:
number of ports = upper boundary - lower boundary + 1For example, 5010 - 5000 + 1 = 11.
Why Port Ranges Are Used
- Reduce repetitive rules: An application using adjacent ports can be covered by one rule instead of many individual entries.
- Allow or block dynamic service intervals: Some applications negotiate or allocate ports from a documented interval.
- Group NAT or forwarding mappings: An external interval can be mapped to an internal host and corresponding port interval.
- Represent reusable services: A service object or service group can hold protocol and range information for use in multiple policies.
A range is not automatically better than individual ports. Use one only when the application actually requires the complete interval.
TCP and UDP Must Be Selected Correctly
TCP is a connection-oriented transport protocol. UDP is a connectionless transport protocol. Each has separate port spaces, so TCP port 5000 and UDP port 5000 are different traffic targets.
A range by itself does not identify an application. The rule must specify TCP, UDP, or both if the platform supports a combined protocol choice. Select both only when the application documentation requires both.
TCP destination ports 5000-5010
UDP destination ports 5000-5010These are two different rule matches even though the numeric range is identical. Confirm the protocol from application documentation, packet capture, or observed traffic before creating the rule.
Range Syntax and Validation
Products use different input formats. Common notation is start-end; some products use start:end. For example:
Generic policy: 5000-5010
iptables: 5000:5010
Cisco-style ACL: range 5000 5010Always follow the syntax required by the specific product. Do not assume that a colon, hyphen, comma, or space has the same meaning everywhere.
Validation Rules
- Both boundaries must be numeric port values from 0 through 65535.
- The lower boundary must be less than or equal to the upper boundary.
- Endpoints are inclusive.
- A reversed range such as
5010-5000should be rejected or corrected; do not rely on a product to reinterpret it. - Out-of-range values such as
65536are invalid. - Overlapping ranges may be accepted, merged, or flagged depending on the product. Remove unnecessary overlap to make policy behavior easier to understand.
- Do not confuse a list such as
443,8443with the contiguous range443-8443.
Port-Number Categories
Port categories provide useful context, but they do not prove what service is running on a port.
An ephemeral port is a temporary port commonly selected by a client operating system for an outbound connection. It is often the traffic's source port, not the server's destination port. Do not normally create inbound rules allowing every ephemeral destination port just because clients use ephemeral source ports.
Source Ports and Destination Ports
The source port identifies the port used by the traffic originator. The destination port identifies the intended service on the receiving side.
For ordinary inbound service access, match the server's destination port. A client connecting to a server might look conceptually like this:
Client IP:49152 --TCP--> Server IP:443
source port 49152 destination port 443The client's source port may change from one connection to another. The server's destination port is normally the stable application port that belongs in the inbound rule.
Stateful firewalls track connection state. After permitting a legitimate outbound connection or inbound session, they typically allow the related reply traffic automatically. This usually avoids manually opening broad ephemeral destination-port ranges for return traffic. Stateless ACLs may require explicit rules in both directions, so check the platform's behavior.
Applying Ranges to Common Rule Types
Firewall Allow and Deny Rules
For a firewall policy, combine the port range with a protocol, source, destination, direction, and action. A generic rule pattern is:
protocol: tcp, udp, or explicitly selected protocol
source: authorized source address or network
destination: target host or network
destination port range: start-end
action: allow or denyExample: permit UDP destination ports 5000 through 5010 from only a trusted application network to a media host.
nft add rule inet filter input udp dport 5000-5010 accept
iptables -A INPUT -p udp --dport 5000:5010 -j ACCEPTThe nftables and iptables examples use product-specific syntax. Add source-address restrictions and persistent ruleset management as appropriate.
A deny example could block TCP destination ports 6000 through 6063 from an untrusted source zone while leaving unrelated ports unaffected.
Network ACLs
An ACL is an ordered list of permit and deny entries. ACL syntax varies by vendor. A Cisco-style extended ACL pattern is:
permit udp <source> <source-wildcard> <destination> <destination-wildcard> range 5000 5010Exact syntax, placement, and interface direction vary by operating system. Confirm whether the ACL is applied inbound or outbound and whether it evaluates source or destination ports as expected.
Port Forwarding and Destination NAT
Port forwarding uses destination NAT to map traffic arriving at an external address and port to an internal destination. For an external UDP range of 27015 through 27020, map the external range to the same internal range on the service host when that is what the application requires.
NAT alone may not permit traffic. A matching firewall policy must allow the translated or pre-translation traffic according to the platform's processing order. Verify the external range, internal address, internal range, protocol, and firewall policy together.
Service Objects and Policy Definitions
A service object can store a protocol and one or more ports. A service group can combine individual ports and ranges. This improves consistency when several policies use the same application requirements, but changes to a shared object can affect every policy that references it.
Security Groups and Cloud Firewall Rules
Cloud security groups and virtual firewall rules often support a start port and end port for a contiguous range. Scope ingress or egress rules to the required protocol, source CIDR, destination, and direction. Product behavior differs, so confirm whether the platform treats a rule as stateful and whether it supports separate source-port matching.
Rule Ordering and Scope
An otherwise correct range may not work because an earlier deny or more-specific rule takes precedence. Some systems use first match; others evaluate priorities, zones, or separate policy chains. Review the effective policy rather than only the rule that you edited.
Pair the range with:
- Authorized source addresses or networks.
- The intended destination host, subnet, or translated address.
- The correct interface, security zone, VLAN, or network direction.
- The required protocol.
- The narrowest action and time scope needed.
Least privilege means granting only the minimum access required for the intended function. Avoid source any, destination any, and both protocols unless they are genuinely necessary.
Practical Examples
Permit an Adjacent UDP Range
A media application requires UDP destination ports 5000 through 5010. Create an inbound rule from only the trusted application network to the media host. Select UDP, set the destination range to 5000-5010, and deny unrelated sources.
Block a Risky TCP Interval
To block TCP destination ports 6000 through 6063 from an untrusted zone, create a deny rule scoped to that source zone. Check rule ordering so that an earlier broad permit does not bypass the deny.
Forward an External Game-Service Range
Map external UDP ports 27015 through 27020 to the same destination-port interval on an internal host. Confirm that the internal service listens on those ports and that a matching firewall allow policy exists.
Represent Nonadjacent Ports Correctly
If an application needs TCP 443 and TCP 8443, use two individual ports or a service group. Do not configure 443-8443, because that would include every port between them.
Testing and Verification
- Verify the service: Confirm that the destination host is listening on the intended protocol, local address, and ports.
- Check the boundaries: Test the first port, a port in the middle, and the final port.
- Check an adjacent excluded port: For
10000-10010, verify that 10011 is not permitted when the policy is intended to exclude it. - Test from an authorized client: Use a client in the permitted source network and test the actual application where possible.
- Review evidence: Examine firewall logs, rule counters, packet captures, and rule-hit information.
- Test protocol separately: TCP connectivity tests do not prove that UDP is permitted. UDP verification may require application-level evidence or packet capture.
ss -lntu
nc -vz <host> 5000-5010ss -lntu displays listening TCP and UDP sockets on Linux. Netcat's range syntax and behavior vary by implementation, and a successful TCP test does not validate UDP. Use packet capture or application logs when a UDP service does not provide a direct response.
For broader inspection, packet-analysis tools such as tcpdump can show protocol, source port, destination port, and whether packets reach the expected interface.
Troubleshooting Port-Range Rules
Only Some Ports Work
- The application may not listen on every port in the configured interval.
- A host firewall, second network firewall, or security group may allow only part of the range.
- The lower or upper boundary may be incorrect.
- Inspect listening sockets, test the first, middle, and final ports, and review every policy layer and NAT mapping.
The Rule Does Not Match
- The wrong protocol may be selected.
- The policy may match source ports instead of destination ports.
- Traffic may arrive through another interface, zone, or address.
- An earlier rule may override the intended policy.
- Confirm TCP versus UDP, inspect rule order and logs, and verify source, destination, interface, and zone scope.
Port Forwarding Does Not Work Externally
- No corresponding firewall allow policy may exist.
- The internal host may not have a listening service.
- The NAT rule may translate to the wrong address or port interval.
- An upstream carrier or perimeter filter may block the ports.
- Test locally first, compare external and internal boundaries, review NAT and firewall logs, and test from a genuinely external network.
A Broad Rule Exposes Unintended Services
- A range may have been used where separate ports were required.
- The source may be set to any address.
- Both TCP and UDP may be allowed without a requirement.
- Compare the interval with the application's documented needs, restrict sources and protocols, and replace the broad range with individual ports or a narrower service object.
Security and Maintenance
- Open only the smallest necessary range.
- Document the application, protocol, port boundaries, owner, source networks, destination, and business reason.
- Record whether the rule is temporary, and remove it when the temporary work is complete.
- Review broad ranges periodically, especially ranges open to untrusted networks.
- Monitor logs and rule counters for unexpected sources, protocols, or traffic volumes.
- Recheck shared service objects after application changes because one object can affect multiple policies.
Key Takeaways
- A range such as
5000-5010represents every port from 5000 through 5010. - Valid TCP and UDP port numbers run from 0 through 65535.
- Select TCP, UDP, or both explicitly; a numeric range alone does not identify an application.
- Use lists or service groups for noncontiguous ports.
- Inbound service rules normally match destination ports.
- Rule order, address scope, interfaces, zones, NAT, and additional firewalls can change the result.
- Test both boundaries, a middle port, and an excluded neighboring port.
- Apply least privilege, document the reason, and remove temporary access.
For related fundamentals, review TCP and UDP ports, computer networks, IP routing, starting Nmap, and UDP scanning.