How do you allow an IP to access a port in iptables on Linux?

In Linux, the iptables command can be used to allow specific IP addresses to access specific ports. Here is an example:

If you want to allow a host with the IP address 192.168.0.100 to access TCP port 80, you can use the following command:

sudo iptables -A INPUT -p tcp -s 192.168.0.100 --dport 80 -j ACCEPT

Can you explain this command?

  1. Sudo allows you to run commands with administrator privileges.
  2. iptables is the command itself.
  3. -A INPUT indicates adding a rule to the INPUT chain, which is the chain that handles inbound traffic.
  4. The rule applies to TCP protocol when using -p tcp.
  5. – The rule applies to packets with a source IP address of 192.168.0.100.
  6. –dport 80 means the rule applies to packets with a destination port number of 80.
  7. -j ACCEPT means that matched packets will be accepted.

After executing this command, the host with the IP address of 192.168.0.100 will be able to access TCP port 80.

Please note that this is just a temporary rule that will expire after restarting the system. To make it permanent, you can add the rule to the firewall rule configuration file. The location of the configuration file may vary in different Linux distributions. Typically, you can find this file in the directories /etc/iptables/ or /etc/sysconfig/iptables.

bannerAds