美文网首页
iptables 命令学习

iptables 命令学习

作者: 全都是泡沫啦 | 来源:发表于2019-11-03 22:30 被阅读0次
iptables -t filter -A INPUT -j DROP #拒绝所有人访问服务器(主动访问服务器,或服务器访问外界的响应数据)
iptables -L          # 默认为filter表,-L查看表中所有的链上的规则,policy为默认规则   例如(policy ACCPT)
iptables -vxnL
v:显示详细信息,包括每条规则的匹配包数量和匹配字节数
x:在v的基础上,禁止自动单位换算(K M)
n:只显示ip地址和端口号码,不显示域名和服务名称

iptables -D INPUT 1  # 默认为filter表,1为INPUT链上第一条规则
iptables -D INPUT -s 192.168.0.1 -j DROP #按内容匹配删除filter表INPUT链中内容为‘-s 192.168.0.1 -j DROP’ 的规则
iptables -P INPUT DROP  #设置filter表INPUT链上的默认规则是DROP

iptables -F INPUT  # 默认为filter表,清除INPUT链上的规则 不影响-P设置的默认规则
iptables -F        # 默认为filter表,清除所有链上的规则 不影响-P设置的默认规则

注意: -P 设置为DROP后,使用-F一定要小心。可以配置crontab 配置22端口常开;

匹配条件:
流入和流出接口(-i -o)
    i eth0 匹配是否从网络接口eth0进来
来源和目的地址(-s -d)
        ip   -s 192.168.0.1
        网段 -s 192.168.0.0/24
        域名 -s www.baidu.com
        空(任何地址)
协议类型      (-p)
    -p tcp
    -p udp
    -p icmp --icmp-type 类型   ping:type=8 pong:type=0
来源和目的端口(--sport --dport) 必须配合-p参数使用
    --sport 80   ==80
    --sport 6000:8000  6000<= <=8000
    --sport :3000  <=3000
    --sport 1000:  >=1000
动作
ACCEPT  接受
DROP    丢弃
REJECT  拒绝(给回应)
SNAT    
DNAT
MASQUERADE  

实现上网
-j SNAT --to IP[-IP][:端口-端口](net表的POSTROUTING链)  #源地址转换
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 \
 -j SNAT --to 1.1.1.1

实现端口数据转发 
-j DNAT --to IP[-IP][:端口-端口](net表的PREROUTING链)  #目的地址转换
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to 192.168.0.1:81

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to 192.168.0.1-192.168.0.10:80  #有点类似于负载均衡

动态源地址转换(动态ip的情况下使用)
iptables -t net -A POSTROUTING -s 192.168.0.0./24 -o eth0 -j MASQUERADE 将源地址是192.168.0.0/24的数据包进行地址伪装,转换成eth0上的ip地址,eth0为口尤其外网的出口ip地址

附加模块
按包的状态匹配 (state)
按来源mac匹配  (mac)
按包的速率匹配 (limit)
iptables -A FORWARD -d 192.168.0.1 -m limit --limit 50/s -j ACCEPT
iptables -A FORWARD -d 192.168.0.1 -j DROP   (要想限制需要加一条DROP)
多端口匹配     (multiport) 必须配合-p 使用
iptables -A INPUT -p tcp -m mutiport --dports 21,22,25,80,110 -j ACCEPT  #允许所有客户端访问本服务器的21 22 25 80 110端口上的tcp服务 

iptables保存和恢复
iptables–save > /etc/iptables/iptables.rules
iptables–restore < /etc/iptables/iptables.rules

=====配置web服务器的防火墙
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -p tcp -m multiport --dports 22,80 -j ACCEPT
    或者
    iptables -A INPUT -p tcp --dport 22 -j ACCEPT
    iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -m state --state RELATED,ESTABLISHED   -j ACCEPT
#允许已经建立tcp链接的包以及该连接相关的包通过,状态防火墙能识别tcp或者udp会话,非状态防火墙只能根据端口识别,不能识别会话
iptables -P INPUT DROP

image.png
image.png
image.png
image.png
image.png

相关文章

网友评论

      本文标题:iptables 命令学习

      本文链接:https://www.haomeiwen.com/subject/ownlbctx.html