Centos7修改iptables规则并开机永久生效两种方式

防火墙规则保存及自定义链 类似于函数,将类型相同的规则放入同一个链中#自定义添加WEB链#创建一个名为WEB的链,使用离散方式定义tcp协议的目的端口80和443,允许通过#在WEB链中添加,允许TCP协议的目的端口8080允许通过的规则​​​​#在INPUR链中添加规则,将WEB链的规则加载到主机192.168.190.130上​​​#删除WEB链#当WEB链被赋予链接,及链内有规则时,不可以被删除​​​​​#先将与WEB链关联的INPUT规则删除#再将WEB链内的规则全部清空。 阅读详情
AI摘要

本文介绍在Centos7系统中,如何通过修改配置文件或使用命令行来调整iptables防火墙规则,并确保这些修改在系统重启后仍然有效。文章详细解释了两种修改iptables规则的方法,包括直接编辑配置文件和利用命令行进行更改,同时提供了如何使规则永久生效的具体步骤。

相信很多使用Centos7的朋友和我一样,都是删除默认的firewalld服务,而重新安装iptables做为防火墙。这一篇文章我们一起来看看如何修改iptables规则以及如何使得修改的iptables规则永久生效。

我是T型人小付,一位坚持终身学习的互联网从业者。喜欢我的博客欢迎在csdn上关注我,如果有问题欢迎在底下的评论区交流,谢谢。

前提条件

这篇文章的前提条件是centos7机器已经按照下面方法删除了firewalld服务,并安装了iptables。同时将iptables设置为了开机自启动

sudo systemctl stop firewalld.service
sudo systemctl disable firewalld.service
sudo yum -y install iptables-services
sudo systemctl start iptables.service
sudo systemctl enable iptables.service

修改iptables的两种方式

修改配置文件

配置文件的路径为/etc/sysconfig/iptables,直接编辑这个文件,例如在该文件最上面的规则插入一条允许80端口的规则

# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

之后如果想马上生效,需要重启iptables服务

systemctl restart iptables

再查看iptables规则的时候会发现刚才的规则已经生效了,如下最上面那条

[root@k8s-master sysconfig]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:80
   51  3152 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
    1   229 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 30 packets, 3520 bytes)
 pkts bytes target     prot opt in     out     source               destination

之后通过reboot命令重启机器,之后查看iptables规则发现修改的规则依然生效。

这说明直接修改iptables的配置文件会永久生效。

命令行

除了直接修改配置文件,也可以命令行快速修改规则。尤其是在批量用脚本修改iptables的场合命令行会更适用一些。

例如直接在命令行添加了一个允许443端口的规则

[root@k8s-master ~]# iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
[root@k8s-master ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:443
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:80
  535 38884 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
  192 32397 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 30 packets, 1940 bytes)
 pkts bytes target     prot opt in     out     source               destination

可以看到命令跑完就直接生效,但是命令对应的规则并没有被写到配置文件里面,所以开机以后就失效了。

那么该如何保存命令行修改的规则呢?

保存命令行修改的规则

第一种方法

执行命令service iptables save,如下

[root@k8s-master ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]

之后查看下配置文件,发现新的规则被写进了配置文件中,并且里面还写了是通过命令行写入的

# Generated by iptables-save v1.4.21 on Sun Apr 26 22:00:57 2020
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [4:440]
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
# Completed on Sun Apr 26 22:00:57 2020

之后再重启机器就依然是生效的了。

第二种方法

执行命令iptables-save > xxx写入到一个文件,开机以后执行命令iptables-restore < xxx用来恢复。

例如删除掉刚才配置文件里面的443端口那条命令,然后将完整规则保存到别的目录

[root@k8s-master sysconfig]# iptables-save > /tmp/iptables.bak
[root@k8s-master sysconfig]# cat /tmp/iptables.bak
# Generated by iptables-save v1.4.21 on Sun Apr 26 22:24:10 2020
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [351:44722]
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
# Completed on Sun Apr 26 22:24:10 2020

之后在/etc/rc.d/rc.local中添加如下命令

/usr/sbin/iptables-restore < /tmp/iptables.bak

之后给rc.local文件添加可执行权限使得可以被开机运行

chmod a+x rc.local

再开机就会发现虽然配置文件没有更改,但是规则还是生效了。但是这样子有一个弊端,因为真正保存完整规则的并不是配置文件,而是另外的一个文件,如果运维人员没有记录好容易造成问题。

总结

总结一下两种修改iptables规则的方法:

  • 直接修改配置文件
  • 命令行修改规则

