linux下Iptables防火墙介绍
1、简介
iptables是linux/unix自带的一款开源基于包过滤的防火墙工具,使用非常灵活,对硬件资源需求不是很高,是在内核中集成的服务,主要工作在OSI的二、三、四层。
术语介绍:
Netfilter:是表的容器 表:链的容器 链:规则的容器 规则:iptables一系列过滤信息的规范和具体方法
工作流程:
客户端请求数据------》iptables Filter-------》获取主机的服务(直接拒绝Drop)
1 2 3 4 | 数据包————过滤规则 1————过滤规则 2————默认规则 拒绝就 Drop--------------------------------后面的规则不起作用 没有匹配————拒绝就 Drop--------------后面的规则不起作用 前两个规则都没有匹配—————默认规则去过滤 |
防火墙是层层过滤,数据包的匹配规则是自上而下顺序匹配,如果前面都没有匹配上规则(这个匹配规则是无论通过还是拒绝都是匹配上规则),明确通过或阻止,最后交给防火墙默认规则去处理
2、常用表的介绍
常用的表有:filter、nat、mangle
完整过程:
1、数据包进入----经过NAT PREROUTING----经过 FORWARD----FILTER INPUT----NAT OUTPUT----FILTER OUTPUT----NAT POSTROUTING 主要用于NAT或端口映射
2、数据包经过----经过 FORWARD----FILTER FORWARD----NAT POSTROUTING 主要用于过滤
3、帮助信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | [root@VM_0_7_centos ~] # iptables -h iptables v1.4.7 Usage: iptables -[ACD] chain rule-specification [options] iptables -I chain [rulenum] rule-specification [options] iptables -R chain rulenum rule-specification [options] iptables -D chain rulenum [options] iptables -[LS] [chain [rulenum]] [options] iptables -[FZ] [chain] [options] iptables -[NX] chain iptables -E old-chain-name new-chain-name iptables -P chain target [options] iptables -h (print this help information) Commands: Either long or short options are allowed. --append -A chain Append to chain 把规则添加到链的结尾 --check -C chain Check for the existence of a rule --delete -D chain Delete matching rule from chain 删除匹配规则从链中 --delete -D chain rulenum Delete rule rulenum (1 = first) from chain --insert -I chain [rulenum] Insert in chain as rulenum (default 1=first) 把规则添加到链的开头 --replace -R chain rulenum Replace rule rulenum (1 = first) in chain --list -L [chain [rulenum]] List the rules in a chain or all chains #以列表的形式查看 --list-rules -S [chain [rulenum]] Print the rules in a chain or all chains --flush -F [chain] Delete all rules in chain or all chains #清除所有规则 --zero -Z [chain [rulenum]] Zero counters in chain or all chains #清除计数器 --new -N chain Create a new user-defined chain #以数字的形式查看 --delete-chain -X [chain] Delete a user-defined chain #清除用户自定义链 --policy -P chain target Change policy on chain to target #将链策略更改为目标 --rename-chain -E old-chain new-chain Change chain name, (moving any references) Options: [!] --proto -p proto protocol: by number or name, eg. `tcp' 指定端口类型,如tcp,udp [!] -- source -s address[ /mask ][...] source specification 原规则(-s 后面跟IP地址) [!] --destination -d address[ /mask ][...] destination specification [!] -- in -interface -i input name[+] network interface name ([+] for wildcard) 网络接口名称(后面跟网络接口,如eth0) --jump -j target target for rule (may load target extension) 目标规则 --goto -g chain jump to chain with no return --match -m match extended match (may load extension) --numeric -n numeric output of addresses and ports #端口和地址的数字输出 [!] --out-interface -o output name[+] network interface name ([+] for wildcard) 网络接口名称(是output的网络接口名称,和-i区别开来) --table -t table table to manipulate (default: `filter') #指定表 --verbose - v verbose mode --line-numbers print line numbers when listing --exact -x expand numbers (display exact values) [!] --fragment -f match second or further fragments only --modprobe=< command > try to insert modules using this command -- set -counters PKTS BYTES set the counter during insert /append [!] --version -V print package version. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | [root@VM_0_7_centos ~] # /etc/init.d/iptables status 查看iptables的运行状态 Table: filter Chain INPUT (policy ACCEPT) num target prot opt source destination Chain FORWARD (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination [root@VM_0_7_centos ~] # iptables -L -n 查看表列表和端口地址 Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination |
清除规则
1 2 3 4 5 6 7 8 9 10 | [root@VM_0_7_centos ~] # iptables -F 清除iptables规则 [root@VM_0_7_centos ~] # iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination |
清除指定表 iptablers -F -t 表名
1 | [root@VM_0_7_centos ~] # iptables -t filter -A INPUT -p tcp --dport 22 -j DROP |
因为默认的是filter表,所以和iptables -F一样
配置防火墙前最好写一个定时任务,每多长时间就关闭防火墙,这样就可以防止无法远程登录
-A 把规则添加到链的结尾
-I 把规则添加到链的开头
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | [root@VM_0_7_centos ~] # iptables -t filter -A INPUT -p tcp --dport 9001 -j DROP [root@VM_0_7_centos ~] # iptables -t filter -A INPUT -p tcp --dport 9002 -j DROP [root@VM_0_7_centos ~] # /etc/init.d/iptables status Table: filter Chain INPUT (policy ACCEPT) num target prot opt source destination 1 DROP tcp -- 0.0.0.0 /0 0.0.0.0 /0 tcp dpt:9001 2 DROP tcp -- 0.0.0.0 /0 0.0.0.0 /0 tcp dpt:9002 Chain FORWARD (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination [root@VM_0_7_centos ~] # iptables -L -n Chain INPUT (policy ACCEPT) target prot opt source destination DROP tcp -- 0.0.0.0 /0 0.0.0.0 /0 tcp dpt:9001 DROP tcp -- 0.0.0.0 /0 0.0.0.0 /0 tcp dpt:9002 Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination |
在实际的企业应用中,如果默认的规则是允许的,但是我又得禁止某一个应用或服务时,就会用到-I参数,将规则放在最前面,比如你发现一个网站被一个IP频繁访问,就可以用它来禁止
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [root@VM_0_7_centos ~] # iptables -t filter -I INPUT -p tcp -s 10.0.100.1 --dport 9003 -j ACCEPT [root@VM_0_7_centos ~] # /etc/init.d/iptables status Table: filter Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT tcp -- 10.0.100.1 0.0.0.0 /0 tcp dpt:9003 2 DROP tcp -- 0.0.0.0 /0 0.0.0.0 /0 tcp dpt:9001 3 DROP tcp -- 0.0.0.0 /0 0.0.0.0 /0 tcp dpt:9002 Chain FORWARD (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination |
删除某一行规则
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | [root@VM_0_7_centos ~] # /etc/init.d/iptables status Table: filter Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT tcp -- 10.0.100.1 0.0.0.0 /0 tcp dpt:9003 2 DROP tcp -- 0.0.0.0 /0 0.0.0.0 /0 tcp dpt:9001 3 DROP tcp -- 0.0.0.0 /0 0.0.0.0 /0 tcp dpt:9002 Chain FORWARD (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination [root@VM_0_7_centos ~] # iptables -D INPUT 3 [root@VM_0_7_centos ~] # /etc/init.d/iptables status Table: filter Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT tcp -- 10.0.100.1 0.0.0.0 /0 tcp dpt:9003 2 DROP tcp -- 0.0.0.0 /0 0.0.0.0 /0 tcp dpt:9001 Chain FORWARD (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination |
禁止某网段的流量
[root@VM_0_7_centos ~]# iptables -A INPUT -i eth0 -s 10.0.0.0/24 -j DROP
1 2 3 4 5 6 7 8 9 10 11 12 13 | <br>[root@VM_0_7_centos ~] # /etc/init.d/iptables status Table: filter Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT tcp -- 10.0.100.1 0.0.0.0 /0 tcp dpt:9003 2 DROP tcp -- 0.0.0.0 /0 0.0.0.0 /0 tcp dpt:9001 3 DROP all -- 10.0.0.0 /24 0.0.0.0 /0 Chain FORWARD (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination |
iptables企业级应用配置
清除规则
1 2 3 4 5 6 7 8 9 10 11 12 13 | [root@VM_0_7_centos ~] # iptables -F [root@VM_0_7_centos ~] # iptables -X [root@VM_0_7_centos ~] # iptables -Z [root@VM_0_7_centos ~] # /etc/init.d/iptables status Table: filter Chain INPUT (policy ACCEPT) num target prot opt source destination Chain FORWARD (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination |
配置规则使得内网用户可用
1 2 3 4 | [root@VM_0_7_centos ~] # iptables -A INPUT -s 100.1.3.9/24 -j ACCEPT [root@VM_0_7_centos ~] # iptables -A INPUT -p tcp --dport 22 -j ACCEPT [root@VM_0_7_centos ~] # iptables -A INPUT -i lo -j ACCEPT [root@VM_0_7_centos ~] # iptables -A OUTPUT -o lo -j ACCEPT |
设置规则(默认规则)
1 2 3 | [root@VM_0_7_centos ~] # iptables -P INPUT DROP [root@VM_0_7_centos ~] # iptables -P FORWARD DROP [root@VM_0_7_centos ~] # iptables -P OUTPUT DROP |
配置用户访问web页面(这个不能禁止,因为网站是开放给全球用户访问)
1 2 3 4 5 6 7 8 9 10 11 12 | [root@VM_0_7_centos ~] # iptables -t filter -A INPUT -p tcp --dport 80 -j ACCEPT [root@VM_0_7_centos ~] # /etc/init.d/iptables status Table: filter Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT tcp -- 0.0.0.0 /0 0.0.0.0 /0 tcp dpt:80 Chain FORWARD (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination |
因为所有iptables命令都是保存在内存中的,重启计算机就会失效
1 2 3 4 5 | [root@VM_0_7_centos ~] # cp /etc/sysconfig/iptables /etc/sysconfig/iptables.bak [root@VM_0_7_centos ~] # /etc/init.d/iptables save iptables: Saving firewall rules to /etc/sysconfig/iptables :[ OK ] 成功保存iptables规则 |
维护iptables防火墙
1 2 3 4 5 6 7 8 9 | [root@VM_0_7_centos ~] # vim /etc/sysconfig/iptables # Generated by iptables-save v1.4.7 on Fri Mar 23 11:13:52 2018 *filter :INPUT ACCEPT [938:61166] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [872:82030] -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT COMMIT # Completed on Fri Mar 23 11:13:52 2018 |

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
CentOS 7安装指南(U盘版)
一、准备阶段 1、下载CentOS7镜像文件(ISO文件)到自己电脑,官网下载路径: 下载 如下图 图1-1 2、制作U盘,并写入映像文件。 部分图片选自:https://jingyan.baidu.com/article/cdddd41c68d72753ca00e143.html 准备:8G U盘一个,需要格式化(大于4G,毕竟ISO文件就已经大于4G了)。 在电脑上安装最新版UltraISO软件,见下图 图1-2 安装完成之后运行UltraISO软件 图1-3 选择文件---打开 图1-4 找到你下载的Centos7的DVD文件,ISO格式的。 加载完成之后可以看到UltraISO中的Centos7文件 图1-5 选择启动--->写入硬盘镜像 图1-6 选择你的U盘 选择写入方式为:USB-HDD+V2 附上:各种U盘启动模式简介 1、USB-HDD:硬盘仿真模式,DOS启动后显示C:盘,HPU盘格式化工具制作的U盘即采用此启动模式。此模式兼容性很高,但对于一些只支持USB-ZIP模式的电脑则无法启动。 2、USB-ZIP:大容量软盘仿真模式,DOS启动后显示A盘...
- 下一篇
CentOS6.5安装mysql以及常见问题的解决
前言 最近在学习Linux系统,今天在安装MySQL数据库时出现很多问题,花费了两个小时终于解决,故记录下来以供大家参考。(本人目前还在学习阶段,下面写到的是自己结合网上查到的资料以及各位前辈给出的解决方法综合得出的,如有不对的地方,还请各位指出。本人也是第一次也博客,排版问题还请各位多多包涵。) 运行环境 虚拟机:VMware Workstation15 Linux版本:CentOS6.5 MySQL版本:5.6.22 安装方式的选择 Linux下安装MYSQL有三种方式: 1. 通过yum命令在线下载安装 2.下载离线rpm安装包安装 3.下载源码编译安装 这里我使用第二种方式,其他方式请自行查阅。 MySQL的安装 1.查看CentOS自带的mysql rpm -qa | grep mysql 2.卸载原装mysql rpm -e --nodeps 相应mysql版本 3.上传mysql到Linux系统 我使用SSH Communications Security进行上传 4.解压mysql至usr/local/mysql目录下,mysql目录需要自己创建 cd /usr/...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- CentOS7设置SWAP分区,小内存服务器的救世主
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- SpringBoot2全家桶,快速入门学习开发网站教程
- Docker安装Oracle12C,快速搭建Oracle学习环境
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- MySQL8.0.19开启GTID主从同步CentOS8
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7