The iptables firewall is preinstalled on most of the Linux distributions such as Centos, Ubuntu, Debian, etc. We are using iptables to secure the server by restricting access to the ports, denying access from a specific IP or range etc. Here I am providing the steps to open a port on the server using the iptables command. You can watch the video tutorial by clicking here.
- Login to the server as root or a privileged user.
- First, check the currently applied rules by using the following command:
# iptables -L
- It is good to take a backup of the currently applied rules before adding a new one to make sure we can restore it to the previous state if something goes wrong:
# iptables-save > IPtables_backup.txt
- Add the rule in the following format:
#sudo iptables -A INPUT -p tcp --dport xxxx -j ACCEPT
I am providing a sample rule below to open the port 3306:
# sudo iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
- If you need to remove a rule, use the following command format:
#sudo iptables -D INPUT -p tcp --dport xxxx -j ACCEPT
- If you need to open a specific port for a specific IP address only, use the following format:
# sudo iptables -A INPUT -p tcp -s your_ip --dport xxxx -j ACCEPT