而其中直接修改配置文件会自动永久生效,通过命令行修改规则可以用下面两种方法永久生效:

  • service iptables save
  • iptables-save和iptables-restore以及开机文件/etc/rc.d/rc.local

我个人比较建议的方式是,少量更改可以考虑修改配置文件,大量批量修改用命令行然后用service iptables save保存到配置文件中。

CentOS 7 从 firewalld 迁移到 iptables 的工程实践指南 iptables 是 Linux 内核 Netfilter 框架的命令行接口,通过直接操作四表五链(filter、nat、mangle、raw)实现细粒度网络包过滤与地址转换。其核心原理基于规则匹配顺序、连接状态跟踪(conntrack)及模块化扩展(如 xt_time、ipset),技术价值在于零抽象、高确定性与强可审计性,广泛应用于生产环境防火墙加固、Docker/KVM 网络协同、等保合规策略落地及 AI 工具链(如 Free-Claude-Code)的端口映射部署。本文聚焦 CentOS 7 平台, 阅读详情

相关推荐

CentOS 7 防火墙修改为iptables(防火墙相关命令和配置)

目录 CentOS7默认的防火墙不是iptables,而是firewalle. Centos 7 使用firewall : Centos 7 使用iptables: iptables常用命令 CentOS7默认的防火墙不是iptables,而是firewalle. Centos 7 firewall 命令: 查看已经开放的端口: firewall-cmd --list-ports...

中关村村委的博客 2247

iptables规则重启自动生效--永久生效

方法1: 执行命令:service iptables save 规则自动保存到了/etc/sysconfig/iptables,用此命令保存的规则开机会自动生效。 方法2: 保存规则:#iptables-save >/etc/iptables-script 恢复规则:#iptables-restore>/etc/iptables-script 开机自动恢复规则,把恢复命令添加到启动...

okhymok的博客 3万+

麒麟V10永久设置iptables防火墙规则

通过命令行设置iptables防火墙规则时,规则是即时生效的,并不是永久生效,重启之后,设置的iptables防火墙规则会自动清空,而在实际使用场景中,会希望设置的iptables防火墙规则一直生效。本文将介绍永久设置iptables防火墙规则的详细配置步骤。使用命令行模式添加iptables规则时,规则是即时生效的,并不是永久生效, 重启之后,设置的规则会自动清空。但是实际使用场景中, 会希望设定的防火墙规则一直生效,这就涉及到规则的保存。使用命令行模式添加iptables规则时,重启终端,规则即重置。

太极淘的博客 2330

CentOS配置iptables规则并使其永久生效

2、设置ICMP包过滤,允许每秒1个包,限制触发条件是10个包。1、处理IP碎片数量,防止攻击,允许每秒100个。3、如果想要禁止某个ip访问本机某个特殊端口。三、添加对应禁止规则后保存。1、禁止此IP访问服务器。2、禁止服务器访问此IP。二、清楚一增加的禁止规则。1、一键清空所有规则。5、添加之后执行生效。

qq18346342939的博客 2059

ROS2实战:手把手教你用walking机器人仿真实现医院多楼层自动配送(含实时地图切换)

本文详细介绍了如何利用ROS2和walking机器人仿真平台实现医院多楼层自动配送系统,重点讲解了动态地图切换技术和多楼层导航的实现方法。通过实时加载地图和优化电梯过渡策略,解决了医院复杂环境下的自主配送难题,为医疗物流自动化提供了实用解决方案。

weixin_29090917的博客 357

ECN实战指南:如何用SAP系统搞定工程变更(附库存处理避坑技巧)

本文详细解析了如何在SAP系统中高效执行工程变更通知(ECN),特别聚焦PP模块的操作流程与库存处理技巧。通过实战案例和系统操作指南,帮助制造业企业避免库存混乱和生产中断,实现从创建到生效的全流程管理。

m5n6o7的博客 1079

linux iptables规则修改完成后怎么立即生效

在修改 iptables 规则后,可以使用以下命令立即生效: 使用 service iptables save 命令来保存规则,然后使用 service iptables restart 重启 iptables 服务。 使用 /etc/init.d/iptables restart 命令重启 iptables 服务。 注意:使用这些命令时需要 root 权限。 另外,如果想永久保存 iptab...

weixin_35756892的博客 7324

从ADS到SystemVue:当简单链路预算不够用时,我的射频系统级仿真方案升级实录

