How to Open port IPTables – Close port IPtables

IPtables is the default firewall used on CentOS and RHEL systems. On most F2H services like NVMe 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. So, 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.

But, 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

iptables -L

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

Therefore, this command opens port 80 in IPtables, 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

So, this command would close port 80 in IPtables 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

Finally, always deploy a firewall to your server. You do not need to use IPtables. We recommend CSF. CSF is a software based firewall that is easy to install and configure. CSF will protect your server against common attacks like Brute force and Port sniffing. CSF can be used on High Availability VPS Servers and comes with a module for cPanel too. So see our firewall guide How to install CSF to your Server and protect your server from malicious

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button