Cannot modify iptables on CentOS
My question: How to add a custom iptables rule to accept connection on a certain port?
I'm trying to open port 3500 on my server but failed. I started by using this command: (From http://wiki.centos.org/HowTos/Network/IPTables)
iptables -A INPUT -p tcp --dport 3500 -j ACCEPT
But then I run iptables -L
I still do not see the new rules being listed: (My assumption it should include 3500 in the output)
Chain INPUT (policy ACCEPT)
target prot opt source destination
RH-Firewall-1-INPUT all -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
ACCEPT tcp -- anywhere anywhere tcp dpt:rtmp-port
Chain FORWARD (policy ACCEPT)
target prot opt source destination
RH-Firewall-1-INPUT all -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain RH-Firewall-1-INPUT (2 references)
target prot opt source destination
ACCEPT all -- anywhere anywhere
ACCEPT icmp -- anywhere anywhere icmp any
ACCEPT esp -- anywhere anywhere
ACCEPT ah -- anywhere anywhere
ACCEPT udp -- anywhere 224.0.0.251 udp dpt:mdns
ACCEPT udp -- anywhere anywhere udp dpt:ipp
ACCEPT tcp -- anywhere anywhere tcp dpt:ipp
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT udp -- anywhere anywhere state NEW udp dpt:snmp
ACCEPT udp -- anywhere anywhere state NEW udp dpt:snmptrap
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:http
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ftp
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
Edit So I try to insert ACCEPT rule to the INPUT chain and my iptables now look like this:
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3500
RH-Firewall-1-INPUT all -- 0.0.0.0/0 0.0.0.0/0
But it does not allow me to connect to 3500 from outside. (I can still telnet from inside). When I'm trying to telnet my_host 3500, I get this: telnet: Unable to connect to remote host: Connection refused
Edit 2: My netstat -an | grep "LISTEN "
output:
tcp 0 0 127.0.0.1:3500 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:973 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 :::22 :::* LISTEN
Edit 3: I followed lain advise, also have my service bind to 0.0.0.0:3500 instead of 127.0.0.1:3500 and it works.
Solution 1:
Your rules is listed, rtmp-port
is port 3500 according to IANA port/service names. To get a listing of port numbers rather than their service names use the -n
switch
iptables -L -n
You have also use the -A switch to add your rule to the INPUT chain. This has added after the packets have been sent to the RH-Firewall-1-INPUT chain, the last rule of which is a blanket REJECT so packets destined for port 3500 will be REJECTed before they get tested in the INPUT Chain.
You have a couple of possible solutions - use the -I
switch to insert your rule to the INPUT
chain or the RH-Firewall-1-INPUT
chain
iptables -I INPUT -p tcp --dport 3500 -j ACCEPT
or
iptables -I RH-Firewall-1-INPUT -p tcp --dport 3500 -j ACCEPT
You should probably clean up your rules too, you can use
iptables -D INPUT -p tcp --dport 3500 -j ACCEPT
(several times) to delete the existing rules for port 3500 before adding the new rules.