本文详细介绍了从ADS到SystemVue的射频系统级仿真升级路径,重点分析了基础链路预算在多端口元件和复杂场景下的局限性,并提供了SystemVue在非线性系统验证和跨域协同分析中的优势。通过实际案例展示了如何平滑迁移工具链,提升仿真精度和效率,适用于5G、毫米波雷达等现代射频系统设计。

weixin_34233974的博客 340

【iptables】修改规则和保存规则

如果上图中的命令没有使用-s指定对应规则中原本的源地址,那么在修改完成后,你修改的规则中的源地址对应的原本的匹配条件会自动变成0.0.0.0/0(此IP表示所以网段的IP地址),而此时,-j对应的动作又为REJECT,所以在执行上述命令时,如果没有指明规则原本的源地址,那么所有IP的请求都被拒绝了(因为没有指定原本的源地址,当前规则的源地址自动变成0.0.0.0/0),如果你正在使用ssh远程到服务器上进行iptables设置,那么你的ssh请求也将会被阻断。iptables -t 表名。

m0_56573171的博客 3505

华为OD机考双机位C卷 - 打印机队列 (Java & Python& JS & C/C++ & GO )

华为OD机考双机位C卷 - 打印机队列

算法大师! 4619

YOLOv11改进 | 主干/Backbone篇 | 利用目标检测轻量化网络MobileNetV2替换Backbone(yolov11系列全系轻量化)

本文给大家带来的改进机制是,其是专为移动和嵌入式视觉应用设计的轻量化网络结构。其在MobilNetV1的基础上采用反转残差结构和线性瓶颈层。这种结构通过轻量级的深度卷积和线性卷积过滤特征,同时去除狭窄层中的非线性,以维持表征能力。MobileNetV2在性能上和精度上都要比V1版本强很多,其在多种应用(如对象检测、细粒度分类、面部属性识别和大规模地理定位)中都展现了一定的有效性。YOLOv11改进系列专栏——本专栏持续复习各种顶会内容——科研必备目录一、本文介绍二、MobileNetV2的框架原理。

Snu77的博客 2841

在CentOS 7上如何从FirewallD迁移至Iptables

与大多数其他 Linux 发行版一样,CentOS 7 使用 Linux 内核中的netfilter框架来访问通过网络堆栈流动的数据包。这提供了必要的接口来检查和操作数据包,以实现防火墙系统。大多数发行版使用iptables防火墙,它使用netfilter钩子来执行防火墙规则。CentOS 7 配备了一个名为firewalld的替代服务,它实现了相同的功能。虽然firewalld是一个非常强大的防火墙解决方案,具有出色的功能,但对于一些用户来说,如果他们熟悉iptables。

rubys007的博客 1183

PyTorch实战(19)——变分自编码器(Variational Autoencoder,VAE)

变分自编码器 (Variational Autoencoder, VAE) 是一种结合了自编码器和概率建模的生成模型,通过编码器将输入数据映射到潜在空间中的概率分布,并通过解码器将从潜在空间采样得到的潜在变量映射回原始数据空间,实现了数据的生成和特征学习。本节中,将介绍变分自编码器的基本概念,并使用 PyTorch 实现变分自编码器生成新图像。

盼小辉丶的博客 2855

centos7中iptables配置log日志记录所有流量

CentOS 7.0默认使用的是firewall作为防火墙,使用iptables必须重新设置一下 1、直接关闭防火墙 systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewall开机启动 2、设置 iptables service yum -y install iptable...

qq_39405012的博客 4857

学Simulink--人形机器人控制场景实例:基于Simulink的多连杆人形机器人拉格朗日动力学建模仿真

matlab编辑% 连杆长度 (m)% 躯干(髋到CoM)% 大腿% 小腿% 质量 (kg)% 转动惯量(绕质心,简化为杆)matlab编辑% 躯干CoM(假设在髋上方L_body/2处)% 左大腿CoM% 左小腿CoM% (右腿类似,略)本文完成了基于Simulink的多连杆人形机器人拉格朗日动力学建模仿真,实现了:✅ 定义7-DOF平面人形结构✅ 使用Symbolic Math Toolbox自动推导动力学方程✅ 生成。

amy_mhd的博客 397

从零搭建 C# + YOLO 工业视觉系统:从模型训练到上位机部署(工业级落地)

做工业质检、自动化生产线的同学都懂:一套能用的工业视觉系统,既要“检测准”,又要“界面易操作”,还要“部署稳”。之前帮某电子厂做PCB缺陷检测,一开始用Python+YOLO做检测核心,QT做上位机,结果踩了无数坑:Python推理速度波动大,QT和Python跨语言集成繁琐,数据传递容易卡顿;后来换成C# + YOLO的组合,直接打通了“模型训练-上位机开发-工业部署”全流程——C#的WinForms/WPF开发上位机高效,工业相机SDK支持完善;

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 737

