Iptables防火墙
1 位置
使用vim /usr/sysconfig/iptables
2 启动、关闭、保存
- service iptables stop
- service iptables start
- service iptables restart
- service iptables save
3 结构
iptables –> tables –> chains –>rules
3.1 iptables的表与链
iptables具有Filter,NAT,Mangle,Raw四种内建表
3.1.1 Filter表
filter表示iptables的默认表,它具有三种内建链:
- input chain - 处理来之外部的数据
- output chain - 处理向外发送的数据
forward chain- 将数据转发到本机的其它网卡上
3.1.2 NAT表
NAT有三种内建的链:
- prerouting - 处理刚到达本机并在路由转发前的数据包,它会转换数据包中的目标IP地址(destination ip address),通常用于DNAT(destination NAT)。
- postrouting - 处理即将离开本机数据包,它会转换数据包中的源目标IP地址(source ip address),通常SNAT(source NAT)
- output - 处理本机产生的数据包
3.1.3 Mangle表
Mangle表用于指定如何处理数据包,它能改变TCP头中的Qos位,Mangle表具有5个内建链
- prerouting
- output
- forward
- input
- postrouting
3.1.4 Raw表
raw表用户处理异常,它具有2个内建链
- prerouting chain
- output chain
3.2 Iptables规则(Rules)
- rules包括一个条件和一个目标(target)
- 如果满足条件就执行目标target中规则或者特定值
- 如果不满足条件,就判断下一条Rules
3.2.1 目标值
- accept - 允许防火墙接收数据包
- drop - 防火墙丢弃数据包
- queue - 防火墙将数据包移交到用户空间
- return - 防火墙停止执行当前链中的后续rules规则,并返回到调用链(the calling chain)
4 命令
#iptables -t filter -L 查看filter表
#iptables -t nat -L 查看nat表
#iptables -t mangel -L 查看mangel表
#iptables -t raw -L 查看Raw表
例如 以下例子表明在filter表的input链, forward链, output链中存在规则:
# iptables --list
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 RH-Firewall-1-INPUT all -- 0.0.0.0/0 0.0.0.0/0 Chain FORWARD (policy ACCEPT)
num target prot opt source destination
1 RH-Firewall-1-INPUT all -- 0.0.0.0/0 0.0.0.0/0 Chain OUTPUT (policy ACCEPT)
num target prot opt source destination Chain RH-Firewall-1-INPUT (2 references)
num target prot opt source destination
1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
2 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 icmp type 255
3 ACCEPT esp -- 0.0.0.0/0 0.0.0.0/0
4 ACCEPT ah -- 0.0.0.0/0 0.0.0.0/0
5 ACCEPT udp -- 0.0.0.0/0 224.0.0.251 udp dpt:5353
6 ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:631
7 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:631
8 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
9 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
10 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
字段说明
num:编号
target:目标
prot:协议
source:数据包的源IP地址
destination:数据包的目标地址
4.1 清空所有的规则
#iptables –flush
4.2 追加命令
iptables -A命令追加新规则,其中-A表示append,一般而言最后一条规则用于丢弃(drop)所有数据包,并且使用-A参数添加新规则,那么就是无用的。
4.2.1 语法
iptables –A chain firewall-rule
- -A chain 指定要追加的规则的链
- firewall-rule 具体规则的参数
4.2.2 基本参数
用于描述数据包的协议,源地址、目的地址、允许经过的网络接口,以及如何处理这些数据包。
- 协议 –p (protocol)
如tcp,udp,icmp等,可以使用all来指定所有协议不指定-p参数,默认值是all,
可以使用协议名(tcp,udp),或者协议值(6代表tcp),映射关系可以查看/etc/protocols
- 源地址 –s (source)
指定数据包的源地址,参数可以使用IP地址、网络地址、主机名,不指定-s参数,就是代表所有地址。例如:-s 192.168.1.101 具体的IP地址
例如:-s 192.168.1.10/24 指定网络地址
- 目的地址 –d (destination)
指定目的地址,参数和-s相同 - 执行目标 –j (jump to target)
-j代表了当与规则(Rule)匹配时如何处理数据包,可能的值是accept、drop、queue、return,还可以指定其他链(chain)作为目标 - 输入接口 –i (input interface)
指定了要处理来自哪个接口的数据包,这些数据包进入input、forward、prepoute链,不指定将处理进入所有接口的数据包例如:-i eth0 指定了要处理eth0进入的数据
可以取反 !-i eth0,指eth0以外。
可以匹配 -i eth+ 指以eth开头的
- 输出接口 –o (out interface)
数据包有那个接口输出,类似于 –i - 源端口 –sport
例如 –sport 22例如 –sport 22:100指定端口范围
- 目的端口 –dport
类似于-sport - TCP标志
- ICMP类型
5 实例分析
例如:接收目标端口为22的数据包
iptables –A INPUT –i etho –p tcp –dprot 22 –j ACCEPT
例如:拒绝所有其他数据包
iptables –A INPUT –j DROP
6 修改默认策略
上例仅对接收的数据包进行过滤,而对于要发出的数据包却没有任何限制。
使用iptables –L查看防火墙配置时,发现所有的链旁边都有(policy ACCEPT)标注,这表明当前链的默认策略为ACCEPT
# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
DROP all -- anywhere anywhere Chain FORWARD (policy ACCEPT)
target prot opt source destination Chain OUTPUT (policy ACCEPT)
target prot opt source destination
这种情况下,如果没有明确添加DROP规则,那么默认情况下采用ACCEPT策略进行过滤。
6.1 添加DROP规则
iptables –A INPUT –j DROP
iptables –A OUTPUT –j DROP
iptables –A FORWARD –j DROP
6.2 SSH
允许接收远程主机的SSH请求
iptables –A INPUT –i eth0 –p tcp –dport 22 –m state --state NEW,ESTABLISHED –j ACCEPT
允许发送本地主机的SSH响应
iptables –A OUTPUT –o eth0 –p tcp –sport 22 –m state --state ESTABLISHED –j ACCEPT
注意这几个参数
- -m state:启用状态匹配模块(state matching module)
- -- state:状态匹配模块的参数,当ssh客户端第一个数据包到达服务器时,状态字段为NEW,建立连接后数据包的状态字段都是ESTABLISHED
6.3 HTTP
允许接收远程主机的http请求
iptables –A INPUT –i eth0 –p tcp --dport 80 –m state --state NEW ,ESTABLISHED –j ACCPET
允许发送本地主机的Http响应
iptables –A OUTPUT –i eth0 –p tcp -- sport 80 –m state --state ESTABLISHED -j ACCPET
6.4 完成的配置步骤
6.4.1 删除现有的规则
iptables -F
6.4.2 配置默认策略
iptables –P INPUT DROP iptables –P FORWARD DROP iptables –P OUTPUT DROP
6.4.3 允许远程主机进行SSH连接
iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
6.4.4 允许本地主机进行SSH连接
iptables -A OUTPUT -o eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
6.4.5允许http请求
iptables -A INPUT -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
Iptables防火墙的更多相关文章
- CentOS系统配置 iptables防火墙
阿里云CentOS系统配置iptables防火墙 虽说阿里云推出了云盾服务,但是自己再加一层防火墙总归是更安全些,下面是我在阿里云vps上配置防火墙的过程,目前只配置INPUT.OUTPUT和FO ...
- 关闭SELinux和iptables防火墙
1.关闭SELinux: 编辑SELinux配置文件: [root@Redis selinux]# vim /etc/selinux/config 修改SELINUX配置项为disable SELIN ...
- CentOS 7.0,启用iptables防火墙
CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop firewalld.service #停止fir ...
- 编译内核实现iptables防火墙layer7应用层过滤 (三)
在前面的两篇文章中我们主要讲解了Linux防火墙iptables的原理及配置规则,想博友们也都知道iptables防火墙是工作在网络层,针对TCP/IP数据包实施过滤和限制,属于典型的包过滤防火墙.以 ...
- [CentOs7]iptables防火墙安装与设置
摘要 CentOS 7.0默认使用的是firewall作为防火墙,如果改为iptables防火墙,如何操作? 关闭firewall: systemctl stop firewalld.service ...
- iptables防火墙作为基本需求的配置
企业中使用iptables防火墙:(一般不要在命令中输入规则) # Firewall configuration written by system-config-firewall# Manual c ...
- [moka同学摘录]iptables防火墙规则的添加、删除、修改、保存
文章来源:http://www.splaybow.com/post/iptables-rule-add-delete-modify-save.html 本文介绍iptables这个Linux下最强大的 ...
- linux 的iptables防火墙
.a文件就是*.o文件的集合, 是object文件的归档文件, 所以, 用nm -A ???.a看到的 symbolic符合名称都是 相应的, 包含的 .o文件.... linux 2.4内核中 ...
- 使用iptables防火墙限制web的访问PV
使用iptables防火墙限制web的访问PV #!/bin/bash while true;do awk '{print $1}' /tmp/test/access.log| grep -v &qu ...
- Netfilter/iptables防火墙
http://os.51cto.com/art/201107/273443.htm [51CTO独家特稿]Linux系统管理员们都接触过Netfilter/iptables,这是Linux系统自带的免 ...
随机推荐
- service mysql start出错,mysql启动不了,解决mysql: unrecognized service错误
service mysql start出错,mysql启动不了,解决mysql: unrecognized service错误的方法如下: [root@ctohome.com ~]# service ...
- 使用花生壳6.5客户端FTP设置
1.打开FTP客户端—选项—参数选择 2.设置为主动模式(PORT) 3.连接FTP服务器 4.FTP连接成功
- Array-基本功能
<title>Array-基本功能</title></head> <body> <script type="text/javascrip ...
- golang的cgo支持调用C++的方法
1)swift,貌似官网的推荐 2)extern "C" 我使用后者,用起来比较爽,上代码 c.h #pragma once #ifdef __cplusplus extern & ...
- partition-list
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- ionic cordova 热更新
因为项目需要,使用cordova的热更新插件,本地调试很简单,看连接https://github.com/nordnet/cordova-hot-code-push,就几步,这里不说了. 下面两个要装 ...
- 怎样实现excel隔行隔列变色效果的方法
大家在使用excel的过程中,一定见过别人编排的excel文档,隔行添加单元格背景颜色,也就是通常所说的隔行变色效果.Excel中隔行变色效果的好处是:当在Excel中浏览一个非常大的工作簿中的数据时 ...
- jQuery 中 attr() 和 prop() 方法的区别
前几天,有人给 Multiple Select 插件 提了问题: setSelects doesn't work in Firefox when using jquery 1.9.0 一直都在用 jQ ...
- python Shapely 使用指南
翻译:http://toblerity.org/shapely/manual.html 引入包 from shapely.geometry import Point from shapely.geom ...
- C# ASP.NET MVC HtmlHelper用法大全
UrlHrlper 下面的两个地址一样的功能 下边这个防止路由规则改变 比如UserInfo/Index改为UserInfo-Index,使用下面的不受影响 另一种形式的超链接: <%: Htm ...