目录

iptables的使用

iptables是用来设置、维护和检查Linux内核的IP包过滤规则的。

重置统计数据

注意:这里是重置所有端口的统计数据

  1. 重置所有输入端口 iptables -Z INPUT
  2. 重置所有输出端口 iptables -Z OUTPUT

移除统计端口

  1. 移除输入端口
    iptables -D INPUT -p tcp –dport 8080 //iptables -D INPUT -p tcp –dport 8080 -d ip(destination)
  2. 移除输出端口 iptables -D OUTPUT -p tcp –sport 8080 //iptables -D OUTPUT -p tcp –sport 8080 -s ip(destination)
  3. 有时候要删除的规则太长,删除时要写一大串,既浪费时间又容易写错,这时我们可以先使用–line-number找出该条规则的行号,再通过行号删除规则。
iptables -nL --line-number

Chain INPUT (policy ACCEPT)   
num  target     prot opt source              destination   
1    DROP       all  --  192.168.1.1          0.0.0.0/0   
2    DROP       all  --  192.168.1.2          0.0.0.0/0  
3    DROP       all  --  192.168.1.3          0.0.0.0/0  

删除第二行规则  
iptables -D INPUT 2  

获取所有开启监视端口的上行/下行流量

iptables -nvL //以最大单位显示
iptables -nvL -x //以字节显示
指定单一端口匹配:
iptables -A INPUT -p tcp –dport 8381 -j ACCEPT //相当于上传量
iptables -A OUTPUT -p tcp –sport 8381 -j ACCEPT //相当与下载量

iptables -A INPUT -p tcp –dport 8381 -j ACCEPT && iptables -A OUTPUT -p tcp –sport 8381 -j ACCEPT //一条命令检测端口流量

  • iptables -nvL
Chain INPUT (policy ACCEPT 9097 packets, 25M bytes)  
 pkts bytes target     prot opt in     out     source               destination  
    0     0            tcp  --  *      *       0.0.0.0/0            192.168.0.10         tcp dpt:80  
    0     0            tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80  
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80  
18437 1400K ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:8381  

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)  
 pkts bytes target     prot opt in     out     source               destination  

Chain OUTPUT (policy ACCEPT 17008 packets, 28M bytes)  
 pkts bytes target     prot opt in     out     source             destination  
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:8381  
  • iptables -nvL -x
Chain INPUT (policy ACCEPT 9599 packets, 35463063 bytes)  
    pkts      bytes target     prot opt in     out     source               destination  
       0        0            tcp  --  *      *       0.0.0.0/0            192.168.0.10         tcp dpt:80  
       0        0            tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80  
       0        0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80  
   84249  6014848 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:8381  

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)  
    pkts      bytes target     prot opt in     out     source               destination  

Chain OUTPUT (policy ACCEPT 6100 packets, 385963 bytes)  
    pkts      bytes target     prot opt in     out     source               destination  
       0        0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:8381  
   14270 37000676 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp spt:8381