热门标签 | 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的方式避免这种冲突






推荐阅读
  • 基于PgpoolII的PostgreSQL集群安装与配置教程
    本文介绍了基于PgpoolII的PostgreSQL集群的安装与配置教程。Pgpool-II是一个位于PostgreSQL服务器和PostgreSQL数据库客户端之间的中间件,提供了连接池、复制、负载均衡、缓存、看门狗、限制链接等功能,可以用于搭建高可用的PostgreSQL集群。文章详细介绍了通过yum安装Pgpool-II的步骤,并提供了相关的官方参考地址。 ... [详细]
  • Centos7.6安装Gitlab教程及注意事项
    本文介绍了在Centos7.6系统下安装Gitlab的详细教程,并提供了一些注意事项。教程包括查看系统版本、安装必要的软件包、配置防火墙等步骤。同时,还强调了使用阿里云服务器时的特殊配置需求,以及建议至少4GB的可用RAM来运行GitLab。 ... [详细]
  • asp.net core 应用docke部署到centos7
    前言前期准备win10(不要安装hyper-V)VMware-Workstation-Pro15.0Xshell6(非必需)VS2019以上环境请自行安装都是默认安装没什么可说的不 ... [详细]
  • 怎么在Centos7环境中安装Docker
    这期内容当中小编将会给大家带来有关怎么在Centos7环境中安装Docker,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有 ... [详细]
  • 这里我们采用两台Nginx服务器作为前端,一主一从,Keepalived实现状态监测,保证Nginx正常对外提供服务,即主Nginx服务进程死掉之后,keepalived能够通过其自身的检测机制 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 本文介绍了在CentOS上安装Python2.7.2的详细步骤,包括下载、解压、编译和安装等操作。同时提供了一些注意事项,以及测试安装是否成功的方法。 ... [详细]
  • CEPH LIO iSCSI Gateway及其使用参考文档
    本文介绍了CEPH LIO iSCSI Gateway以及使用该网关的参考文档,包括Ceph Block Device、CEPH ISCSI GATEWAY、USING AN ISCSI GATEWAY等。同时提供了多个参考链接,详细介绍了CEPH LIO iSCSI Gateway的配置和使用方法。 ... [详细]
  • 本文介绍了在CentOS 6.4系统中更新源地址的方法,包括备份现有源文件、下载163源、修改文件名、更新列表和系统,并提供了相应的命令。 ... [详细]
  • 1、安装yum-yinstallmemcached2、运行memcached-u用户名-d ... [详细]
  • centos7+cdh5.10.0搭建
    一、选择环境:1.说明本次部署使用台机器,3台用于搭建CDH集群,1台为内部源。内部源机器是可以连接公网的,可以提前部署好内部源,本次部署涉及到的服务器的hosts配置如下:192.168 ... [详细]
  • 我在从Cron(由php执行)运行的php脚本中遇到问题。脚本运行正常,但使用512 ... [详细]
  • 1.在CentOSRHEL7中,有个叫hostnamectl的命令行工具,它允许你查看或修改与主机名相关的配置。$hostnamectlstatus[root@simon- ... [详细]
  • 简介Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。它的目的是打造一个高效、安全的Web平台。发展Ten ... [详细]
  • Centos7搭建Nexus3Nexus官方网站:https:www.sonatype.comdownload-oss-sonatype下载unix的tar.gz版本即可当前 ... [详细]
author-avatar
201153蜡笔小新
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有