How to open a port using iptables

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.

  1. Login to the server as root or a privileged user.
  2. First, check the currently applied rules by using the following command:
    # iptables -L
  3. 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
  4. 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
  5. If you need to remove a rule, use the following command format:
    #sudo iptables -D INPUT -p tcp --dport xxxx -j ACCEPT
  6. 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
  • 1 Users Found This Useful
Was this answer helpful?

Related Articles

How To Install and Use Docker on Ubuntu 16.04

Introduction Docker is an application that makes it simple and easy to run application processes...

Change Main IP of the server :: Vesta Panel

If we are changing the main IP of a server installed with Vesta panel, we can use the following...

cPanel: Apache-FastCGI Data Timeout Error

If you are using a cPanel server and have FastCGI enabled in Apache, you might be facing the...

Domainkey installer in cPanel

You can use the following cPanel script to install domainkey for an account...

Email issues :: Common Troubleshooting Steps

Here I will discuss some common errors and their solution: We are using a mail relay setup for...