Open and Close Ports using IPTables – Open a port in IPtables
IPtables is the default firewall used on CentOS and RHEL systems. On most of First2Host services like VPS Servers or Dedicated Servers, you will find the firewall is active but all ports are open. We do this to ensure all users can connect to services they may install like cPanel or Plesk. If you want to close ports on your server or even open port IPtables if you have a pre-configured firewall you can use the rules below to open and close ports on your firewall.
If you use CentOS 7 or above it’s likely you are using FirewallD and not IPtables. We have written a guide on how to open ports and close ports when using FirewallD
List Current Firewall Rules
This command lists all the current firewall rules loaded into IPtables.
Open port IPtables
You can open port centOS servers by adding a new rule to IPtables. You should restart IPtables after adding rules.
iptables -I INPUT -p tcp -m tcp --dport 80 -j ACCEPT service iptables save
This command opens port 80. To open different ports Just swap the 80 for the port number you wish to open. Run the service iptables save command to save the rules to your firewall configuration.
Close port IPtables
iptables -I INPUT -p tcp -m tcp --dport 80 -j REJECT service iptables save
This command would close the port 80 on your server and no one would be able to connect via that port. Just swap the 80 for your required port number then run the service iptables save command to save this to your IPtables configuration. You can also use the DROP command instead of REJECT.
If you use a CentOS 7 or CentOS 8 server you likely use FirewallD and not IPtables. See the How to open ports and close ports in FirewallD
Always deploy a firewall to your server. See our firewall guide How to install CSF to your Server
Join The Discussion.
[Search Terms: iptables open port, iptables close port]
How was this article? – Open and Close Ports using IPTables