iptables

作者: August________ | 来源:发表于2019-11-26 22:16 被阅读0次

iptables

查看

#iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

  • -L:查看当前所有表的规则,默认查看filter表,如果要查看NAT表,可以加上-t NAT参数
  • -n:不对ip地址进行反查。
  • -v:输出详细信息。
  • --line-number:显示规则的行号

添加

  • -A:添加到规则的尾端

  • -I:可以插入到指定位置。

  • 添加一条规则到尾端

#iptables -A INPUT -s 192.168.1.5 -j DROP
  • 插入一条规则到第三行,将行数直接写到规则链的后面
#iptables -I INPUT 3 -s 192.168.1.3 -j DROP

删除

  • -D:删除添加的规则。
#iptables -D INPUT -s 192.168.1.5 -j DROP

修改

  • -R:修改规则
#iptables -R INPUT 3 -j ACCEPT

清除

#iptables -F

匹配条件

类型 选项 用法
通用匹配 协议地址 -p 协议名
地址匹配 -s 源地址 -d 目标地址
接口匹配 -i 收数据的网卡 -o 发数据的网卡
隐含匹配 端口匹配 --sport 源端口 -dport 目标端口
ICMP类型匹配 -icmp-typeICMP类型

​ 内核的IP转发

​ echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf

​ 封禁IP地址/网段

​ 主机型

​ iptables -t filter -A INPUT -s 192.168.4.1 -j DROP

​ iptables -t filter -A INPUT -s 10.0.0.0/24 -j DROP

​ 网络型

​ iptables -t filter -A FORWARD -s 192.168.4.0/24 -j DROP

​ 保护特定的网络服务

​ 限制对指定服务端口的访问

​ iptables -t filter -A INPUT -s 192.168.4.1 -p tcp --dport 22 -j ACCEPT

​ iptables -t filter -A INPUT -s 192.168.4.2 -p tcp --dport 3306 -j DROP

​ 禁ping策略

​ iptables -t filter -A INPUT -p icmp --icmp-type echo-request -j DROP

​ 禁止其他主机的ping包进来

​ iptables -t filter -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT

​ 允许其他主机的pong包进来

​ 亦可在OUTPUT链设置

​ 可用抓包查看

​ 扩展条件

​ 有对应的防火墙模块支持

​ -m 扩展模块 --扩展条件 条件值

​ 例:-m mac --mac-source 源mac地址

linux网关服务开启IP路由转发

​ 配置SNAT

​ iptables -t nat -A POSTROUTING -s 192.168.4.0/24 -p tcp --dport 80 \

​ -j NSAT --to-source 192.168.2.52

​ 地址伪装

​ 外网接口IP不固定

​ iptables -t nat -A POSTROUTING -s 192.168.4.0/24 -p tcp --dport 80 -o eth1 \

​ -j MASQUERADE

相关文章

网友评论

      本文标题:iptables

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