How to open/block a Port in IPtables Firewall on a Linux server
How to open a Specific Port in IPtables Firewall on a Linux server
Using Iptables command you can add, edit and delete firewall filter rules. You must have server root access to make changes in Iptables firewall.
IMPORTANT: Be careful when you execute Iptables firewall commands on server backend because some commands might lock you out from the server. Before running the iptables command, you must double check the command and also you must know what all changes the rules will do on the server.
How to Open an incoming port in IPtables
2. Run the below command to open incoming port
iptables -A INPUT -p tcp –dport portnumber -j ACCEPT
In the above command “portnumber” should be replaced with the incoming port number you wish to open
INPUT = INPUT means incoming traffic to the server. (The server port can be accessed from outside the server). An example is given below
root@server [~]#
3. Run the command “service iptables save” to save the firewall rules
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
iptables -A INPUT -i eth0 -p tcp –dport 80 -j ACCEPT
-A = To Append Rules
INPUT = Incoming packets
-i = Interface name (Example : eth0, eth1, venet0 etc)
-p = Protocol (example : tcp, udp etc)
–dport = Destination port
-j = Target
ACCEPT = Allow the packet
How to Open an Outgoing Port in Iptables firewall
2. Run the below command to open outgoing port
iptables -A OUTPUT -p tcp –dport portnumber -j ACCEPT
“portnumber” in the above command should be replaced with the actual outgoing port number you wish to open.
OUTPUT = OUTPUT means outgoing traffic from the server. (From server to outside)
An example is given below :
root@server [~]#
3. Do not forget to save the IP tables rules : “service iptables save”
You must save the iptables rules after making any changes in iptables firewall. Rules will be removed if it is not saved.
Command to save the firewall rules : “service iptables save”
OR
root@server [~]# /etc/init.d/iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
On ubuntu servers “service iptables save” command will not work so you must use the command “iptables-save”
root@server [~]#iptables-save
“iptables -nL | grep 3032” will show the above rule you have added on the server.
How to open range of ports in Iptables
How to check whether the port is open on the server
Example : telnet 192.168.2.5 25
The above command will telnet to port 25 of the server “192.168.2.5”
Trying 192.168.2.5…
Connected to server.example.com
Escape character is ‘^]’.
220-server.example.com ESMTP Exim