大多数 Linux 发行版将默认运行基于主机的防火墙 iptables。
如果你想让你的主机相互通信,你有两个选择:关闭 iptables 或者配置 iptables 以允许通信。
我更喜欢打开 iptables 并配置访问权限。
保持 iptables 只是整个网络的另一层防御。
本文说明了如何在 CentOS/RHEL 中使用打开或者启用某些端口。
正确配置 iptables 是一项复杂的任务,需要深入的网络知识。
此处提供的示例是简单的参考。
- 检查服务表的状态,如果停止则启动
# service iptables status # service iptables start
# chkconfig --list iptables iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
# chkconfig iptables on
- 检查当前的 iptables 规则(下面的输出显示当前没有设置 iptables 规则)。
# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
- 添加防火墙(iptable)规则允许传入tcp端口(例如22):
# iptables -A INPUT -i eth0 -p tcp --dport 22 -j ACCEPT
- 列出iptables 来验证新添加的规则。
# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp -- anywhere anywhere tcp dpt:ssh Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
重复步骤 3 继续向 Linux 防火墙(iptables)添加端口
https://onitroad.com 更多教程
每次重启后加载规则的过程
- 确保使用上述过程添加了 iptables 规则。
# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp -- anywhere anywhere tcp dpt:ssh Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
- 将 iptables 保存到文件中。
下面命令中的文件名可以是任何内容。
# iptables-save > /root/iptable_rules
- 编辑'/etc/rc.local'文件添加以下条目以在每次重启后恢复iptable规则。
# iptables-restore < /root/iptable_rules
- 保存并关闭文件。
日期:2020-09-17 00:13:19 来源:oir作者:oir