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 / AlmaLinux 8 & 9 / Rocky Linux 8 & 9

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

If using firewalld:

To open a TCP port using firewalld, you can use the following command,

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

and for UDP ports you can use the below commands,

firewall-cmd --zone=public --add-port=12345/udp --permanent  

Then, reload firewalld for the changes to take effect,

firewall-cmd --reload

If using iptables:

To open TCP/UDP port follow below command,

iptables -A INPUT -p tcp -m tcp --dport PORT_HERE -j ACCEPT
    iptables -A INPUT -p udp -m udp --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
    iptables -A INPUT -p udp -m udp --dport 12345 -j ACCEPT

service iptables reload

Note: replace the Ports with actual ports

Firewalld

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