This article will provide a guide for how to open ports on Linux server. This guide requires root-equivalent access.
How to Open Ports with iptables
All Linux distributions are packaged with iptables, a software firewall.
In our examples, we will demonstrate how to open ports for both inbound and outbound tcp and udp traffic on IPv4. For IPv6-related changes, use ip6tables.
To open ports 4081-4085 for inbound tcp traffic over IPv4, use the following command:
iptables -I INPUT -p tcp --dport 4081:4085 -j ACCEPT
To open ports 4081-4085 for inbound udp traffic over IPv4, use the following command:
iptables -I INPUT -p udp --dport 4081:4085 -j ACCEPT
To open ports 4081-4085 for outbound tcp traffic over IPv4, use the following command:
iptables -I OUTPUT -p tcp --sport 4081:4085 -j ACCEPT
To open ports 4081-4085 for outbound tcp traffic over IPv4, use the following command:
iptables -I OUTPUT -p udp --dport 4081:4085 -j ACCEPT
How to Open Ports with firewalld
To open ports 4081-4085 with firewalld, use the following commands:
firewall-cmd --zone=public --permanent --add-port=4081-4085/tcp firewall-cmd --reload
How to Open Ports with UFW
To open ports 4081-4085 with Uncomplicated Firewall (UFW), use the following command:
sudo ufw allow 4081:4085
Conclusion
These examples have demonstrated how to open ports on Linux server using iptables.