Linux 防火墙:Netfilter iptables
一、Netfilter 简介
(1) Netfilter 是 Linux 内置的一种防火墙机制,我们一般也称之为数据包过滤机制,而 iptables 只是操作 netfilter 的一个命令行工具
(2) Netfilter 是 Linux CentOS 6 内置的防火墙机制,Firewall 是 Linux CentOS 7 内置的防火墙机制,如果想在 CentOS 7 中使用 netfilter 而不是 firewall,操作如下
[root@MongoDB ~]# systemctl stop firewalld.service // 停止firewalld
[root@MongoDB ~]# systemctl disable firewalld.service // 禁止firewall开机启动
[root@MongoDB ~]# yum install iptables-services -y
[root@MongoDB ~]# systemctl start iptables.service //启动防火墙
[root@MongoDB ~]# systemctl enable iptables.service // 设置防火墙开机启动
centos7 iptables服务
1.查看iptables服务状态
[root@MongoDB ~]#systemctl status iptables
systemctl start iptables // 启动iptables服务
systemctl restart iptables // 重启iptables服务
systemctl stop iptables // 关闭iptables服务
centos6 iptables 服务
service iptables start // 启动 iptables服务
service iptables restart // 重启iptables服务
service iptables stop // 关闭iptables服务
service iptables status // 查看iptables服务状态
/etc/init.d/iptables stop // 关闭iptables服务
/etc/init.d/iptables status // 查看iptables状态
2.查看规则,默认查看filter表的规则
iptables -nvL
针对INPUT链 默认规则ACCEPT通过
iptables -nvL
Chain INPUT (policy ACCEPT packets, bytes) // 针对INPUT链 默认规则
// INPUT链 规则
pkts bytes target prot opt in out source destination
858K ACCEPT all -- * * 0.0.0.0/ 0.0.0.0/ state RELATED,ESTABLISHED
ACCEPT icmp -- * * 0.0.0.0/ 0.0.0.0/
ACCEPT all -- lo * 0.0.0.0/ 0.0.0.0/
ACCEPT tcp -- * * 0.0.0.0/ 0.0.0.0/ state NEW tcp dpt:
ACCEPT tcp -- * * 0.0.0.0/ 0.0.0.0/ state NEW tcp dpt:
3205K REJECT all -- * * 0.0.0.0/ 0.0.0.0/ reject-with icmp-host-prohibited // FORWARD链 默认规则
Chain FORWARD (policy ACCEPT packets, bytes)
pkts bytes target prot opt in out source destination
REJECT all -- * * 0.0.0.0/ 0.0.0.0/ reject-with icmp-host-prohibited // OUTPUT链 默认规则
Chain OUTPUT (policy ACCEPT packets, 950K bytes)
pkts bytes target prot opt in out source destination
二、iptables 常见用法:
iptables -F # 清空规则,默认清空filter表的规则
iptables -X # 删除用户自定义规则
iptables -Z # 清空链的计数器
service iptables save # 保存规则,会把当前规则保存到/etc/sysconfig/iptables
iptables-save > my.ipt # 备份规则,这里指定备份到my.ipt文件
iptables-restore < my.ipt # 恢复规则,这里指定使用my.ipt文件进行恢复
清空所有的规则,只留下默认规则
iptables -F 清空规则,默认清空filter表的规则
只留下默认规则 默认规则都是ACCEPT动作
[root@MongoDB ~]# iptables -F
[root@MongoDB ~]# iptables -nvL
Chain INPUT (policy ACCEPT packets, bytes)
pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT packets, bytes)
pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT packets, bytes)
pkts bytes target prot opt in out source destination
iptables -X 删除用户定义规则
iptables -Z 清空链的计数器
iptables -A INPUT -p tcp --dport 80 -j ACCEPT # 放通80端口
iptables -A INPUT -p tcp --dport 22 -j DROP # 禁用22端口
iptables -A INPUT -p tcp -m multiport --dport 80,843,443 -j ACCEPT # 放通多个端口
iptables -A INPUT -s 192.168.1.1/32 -p tcp --dport 22 -j ACCEPT # 只允许某个IP访问22端口
iptables -A INPUT -s 192.168.1.1/32 -p tcp -m multiport --dport 3873,4507,3306 -j ACCEPT # 允许某个IP访问多个端口
添加一条规则 到filter表 允许8080端口
[root@MongoDB ~]# iptables -t filter -A INPUT -p tcp --dport 8080 -j ACCEPT
删除一条规则
先执行 iptables -nvL --line-numbers 查看规则的序列号
[root@MongoDB ~]# iptables -nvL --line-numbers
Chain INPUT (policy ACCEPT 93 packets, 7775 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 59 packets, 6900 bytes)
num pkts bytes target prot opt in out source destination
-D 删除规则
然后执行 iptables -D INPUT <number> 删除规则
[root@MongoDB ~]# iptables -D INPUT 1
[root@MongoDB ~]#
[root@MongoDB ~]# iptables -nvL --line-numbers
Chain INPUT (policy ACCEPT 14 packets, 1020 bytes)
num pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 6 packets, 728 bytes)
num pkts bytes target prot opt in out source destination
/etc/init.d/iptables restart 和 /etc/init.d/iptable reload 区别
相同点:二者都是使配置文件重新生效
不同点:
reload (重新加载),reload会重新加载配置文件,服务不会中断。而且reload时会测试conf语法等,如果出错会rollback用上一次正确配置文件保持正常运行。也叫平滑重启,不会对已经连接的服务造成影响。
restart (重启)(先stop后start),会重启服务。这个重启会造成服务一瞬间的中断,如果配置文件出错会导致服务启动失败,那就是更长时间的服务中断了。
注意:修改配置文件前一定要先备份!为了保证线上服务高可用,推荐使用reload
-A # 添加规则,默认添加到最后一条规则
-I # 插入规则,默认插入到第一条规则
-D # 删除规则,先执行 iptables -nvL --line-numbers 查看规则的序列号,然后执行 iptables -D INPUT <number> 删除规则
-n # 数字
-s # 用于指定源IP地址,用法如:iptables -A INPUT -s 192.168.1.1/32 -p tcp --dport 22 -j ACCEPT
-d # 用于指定目标IP地址,用法如:iptables -A INPUT -d 192.168.1.1/32 -p tcp --dport 22 -j ACCEPT
-p # 用于指定检查哪个协议,用法如:iptables -A INPUT -p tcp --dport 80 -j ACCEPT
-i # 用于指定入口网卡,用法如:iptables -A INPUT -i eth0 -j ACCEPT
-o # 用于指定出口网卡,用法如:iptables -A FORWARD -o eth0 -j ACCEPT
-j # 用于指定要进行的处理动作,ACCEPT表示接受数据包进来 DROP直接丢弃数据包 用法如:iptables -A INPUT -p tcp --dport 80 -j ACCEPT
-P # 用于设置默认规则,用法如:iptables -P INPUT DROP
-t # 用于指定操作哪张表,用法如:iptables -t nat -nvL , iptables -t filter ,如果没有指定默认是filter表
-Z # 用于清空计数器,用法如:iptables -Z
-L # 用于列出规则链中的所有规则
--sport # 用于指定源端口,用法如:iptables -A INPUT -p tcp --sport 1234 -j ACCEPT
--dport # 用于指定目标端口,用法如:iptables -A INPUT -s 192.168.1.1/32 -p tcp --dport 22 -j ACCEPT
! # 取反
Linux 防火墙:Netfilter iptables的更多相关文章
- Linux防火墙简介 – iptables配置策略
Linux防火墙简介 – iptables配置策略 Netfilter/iptables简介 要想真正掌握Linux防火墙体系,首先要搞清楚Netfilter和iptables的关系,Netfilte ...
- Linux防火墙(iptables/firewalld)
Linux防火墙(iptables/firewalld) 目录 Linux防火墙(iptables/firewalld) 一.iptables 1. iptables概述 2. netfilter和i ...
- linux防火墙之iptables
linux防火墙之iptables 1.1.1 关于iptables简介 IPTABLES 是与最新的 3.5 版本 Linux 内核集成的 IP 信息包过滤系统.如果 Linux 系统连接到因特网或 ...
- linux中的防火墙netfilter iptables
目录 一.Linux防火墙基础 1.1 ptables的表.链结构 1.2 数据包控制的匹配流程 二.编写防火墙规则 1.iptables的安装 2.1 基本语法.控制类型 一般在生产环境中设置网络型 ...
- Linux防火墙配置(iptables, firewalld)
netfilter和底层实现 iptables firealld Linux中的防火墙 RHEL中有几种防火墙共存: iptables firewalld ip6tables ebtables 这些软 ...
- Linux防火墙:iptables禁IP与解封IP常用命令
在Linux服务器被攻击的时候,有的时候会有几个主力IP.如果能拒绝掉这几个IP的攻击的话,会大大减轻服务器的压力,说不定服务器就能恢复正常了. 在Linux下封停IP,有封杀网段和封杀单个IP两种形 ...
- linux防火墙相关 iptables
1. root用户查看防火墙状态(非root用户无权限查看) 查看防火墙状态: service iptables status 2.开启和关闭防火墙 //开启防火墙: service iptables ...
- Linux防火墙之iptables入门
一.防火墙的概念 什么是防火墙?防火墙是一台或一组设备,用以在网络间实施访问控制策略:事实上一个防火墙能够包含OSI模型中的很多层,并且可能会涉及进行数据包过滤的设备,它可以实施数据包检查和过滤,在更 ...
- Linux防火墙之iptables常用扩展匹配条件(二)
上一篇博文我们讲到了iptables的一些常用的扩展匹配模块以及扩展模块的一些选项的说明,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/12273755.htm ...
- Linux防火墙设置——iptables
防火墙用于监控往来流量,并根据用户定义的规则来过滤数据包以保证安全.iptables是Linux下设置防火墙规则的常用工具,它可以让你设置.维护以及查看防火墙的规则表.你可以定义多个表,每个表可以包含 ...
随机推荐
- ionic3 表单输入元素input的三种事件
1.onblur事件 onblur事件即为失去焦点时触发的事件,而所谓的焦点就是一闪一闪的光标.而ionic中onblur则为ionBlur,具体用法如下: //html<ion-input [ ...
- windows保存的文件传输到linux中格式转换
直接从window传输到linux的脚本执行时,会出现以下错误. -bash: xxx: /bin/sh^M: bad interpreter: No such file or directory 解 ...
- PDFBOX详解
PDFBOX详解 摘要 自从Adobe公司1993年第一次发布公共PDF参考以来,支持各种语言和平台的PDF工具和类库就如雨后春笋般涌现.然而,Java应用开发中Adobe技术的支持相对滞后了. 自从 ...
- C++实现景区信息管理系统
景区信息管理系统 实现了: 1.1 建立主程序应用菜单选项 主程序应用菜单选项包含所实现的所有功能,并且对选项采用数字标识进行选择,对其他错误输入可以进行判别,提示输入错误. 1.2 导游线路图的创建 ...
- 【规范】前端编码规范——css 规范
编码 在 css 首行设置文件编码为 UTF-8. @charset "UTF-8"; class 命名 class 名称应当尽可能短,并且意义明确.使用有意义的名称,使用有组织的 ...
- Android中的AlarmManager的使用
AlarmManager是Android中的一种系统级别的提醒服务,它会为我们在特定的时刻广播一个指定的Intent.而使用Intent的时候,我们还需要它执行一个动作,如startActivity, ...
- IT观察】网络通信、图片显示、数据库操作……Android程序员如何利用开源框架
每个Android 程序员都不是Android应用开发之路上孤军奋战的一个人,GitHub上浩如烟海的开源框架或类库就是前人为我们发明的轮子,有的轮子能提高软件性能,而有的轮子似乎是以牺牲性能为代价换 ...
- 使用PHP几种写99乘法表的方式
首先按照规矩,还是先废话一番,对于刚学PHP的新手来讲,用php写九九乘法表无疑是非常经典的一道练习题. 但不要小看这道练习题,它对于逻辑的考验还是相当到位的. 也许有人会觉得,九九乘法表有什么难的, ...
- Ext Js 6.2.1 classic grid 滚动条bug解决方案
此bug未在其他版本发现,参考高版本代码重写类解决此bug,直接上代码: /** * 如果列表同时存在横向滚动条和竖向滚动条,当竖向滚动条滚动到底部时 * 点击横向滚动条,滚动条会自动滚动到顶部 * ...
- centos7上修改lv逻辑卷的大小
author:headsen chen date: 2019-03-18 15:24:22 1,查看 [root@localhost mnt]# df -h Filesystem Size Used ...