Unit

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.

Requirement: One service port. Representation: Single port. Example: TCP 443. Security consideration: Usually more precise than a range.

Requirement: Adjacent ports from a lower boundary to an upper boundary. Representation: Contiguous range. Example: UDP 5000-5010. Security consideration: Includes every port between the boundaries.

Requirement: Separate ports that are not adjacent. Representation: Port list or service group. Example: TCP 443 and TCP 8443. Security consideration: Do not replace this with 443-8443, which would open thousands of unnecessary ports.

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 + 1

For 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-5010

These 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 5010

Always 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-5000 should be rejected or corrected; do not rely on a product to reinterpret it.
  • Out-of-range values such as 65536 are 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,8443 with the contiguous range 443-8443.

Configuration issue: Reversed boundaries. Why it fails or is unsafe: The policy may be rejected or may not match as intended. Corrective action: Put the smaller number first.

Configuration issue: Port below 0 or above 65535. Why it fails or is unsafe: It is outside the valid TCP and UDP port interval. Corrective action: Correct the value or verify whether a different field was intended.

Configuration issue: Unintended overlap. Why it fails or is unsafe: Duplicate or conflicting policy entries can make troubleshooting difficult. Corrective action: Consolidate ranges where appropriate and review rule order.

Configuration issue: A range used for nonadjacent ports. Why it fails or is unsafe: It permits unrelated ports between the required values. Corrective action: Use individual ports or a service group.

Configuration issue: TCP selected for a UDP application, or vice versa. Why it fails or is unsafe: The packets do not match the rule. Corrective action: Confirm the transport protocol and create separate rules if needed.

Port-Number Categories

Port categories provide useful context, but they do not prove what service is running on a port.

Category: Well-known ports. Typical interval: 0-1023. Typical use: Common system and standard services. Administrative caution: A broad low-numbered range may expose many unrelated services.

Category: Registered ports. Typical interval: 1024-49151. Typical use: Applications and services using assigned or commonly recognized ports. Administrative caution: Verify actual application ownership before opening a range.

Category: Dynamic/private ports. Typical interval: 49152-65535. Typical use: Temporary client connections and application-selected ports. Administrative caution: A broad range can create a large attack surface; do not open it merely because clients use ephemeral ports.

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 443

The 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 deny

Example: 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 ACCEPT

The 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 5010

Exact 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

  1. Verify the service: Confirm that the destination host is listening on the intended protocol, local address, and ports.
  2. Check the boundaries: Test the first port, a port in the middle, and the final port.
  3. Check an adjacent excluded port: For 10000-10010, verify that 10011 is not permitted when the policy is intended to exclude it.
  4. Test from an authorized client: Use a client in the permitted source network and test the actual application where possible.
  5. Review evidence: Examine firewall logs, rule counters, packet captures, and rule-hit information.
  6. 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-5010

ss -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-5010 represents 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.