Open and Close Ports using IPTables – Open a port in 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
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
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
You might also like
More from Dedicated Servers
Enable Mod_RemoteIP – See Visitors Real IP address when using Cloudflare & Apache
If you are using Cloudflare on your Apache server you will always see Cloudflare IPs in your logs and not …
Fix 413 Request Entity Too Large Errors When Using NGINX
Just like Apache, NGINX imposes default limits on the size of files that can be uploaded. A 413 Request Entity …
Cloud-init Modules That Automate and Customize Deployments
Cloud-init is a popular way to automate deployments of instances in a cloud or none cloud environment. To save having …