热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

CentOS7的firewall和安装iptables

前言:CentOS7的防火墙默认使用是firewall,而我们通常使用iptables;本文记录了firewall基础的命令和iptables的安装和使用。firewall部分:part1:服

前言:CentOS7 的防火墙默认使用是firewall,而我们通常使用iptables;

本文记录了firewall基础的命令和iptables的安装和使用。

 

firewall部分:

part1 : 服务命令

systemctl start firewalld#启动

systemctl status firewalld #查看运行状态

systemctl stop firewalld.service #停止firewall

systemctl disable firewalld.service #禁用:禁止firewall开机启动

firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running

firewall-cmd--reload 重启

part2 : 端口命令:

添加

firewall-cmd --zOne=public --add-port=80/tcp --permanent   --permanent永久生效,没有此参数重启后失效)

重新载入

firewall-cmd --reload

查看

firewall-cmd --zOne= public --query-port=80/tcp

删除

firewall-cmd --zOne= public --remove-port=80/tcp --permanent


part3:示例

示例:firewall端口操作完之后需要重启服务生效

[root@iZ2zeczh9tfpmxmijw5qppZ ~]# firewall-cmd --zOne=public --query-port=3307/tcp
no
[root@iZ2zeczh9tfpmxmijw5qppZ ~]# firewall-cmd --zOne=public --add-port=3307/tcp --permanent
success
[root@iZ2zeczh9tfpmxmijw5qppZ ~]# firewall-cmd --zOne=public --query-port=3307/tcp
no
[root@iZ2zeczh9tfpmxmijw5qppZ ~]# firewall-cmd --reload
success
[root@iZ2zeczh9tfpmxmijw5qppZ ~]# firewall-cmd --zOne=public --query-port=3307/tcp
yes


示例:mysql开放远程端口

[root@iZ2zeczh9tfpmxmijw5qppZ ~]# firewall-cmd --zOne=public --add-port=3306/tcp --permanent
success
[root@iZ2zeczh9tfpmxmijw5qppZ ~]# firewall-cmd --reload
success
[root@iZ2zeczh9tfpmxmijw5qppZ ~]# firewall-cmd --state
running
[root@iZ2zeczh9tfpmxmijw5qppZ ~]#
[root@iZ2zeczh9tfpmxmijw5qppZ ~]# firewall-cmd --zOne=public --query-port=3306/tcp
yes
[root@iZ2zeczh9tfpmxmijw5qppZ ~]#



iptables部分


part1 : 服务命令

systemctl start iptables #启动

systemctl status iptables #查看运行状态

systemctl restart iptables.service #停止iptables

systemctl stop iptables.service #停止iptables

systemctl disable iptables.service #禁用:禁止iptables开机启动

systemctl enable iptables.service #启用:设置iptables开机启动


part2 : 安装

step1 : 查看是否安装

[root@iZ2zeczh9tfpmxmijw5qppZ ~]# systemctl status iptables
Unit iptables.service could not be found.
[root@iZ2zeczh9tfpmxmijw5qppZ ~]#
没有相关服务

step2 : yum install

[root@iZ2zeczh9tfpmxmijw5qppZ ~]# yum install iptables-services
Loaded plugins: fastestmirror
base | 3.6 kB 00:00:00
epel | 4.3 kB 00:00:00
extras | 3.4 kB 00:00:00
mysql-connectors-community | 2.5 kB 00:00:00
mysql-tools-community | 2.5 kB 00:00:00
mysql56-community | 2.5 kB 00:00:00
updates | 3.4 kB 00:00:00
updates/7/x86_64/primary_db | 6.4 MB 00:00:06
Loading mirror speeds from cached hostfile
* base: mirrors.aliyuncs.com
* epel: mirrors.aliyuncs.com
* extras: mirrors.aliyuncs.com
* updates: mirrors.aliyuncs.com
Resolving Dependencies
--> Running transaction check
---> Package iptables-services.x86_64 0:1.4.21-17.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================================
Package Arch Version Repository Size
========================================================================================================================================
Installing:
iptables-services x86_64 1.4.21-17.el7 base 50 k

Transaction Summary
========================================================================================================================================
Install 1 Package

Total download size: 50 k
Installed size: 24 k
Is this ok [y/d/N]: Y
Downloading packages:
iptables-services-1.4.21-17.el7.x86_64.rpm | 50 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : iptables-services-1.4.21-17.el7.x86_64 1/1
warning: /etc/sysconfig/iptables created as /etc/sysconfig/iptables.rpmnew
Verifying : iptables-services-1.4.21-17.el7.x86_64 1/1

Installed:
iptables-services.x86_64 0:1.4.21-17.el7

Complete!
[root@iZ2zeczh9tfpmxmijw5qppZ ~]#
安装成功!

step3 : check

[root@iZ2zeczh9tfpmxmijw5qppZ ~]# systemctl status iptables
iptables.service - IPv4 firewall with iptables
Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)
Active: inactive (dead)
[root@iZ2zeczh9tfpmxmijw5qppZ ~]#

step4 : start

