Block Incoming Port
The syntax to block an incoming port using iptables is as follows. This applies to all the interfaces globally.
-j DROP
To block the port only on a specific interface use the -i option.
-p tcp --destination-port -j DROP
To block port only for given IP or Subnet use the -s option to specify the subnet or IP addess.
-p tcp --destination-port -s -j DROP
-p tcp --destination-port -s -j DROP
For example:
To block port 21 (to block FTP), use the command below:
Save the iptables for rules to be persistent across reboots.
To block port 21 for a specific IP address (e.g. 10.10.10.10) on interface eth1 use the command :
Save the iptables for rules to be persistent across reboots.
Block Outgoing Port
The syntax to block an outgoing port using iptables is as follows. This applies to all the interfaces globally.
-j DROP
To block the port only on a specific interface use the -i option.
-p tcp --destination-port -j DROP
To block port only for given IP or Subnet use the -s option to specify the subnet or IP addess.
-p tcp --destination-port -s -j DROP
-p tcp --destination-port -s -j DROP
For example:
To block outgoing port # 25, use the below command.
Save the iptables for rules to be persistent across reboots.
To block port # 25 only for ip address 10.10.10.10 use the command :
Save the iptables for rules to be persistent across reboots.