A service stack often needs the same iptables decision for several ports, such as HTTP, HTTPS, and an alternate management listener. The multiport match keeps those ports in one rule so the chain is shorter and the counter shows the combined traffic for that service group.
The multiport match works with TCP, UDP, UDPLite, DCCP, or SCTP and accepts a comma-separated list of ports or port ranges. The standard extension limits one rule to a small set of ports, so large or unrelated service groups should be split into separate rules.
Use a multiport rule only when the same protocol, source, destination, interface, state, and target apply to every listed port. If one port needs a different source range or target, write a separate rule so the chain remains auditable.
Related: How to allow a port with iptables
Related: How to list iptables rules with counters
Related: How to save iptables rules permanently
Tool: iptables Rule Generator
$ sudo iptables --list INPUT --line-numbers --numeric --verbose Chain INPUT (policy DROP 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination
If a broad DROP or REJECT rule already exists, insert the multiport rule above it instead of appending after it.
Related: How to insert an iptables rule at a specific position
$ sudo iptables -C INPUT -p tcp -m multiport --dports 80,443,8443 -j ACCEPT iptables: Bad rule (does a matching rule exist in that chain?).
No output with exit status 0 means the exact rule is already present.
$ sudo iptables -A INPUT -p tcp -m multiport --dports 80,443,8443 -j ACCEPT
Replace the example ports with the real service ports. Keep the list to ports that share the same security decision and protocol.
$ sudo iptables -S INPUT -P INPUT DROP -A INPUT -p tcp -m multiport --dports 80,443,8443 -j ACCEPT
$ nc -vz -w 2 server.example.com 80 Connection to server.example.com 80 port [tcp/http] succeeded! $ nc -vz -w 2 server.example.com 443 Connection to server.example.com 443 port [tcp/https] succeeded! $ nc -vz -w 2 server.example.com 8443 Connection to server.example.com 8443 port [tcp/*] succeeded!
Testing only one port proves only that one port. Each listed port still needs an application or connection check that matches its expected traffic.
$ sudo iptables --list INPUT --line-numbers --numeric --verbose --exact Chain INPUT (policy DROP 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 3 180 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 multiport dports 80,443,8443
The counter is shared by all ports in the rule. Use separate rules when per-port counters are required.
$ sudo iptables -D INPUT -p tcp -m multiport --dports 80,443,8443 -j ACCEPT
After deleting the grouped rule, add separate service rules for ports that need different source limits, comments, or targets.
Related: How to delete an iptables rule
$ sudo netfilter-persistent save run-parts: executing /usr/share/netfilter-persistent/plugins.d/15-ip4tables save
Do not save a grouped allow rule until the full port list is reviewed. A stray port in the list can expose a service that was not part of the change request.