How can I open a port or a range of ports in iptables?

Solution In Progress - Updated -

Environment

  • Red Hat Enterprise Linux 7
  • Red Hat Enterprise Linux 8
  • Red Hat Enterprise Linux 9

Issue

  • How can I open a port in the iptables?
  • How to open the port range using iptables?

Resolution

To open a port or a range of ports in iptables on RHEL, you’ll need to use the iptables command to configure the firewall rules.

  • To open a specific port, like port 8080 for TCP. Replace 8080 with the required port number you wish to open:

Syntax:

# iptables -A INPUT -p tcp --dport 8080 -j ACCEPT

Note: This appends a rule gets added to the end of the specified INPUT chain.

  • Insert the same above rule at the top of the specified chain by default using the below command:
    Syntax:
# iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
  • To open a range of ports, for example, ports 8000 to 8100:

Syntax:

# iptables -A INPUT -p tcp --dport 8000:8100 -j ACCEPT

Note: This appends a rule gets added to the end of the specified INPUT chain.

  • Insert the same above rule at the top of the specified chain by default using the below command:
    Syntax:
# iptables -I INPUT -p tcp --dport 8000:8100 -j ACCEPT
  • After adding your rules, make sure to save them so that they persist after a reboot using the below command:
# service iptables save
# iptables-save > /etc/sysconfig/iptables
  • To validate the ports are added or not use the below command:
# iptables -xvnL

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments