iptables
表是包含处理特定类型信息包的规则和链的信息包过滤表,iptables内置了三种表:filter、nat、mangle,分别用于实现包过滤,网络地址转换和包重构功能。
1>filter表包含了:INPUT链(处理进入的数据包)、FORWORD链(处理转发的数据包)和OUPUT链(处理本地生成的数据包)在filter表中只允许对数据包进行DROP或ACCEPT操作,而无法对数据包进行更改
2>nat表用于网络地址转换做过NAT操作的数据包的地址就被改变了,当然这种改变是根据规则进行的
PREROUTING链的作用是在包刚刚到达防火墙时改变他的目的地址(如果需要的话)。
OUTPUT链改变本地产生的包的目的地址
POSTROUTING链在包要离开防火墙之前改变其源地址,
操作:
DNAT操作改变的时目的地址,以使包能重路由到某台主机
SNAT改变包的源地址
3>mangle表有五个内建的链:PREROUTING POSTROUTING OUTPUT INPUT FORWORD
mangle表不能做任何的NAT,他只是改变数据包的TTL TOS MARK 而不是其源 目的地址
iptables
-t ##指定表名称,若未指定表,则默认为filter表
-n ##不做解析 -L ##列出指定表中的策略 -A ##增加策略 -p ##网络协议 --dport ##端口 -s ##数据来源
-j ##动作 ACCEPT ##允许 REJECT ##拒绝 DROP ##丢弃,不拒绝访问,但是不予理睬,会一直在保持接收的状态
-N ##增加链 -E ##修改链名称 -X ##删除链 -D ##删除指定策略 -I ##插入 -R ##修改策略 -P ##修给默认策略
一、iptables服务开启
systemctl stop firewalld
systemctl disable firewalld
systemctl start iptables.service
systemctl enable iptables.service
iptables -nL 查看策略
iptables -F 刷新策略
iptables -nL 查看策略
service iptables save 保存
二、简单设置
iptables -A INPUT -s 172.25.254.55 -p tcp --dport 22 -j ACCEPT (172.25.254.55可以连接22(ssh服务端口)
iptables -A INPUT -p tcp --dport 22 -j REJECT (其他人不可以连接22端口)
iptables -nL
三、设置的增添、删除、修改
iptables -P(大写) INPUT DROP (丢弃) 设置filter表INPUT链的默认规则 ,当数据包没有被规则列表里的任何规则匹配到时,按此默认规则处理
iptables -P(大写) INPUT ACCEPT (接受)
iptables -A INPUT -p tcp --dport 80 -j REJECT
iptables -A INPUT -s 172.25.254.55 -p tcp --dport 80 -j ACCEPT (允许172.25.254.55访问80端口但此时并不能生效因为此规则在后面,当识别到规则时找到满足要求的规则时只识别第一个)
iptables -nL
iptables -D INPUT 2 删掉刚才添加的规则的第二条
iptables -nL
iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT (添加到第一条规则)
iptables -nL
iptables -D INPUT 1 删掉刚才添加的规则的第一条
iptables -nL
iptables -I INPUT 1 -p tcp --dpor 80 -j ACCEPT (添加到第一条规则)
iptables -nL
iptables -R INPUT 1 -s 172.25.254.55 -p tcp --dport 80 -j ACCEPT 修改第一条规则
iptables -nL
iptables -t filter -I INPUT 2 -j ACCEPT 添加规则到第二条
iptables -nL
四、iptables中链路的增添删除
1 iptables -nL
2 iptables -N westos 添加链路westos
3 iptables -nL
4 iptables -E westos WESTOS 将链路westos改为WESTOS
5 iptables -nL
6 iptables -X WESTOS 删除链路WESTOS
7 iptables -nL
8 iptables -F 刷新
五、设置对正在进行的以及以前进行过的都直接允许访问,只检测第一次访问的