linux防火墙iptables规则详解
Posted 一口Linux
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux防火墙iptables规则详解相关的知识,希望对你有一定的参考价值。
原文:https://www.toutiao.com/i6956964690006295078/
Centos7默认防火墙使用firewall,firewall的使用请参看:Centos7防火墙基本操作
iptables相比firewall更加复杂,也更加专业,对于专业运维对于iptables更加偏爱。
用法:
iptables [-t table] command chain 匹配标准(ip、协议、端口) -j Target
chain:PREROUTNG,INPUT,FORWARD,OUTPUT,POSTROUTING,一般使用INPUT和OUTPUT
-t talbe 有4表filter raw managle nat,默认filter,
-j Target:指定如何处理(DROP or ACCEPT) Drop指防火墙丢弃数据包,而Accept则是防火墙接收数据包
INPUT和OUTPUT可以理解为请求和响应
看用法是不是有点晕,我们赶紧举个例子,来让你见识一下防火墙的厉害
-A 参数表示追加规则
iptables -t filter -A INPUT -s 192.168.3.2 -j DROP 不允许192.168.3.2 访问服务器
iptables -t filter -A OUTPUT -s 192.168.1.2 -j DROP 理解为不给源IP回应,相当于不允许192.168.1.2访问本机(其他类似分析)
iptables -t filter -A OUTPUT -p tcp --dport 80 -j DROP 禁止访问外网的web
iptables -t filter -A -p tcp --dport 21 -j DROP 禁用FTP访问
查看配置的防火墙规则
iptable防火墙
iptables -t filter -L -n 将filter表中的IP以IP地址表示
iptables -t filter -L --line-numbers 查看规则并增加行号
修改默认策略 -P 参数
iptables -t filter -P INPUT DROP (INPUT链的默认规则为DROP,意思就是不允许其他机器连接过来)
删除规则
iptables -t filter -D chain NUM (格式)
iptables -t filter -L OUTPUT --line-number (获取防火墙规则的num)
iptables -t filter -D OUTPUT 1
iptables -t filter -D OUTPUT -p tcp --dport 80 -j DROP
清空规则
iptables -t filter -F OUTPUT 清除OUTPUT链
iptables -t filter -F 清除所有链的规则
插入规则
iptables -t filter -I INPUT 1 -p tcp --dport 8080 -j DROP 在1号规则后面加该规则,该规则表示不允许访问本机的8080端口
根据IP地址配置规则 -s 指定源IP -d 指定目的IP
iptables -t filter -A INPUT -d 192.168.1.2 -j DROP
iptables -t filter -A OUTPUT -s 192.168.1.2/23 -j DROP(对ip段封堵)
根据协议
iptables -A OUTPUT -p icmp -j ACCEPT (允许ping,ping是icmp协议)
iptables -A INPUT -p tcp --dport 22 -j ACCEPT (允许sftp)
以上是关于linux防火墙iptables规则详解的主要内容,如果未能解决你的问题,请参考以下文章