阻止传入端口

使用 iptables 阻止传入端口的语法如下。
这适用于全局所有接口。

# iptables -A INPUT -p tcp --destination-port [port number] -j DROP

要仅在特定接口上阻止端口,请使用 -i 选项。

# iptables -A INPUT -i [interface name] -p tcp --destination-port [port number] -j DROP

要仅阻止给定 IP 或者子网的端口,请使用 -s 选项指定子网或者 IP 地址。

# iptables -A INPUT -i [interface name] -p tcp --destination-port [port number] -s [ip address] -j DROP
# iptables -A INPUT -i [interface name] -p tcp --destination-port [port number] -s [ip subnet] -j DROP

例如:

要阻止端口 21(阻止 FTP),请使用以下命令:

# iptables -A INPUT -p tcp --destination-port 21 -j DROP

保存 iptables 以使规则在重新启动后保持不变。

# service iptables save

要在接口 eth1 上阻止特定 IP 地址(例如 10.10.10.10)的端口 21,请使用以下命令:

# iptables -A INPUT -p tcp -i eth1 -s ! 10.10.10.10 --destination-port 21 -j DROP

保存 iptables 以使规则在重新启动后保持不变。

# service iptables save
CentOS/RHEL:如何使用 iptables 阻止传入和传出端口

始终建议停止服务并阻止不需要的端口。
保持不需要的端口打开,可能会导致系统出现漏洞。
根据需要,您可以阻止特定端口上的传入和传出流量。

之路 on it Road.com

阻止传出端口

使用 iptables 阻止传出端口的语法如下。
这适用于全局所有接口。

# iptables -A OUTPUT -p tcp --destination-port [port number] -j DROP

要仅在特定接口上阻止端口,请使用 -i 选项。

# iptables -A OUTPUT -i [interface name] -p tcp --destination-port [port number] -j DROP

要仅阻止给定 IP 或者子网的端口,请使用 -s 选项指定子网或者 IP 地址。

# iptables -A OUTPUT -i [interface name] -p tcp --destination-port [port number] -s [ip address] -j DROP
# iptables -A OUTPUT -i [interface name] -p tcp --destination-port [port number] -s [ip subnet] -j DROP

例如:

要阻止传出端口 # 25,请使用以下命令。

# iptables -A OUTPUT -p tcp --destination-port 25 -j DROP

保存 iptables 以使规则在重新启动后保持不变。

# service iptables save

要仅为 IP 地址 10.10.10.10 阻止端口 #25,请使用以下命令:

# iptables -A OUTPUT -p tcp -d 10.10.10.10 --destination-port 25 -j DROP

保存 iptables 以使规则在重新启动后保持不变。

# service iptables save
日期:2020-09-17 00:12:10 来源:oir作者:oir