linux centos7 iptables配置

centos7默认防火墙不是iptables,是firewalle,这不是本文的重点。 概述 Linux的防火墙体系主要工作在网络层,针对TCP/IP数据包实施过滤和限制。属于典型的包过滤防火墙。linux系统的防火墙体系基于内核编码实现,具有非常稳定的性能和极高的效率,因此获得广泛的应用。 在很多安全技术资料中,netfilter和iptables都用来指linux防火墙,区别如下: netfilter:指的是linux内核中实现包过滤防火墙的内部结构,不以程序或文件的形式存在,属于“内核态”又称内.

梦想是很难很难的,所以先勇敢一点 1万+

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

如何在 Centos7 使用 iptables

如何在 Centos7 使用 iptables 从 Red Hat Enterprise Linux (RHEL) 7 和 CentOS 7 开始,firewalld 便作为系统默认的防火墙软件,替代之前的 iptables。firewalld 使用 firewall-cmd 命令管理防火墙规则,但是对于习惯了 iptables 的用户来说更倾向于使用传统的 iptables 方式,因为没有学习...

qhh0205 1081

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

centos7设置iptables

关闭firewall防火墙 CentOS 7.0默认使用firewall作为防火墙,关闭firewall: // 停止firewall # systemctl stop firewalld.service // 禁止firewall开机启动 # systemctl disable firewalld.service // 查看默认防火墙状态(关闭后显示not running,开启后显示running) # firewall-cmd --state 安装设置iptables防火墙 //.

yjk13703623757的博客 1047

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

Centos7防火墙iptables安装及设置图文并茂【实现防火墙管理功能】

一、安装iptables Centos7系统默认是使用firewall作为防火墙,为了更好兼容老的系统配置,将防火墙设置为iptables(用原来centos6.5的配置即可直接用)。 查看firewall防火墙状态:systemctl status firewalld 关闭防火墙 关闭firewall防火墙: systemctl stop firewalld 3. 关闭防火墙开机自启动 关闭开机自启动: systemctl disable firewalld 兄弟们我这里重启系统(reb

土豆是狗的博客 8499

centos7下iptables的使用

IPTABLES 策略与规则链 iptables中基本的命令参数 遇到的问题 在早期的Linux系统中,默认使用的是iptables防火墙管理服务来配置防火墙。尽管新型的firewalld防火墙管理服务已经被投入使用多年,但是大量的企业在生产环境中依然出于各种原因而继续使用iptables。 1.策略与规则链 防火墙会从上至下的顺序来读取配置的策略规则,在找到匹配项后就立即结束匹配工作并去...

weixin_42403893的博客 597

Centos iptables 配置

linux防火墙使用以及配置 Centos 7 firewall : 1、firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status firewalld 开机禁用 : systemctl disable firewalld 开机启用 :systemctl enable firewalld 2.systemctl是CentOS7的服务管理工具...

xiaomahuan的专栏 6798

Centos7 禁止ping的几种方式

Centos7 禁止ping的几种方式 有梦想的程序丶猿关注 2019.02.27 17:53字数 143阅读 903评论 0喜欢 0 临时禁ping 将/proc/sys/net/ipv4/icmp_echo_ignore_all文件里面的0临时改为1,从而实现禁止...

2907

云主机上Centos7配置Iptables规则开启80、3306等端口

用MobaXtrem远程连接h3c云平台主机,再在云平台上用MobaXtrem连接h3c内网的Centos7主机,现在Centos7装了mysql、tomcat等想开启3306、80端口,下面讨论配置iptables并开启相应端口步骤:Centos7默认使用firewall作为防火墙,所以要先改为iptables防火墙:1&gt;systemctl stop firewalld  #先关闭fire...

虚拟-Worder的博客 7194

Linux——CentOS 7 安装iptables防火墙

CentOS7默认的防火墙不是iptables,而是firewalle. 安装iptableiptable-service #先检查是否安装了iptables service iptables status #安装iptables yum install -y iptables #升级iptables yum update iptables #安装iptables-services yu...

Hern(宋兆恒) 1084

Centos7 防火墙iptables?

Centos7 防火墙iptables

每天进步一点点!!! 1010
上一篇: Centos7命令行修改网卡配置并生效操作详解
下一篇: echo输出带颜色的内容
T型人小付
博客等级 码龄7年 433粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值