[root@iZ2zeczh9tfpmxmijw5qppZ ~]# systemctl start iptables
[root@iZ2zeczh9tfpmxmijw5qppZ ~]# systemctl enable iptables.service
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@iZ2zeczh9tfpmxmijw5qppZ ~]#
[root@iZ2zeczh9tfpmxmijw5qppZ ~]# systemctl status iptables   iptables.service - IPv4 firewall with iptables   Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled)   Active: active (exited) since Wed 2017-06-21 15:44:41 CST; 1min 17s ago Main PID: 506 (code=exited, status=0/SUCCESS)Jun 21 15:44:41 iZ2zeczh9tfpmxmijw5qppZ systemd[1]: Starting IPv4 firewall with iptables...Jun 21 15:44:41 iZ2zeczh9tfpmxmijw5qppZ iptables.init[506]: iptables: Applying firewall rules: [  OK  ]Jun 21 15:44:41 iZ2zeczh9tfpmxmijw5qppZ systemd[1]: Started IPv4 firewall with iptables.[root@iZ2zeczh9tfpmxmijw5qppZ ~]# 

step5 : 修改配置文件

vi /etc/sysconfig/iptables 

systemctl restart iptables.service #重启防火墙使配置生效


step6 : 关闭SELINUX

vi/etc/selinux/config

#SELINUX=enforcing #注释掉

#SELINUXTYPE=targeted #注释掉

SELINUX=disabled #增加

:wq! #保存退出 

setenforce 0 #使配置立即生效


备注:

SELINUX不关闭时,iptables不读取配置文件,一般采取关闭SELINUX的方式避免这种冲突






推荐阅读
  • 本文介绍了如何在Azure应用服务实例上获取.NetCore 3.0+的支持。作者分享了自己在将代码升级为使用.NET Core 3.0时遇到的问题,并提供了解决方法。文章还介绍了在部署过程中使用Kudu构建的方法,并指出了可能出现的错误。此外,还介绍了开发者应用服务计划和免费产品应用服务计划在不同地区的运行情况。最后,文章指出了当前的.NET SDK不支持目标为.NET Core 3.0的问题,并提供了解决方案。 ... [详细]
  • 基于PgpoolII的PostgreSQL集群安装与配置教程
    本文介绍了基于PgpoolII的PostgreSQL集群的安装与配置教程。Pgpool-II是一个位于PostgreSQL服务器和PostgreSQL数据库客户端之间的中间件,提供了连接池、复制、负载均衡、缓存、看门狗、限制链接等功能,可以用于搭建高可用的PostgreSQL集群。文章详细介绍了通过yum安装Pgpool-II的步骤,并提供了相关的官方参考地址。 ... [详细]
  • docker安装到基本使用
    记录docker概念,安装及入门日常使用Docker安装查看官方文档,在"Debian上安装Docker",其他平台在"这里查 ... [详细]
  • Docker从安装到入门到项目部署
    容器化的时代你怎么可 ... [详细]
  • centos7下安装docker(15.3跨主机网络-macvlan)
    除了ovrlay,docker还开发了另一个支持跨主机容器的driver:macvlanmacvlan本身是linukernel模块,其功能是允许在同一物理网卡上配置多了MAC地址,即:多 ... [详细]
  • 结对编程 地铁最短路径 张波朱新远
    结对编程地铁最短路径一、任务:实现一个帮助进行地铁出行路线规划的命令行程序。PSP2.1PersonalSoftwareProcessStagesTimePlanni ... [详细]
  • DDD在微服务架构中落地应用
    1DDDDomainDrivenDesign(领域驱动设计,DDD),不是一种架构,而是一种架构方法论,是一种拆解业务、划分业务、确定业 ... [详细]
  • 1、概念共享内存:共享内存是进程间通信中最简单的方式之一。共享内存允许两个或更多进程访问同一块内存,就如同malloc()函数向不同进程返回了指向同一个 ... [详细]
  • 本文介绍了在CentOS 7.x上进行端口映射配置的方法,通过修改内核和配置防火墙实现端口映射。作者分享了自己使用华为服务器进行端口映射的经验,发现网速比直连还快且稳定。详细的配置过程包括开启系统路由模式功能、设置IP地址伪装、设置端口映射等。同时,还介绍了如何监听本地端口的tcp请求,以及删除规则和开放的端口的方法。 ... [详细]
  •  参考自:https:linux.cnarticle-6719-1.html一、安装  首先通过xshell5先登陆来到字符界面(xshell通过SSH连接请参见之前随笔)  先下载redis, ... [详细]
  • 修改第二步中按TAB键出来的命令这里注意了:网上很多文章都说这一步改成“>vmlinuzinitrdinitrd.imginst.stage2hd:devsdbquiet”什么的, ... [详细]
  • >>>CentOS7.0默认使用的是firewall作为防火墙,使用iptables必须重新设置一下1、直接关闭防火墙systemctlstopfirewalld.serv ... [详细]
  • asp.net core 应用docke部署到centos7
    前言前期准备win10(不要安装hyper-V)VMware-Workstation-Pro15.0Xshell6(非必需)VS2019以上环境请自行安装都是默认安装没什么可说的不 ... [详细]
  • 首先安装VM,VM破解版和激活版的百度有很多,随意下载一个下载CentOS7地址1:https:mirrors.btte.netcentos7isosx86_64地址2:http: ... [详细]
  • U盘安装centos7.6时报错,No Caching mode page found
    今天U盘安装centos7.6,选择UEFI启动时报错解决办法:后命令行就卡在这了,现在只需要耐心等待,等一会之后会不断的滚动错误警告,这个时候继续等待,那么一会就会出来命令行输入界 ... [详细]
author-avatar
蔚蓝的希望_674
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有