Opening up port 8080 in CentOS / Linux using iptables

I always like to add a comment and limit scope in my firewall rules.

If I was opening up tcp port 8080 from everywhere (no scope limiting needed) for Tomcat I would run the following command

iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT -m comment --comment "Tomcat Server port"

Then make sure to save your running iptables config so that it goes into effect after the next restart

service iptables save 

Note: you’ll need to have the comment module installed for that part to work, probably a good chance that it is if you are running Centos 5 or 6

P.S.

If you want to limit scope you can use the -s flag. Here is an example on how to limit traffic to 8080 from the 192.168.1 subnet

iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -s 192.168.1.0/24 -j ACCEPT -m comment --comment "Tomcat Serve

In case anybody has experienced that the port didn’t open after giving this command, you may have a target REJECT already existing in the INPUT chain and your new rule only added after that (I had this problem) which never reaches. So you have to insert your rule before the REJECT. First use iptables -L -n --line-numbers to display all rules with numbers and then use iptables -I INPUT <n> instead of iptables -A INPUT in the command in the answer (where <n> was the number of the REJECT target). This will insert your new rule above the REJECT and it should work.

To delete a rule at line n you can use.

iptables -D INPUT <n>

Leave a comment