iptables Linux防火墙配置工具

来源:互联网 发布:淘宝nala彩妆是正品吗 编辑:程序博客网 时间:2024/05/17 23:20

iptables Linux防火墙配置工具


iptables 是一个配置 Linux 内核 防火墙 的命令行工具

基础架构


  1. 请看http://lesca.me/archives/iptables-tutorial-structures-configuratios-examples.html
  2. 以及https://wiki.archlinux.org/index.php/Iptables_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)

查看当前规则


sudo iptables -nvL --line-numbers

[admin 23:20:38 ~]:sudo iptables -nvL –line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 416 37142 ACCEPT all – * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
2 0 0 ACCEPT icmp – * * 0.0.0.0/0 0.0.0.0/0
3 0 0 ACCEPT all – lo * 0.0.0.0/0 0.0.0.0/0
4 3 156 ACCEPT tcp – * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
5 43 4411 REJECT all – * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 REJECT all – * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 265 packets, 43025 bytes)
num pkts bytes target prot opt in out source destination

每个链中都显示了通过该链的包数和字节数

重置规则


iptables -Z //封包计数清零
iptables -F //刷新链
iptables -X //清除默认规则
iptables -t nat -F //指定表
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -t raw -F
iptables -t raw -X
iptables -t security -F
iptables -t security -X
iptables -P INPUT ACCEPT //设定默认进入系统的数据包处理规则
iptables -P FORWARD ACCEPT //设定默认进入系统需要转发的数据包处理规则
iptables -P OUTPUT ACCEPT //设定默认从系统发出的数据包处理规则

编辑规则


有两种方式添加规则,一种是在链上附加规则,另一种是将规则插入到链上某个特定位置。这里将讲解这两种方式。

一个建议,由于我们的系统不是路由器,因此应该禁止所有的转发,用如下操作:

iptables -A FORWARD DROP

Dropbox 的局域网同步特性 — 每30秒广播数据包到所有可视的计算机,如果我们碰巧在一个拥有 Dropbox 客户端的局域网中,但是我们不想使用这个特性,那么我们希望拒绝这些数据包。

iptables -A INPUT -p tcp –dport 17500 -j REJECT –reject-with icmp-port-unreachable

现在,我们改变对 Dropbox 的看法,并且决定安装其到我们的计算机,我们也希望局域网同步,但是我们的网络只有一个特定的 IP 地址,因此需要使用 -R 参数来替换旧规则,10.0.0.85 是我们的另一个 IP 地址。

iptables -R INPUT 1 -p tcp –dport 17500 ! -s 10.0.0.85 -j REJECT –reject-with icmp-port-unreachable

因此我们写一条新规则来立即允许受信任的用户。使用 -I 参数来在旧规则前插入一条新规则:

iptables -I INPUT -p tcp –dport 17500 -s 10.0.0.85 -j ACCEPT -m comment –comment “Friendly Dropbox”

更改第二条规则,使其拒绝 17500 端口的任何数据包:

iptables -R INPUT 2 -p tcp –dport 17500 -j REJECT –reject-with icmp-port-unreachable

一套常用配置

iptables -P INPUT ACCEPT # 设置INPUT chain默认行为为接受
iptables -P OUTPUT ACCEPT # 设置OUTPUT chain默认行为为接受
iptables -P FORWARD ACCEPT # 设置FORWARD chain默认行为为接受
iptables -A INPUT -i lo -j ACCEPT # 接受本地会回环(127.0.0.1)
iptables -A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT # 接受已建立的 联建和与已建立的链接有关系的链接
iptables -A INPUT -p icmp -m icmp –icmp-type 8 -j ACCEPT # 接受回显请求的ping
iptables -A INPUT -p tcp -m tcp –dport 22 -j ACCEPT # 开放ssh端口
iptables -A INPUT -p tcp -m tcp –dport 80 -j ACCEPT
iptables -A INPUT -p tcp -m udp –dport 443 -j ACCEPT

参考文档

  1. https://wiki.archlinux.org/index.php/Iptables_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)
  2. http://www.mojidong.com/linux/2013/11/12/linux-iptables/
  3. http://lesca.me/archives/iptables-tutorial-structures-configuratios-examples.html
  4. man 8 iptables
0 0
原创粉丝点击