Opening a port on CentOS

CentOS 6

For example, if you want to open port 80,

iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT

In the above example, "--dport 80" signifies the port number to be opened, replacing 80 with another port number opens that port, for example, if we wish to open port 8080,

iptables -A INPUT -p tcp -m tcp --dport 8080 -j ACCEPT
iptables-save
service iptables restart

CentOS 7

Usually firewalld is the default on all CentOS 7 machines but you can switch to iptables as well

If using firewalld:

firewall-cmd --zone=public --add-port=PORT_HERE/tcp --permanent
firewall-cmd --reload

If using iptables:

iptables -A INPUT -p tcp -m tcp --dport PORT_HERE -j ACCEPT
service iptables reload

Replace PORT_HERE with the port you wish to open.

Example

To open port 80,

IPTables

iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
service iptables reload

Firewalld

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload

Last Modified: January 02 2018 19:13:25