iptables防火墙使用

iptables是基于内核的防火墙,内置filter,nat,mangle三张表

  • filter表:过滤数据包(input,output,forward)
  • nat表:网络地址转换(prerouting,postrouting)
  • mangle表:修改数据包内容

5个链:

  1. input:匹配目标ip是本机的包
  2. output:一般不在此链做配置
  3. forward:匹配经过本机数据包
  4. prerouting:修改目的地址DNAT
  5. postrouting:修改源地址SNAT

防火墙转发:prerouting--------------forward----------------postrouting

发给防火墙:prerouting--------------input-------------output------------postrouting

  • 查看防火墙是否安装:rpm -qa iptables
  • 配置文件位置:/etc/sysconfig/iptables
  • 启动:/etc/init.d/iptables start
  • 查看是否开机启动:chkconfig --list iptables

iptable使用规则:
iptables -t 表名 管理选项 链名 匹配条件 -j 动作

  • iptables -L 查看规则
  • iptables -D INPUT 1 删除INPUT表里第一条规则
  • iptables -D INPUT -s 192.168.1.1 -j DROP 删除INPUT链表里源地址为192.168.1.1的数据包
  • iptables -P INPUT DROP 修改INPUT链表的默认规则
  • iptables -F INPUT 清空INPUT链表里全部规则
  • iptables -F 清空所有链表规则
  • iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j SNAT – to 10.1.12.4 将源地址为192.168.0.0/24网段转为10.1.12.4
  • iptables -t nat -A POSTROUTING -i eth0 -p tcp – dport 80 -j DNAT --to 192.168.51.1:80 将从eth0口收到的要访问80端口的tcp协议数据包转到192.168.51.1的80端口

包匹配状态:

  • NEW:刚建立连接
  • ESTABLISH:已建立连接
  • RELATED:建立连接后关联协议,如FTP
  • INVALID:不能识别属于哪个连接

包的使用:

  • iptables -A INPUT -m state --state RELATED,ESTABLISH -j ACCEPT 接受所有
  • iptables -A INPUT -m mac --mac-source xx:xx:xx:xx:xx -j DROP 阻断来自指定mac的数据包
  • iptables -A FORWARD -d 192.168.0.1 -m limit --limit 50/s -j ACCEPT 接受目的地址为192.168.0.1的数据包,每秒钟50个

多端口匹配:

  • iptables -A INPUT -p tcp -m multiport --dports 21,22,80 -j ACCEPT 接受目的端口为21,22,80的包

🎈注意事项:

  1. 配置后立即生效不需要重启
  2. REJECT:拒绝,有回应
  3. DROP:丢弃,没有回应
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值