Centos 7 Add Iptables Rule Allow Ip And Port For Income And Outgoing Traffic
To add an iptables rule to allow incoming and outgoing traffic for a specific IP address and port in CentOS 7, you can follow these steps:
- Open a terminal window and login as the root user.
- Check the current status of the iptables service using the following command:
systemctl status iptables
3. If the service is not running, start it using the following command:
systemctl start iptables
4. To allow incoming and outgoing traffic for a specific IP address (for example, 192.168.1.100) and port (for example, 8080), you can use the following commands:
iptables -A INPUT -s 192.168.1.100 -p tcp --dport 8080 -j ACCEPT
iptables -A OUTPUT -d 192.168.1.100 -p tcp --sport 8080 -j ACCEPT
The first command appends a rule to the INPUT chain that allows TCP traffic from the IP address 192.168.1.100 on port 8080, while the second command appends a rule to the OUTPUT chain that allows TCP traffic to the IP address 192.168.1.100 with a source port of 8080.
5. Save the changes to the iptables configuration file so that they persist after a system reboot using the following command:
service iptables save
Alternatively, you can use the following command if you're running CentOS 7 with Systemd:
systemctl enable iptables.service
This command enables the iptables service to start automatically at boot time.
6. Verify that the rule has been added correctly by listing the current iptables rules:
iptables -L
This command lists the current iptables rules, including the ones you just added.
That's it! You have successfully added an iptables rule to allow incoming and outgoing traffic for the IP address 192.168.1.100 and port 8080 in CentOS 7.