Centos开启防火墙和端口命令

Centos开启防火墙和端口命令

  • [1 课堂小知识](#1 课堂小知识)
    • [1.1 centos7简介](#1.1 centos7简介)
    • [1.2 iptables方式开启防火墙](#1.2 iptables方式开启防火墙)
  • [2 操作步骤](#2 操作步骤)
    • [2.1 开启查看关闭firewalld服务状态](#2.1 开启查看关闭firewalld服务状态)
    • [2.2 查看端口是否开放](#2.2 查看端口是否开放)
    • [2.3 新增开放端口](#2.3 新增开放端口)
    • [2.4 查看开放的端口](#2.4 查看开放的端口)
  • [3 防火墙的其他指令](#3 防火墙的其他指令)

1 课堂小知识

1.1 centos7简介

CentOS 7是CentOS项目发布的开源类服务器操作系统,于2014年7月7日正式发布。它是一个企业级的Linux发行版本,源于RedHat免费公开的源代码进行再发行。CentOS 7内核更新至3.10.0,支持Linux容器、支持Open VMware Tools及3D图像即装即用、支持OpenJDK-7作为缺省JDK、支持内核空间内的iSCSI及FCoE、支持PTPv2等功能。

1.2 iptables方式开启防火墙

iptables是Linux上常用的防火墙软件,以下是使用iptables命令开启防火墙端口的步骤:

首先,检查是否已经安装了iptables。如果未安装,可以使用以下命令进行安装:

powershell 复制代码
yum install -y iptables

2、确保防火墙服务正在运行。可以使用以下命令启动防火墙服务:

powershell 复制代码
systemctl start iptables.service

3、如果需要设置防火墙开机自启动,可以使用以下命令:

powershell 复制代码
systemctl enable iptables.service

4、打开需要开启的端口。假设需要打开TCP 80端口,可以使用以下命令:

powershell 复制代码
iptables -A INPUT -p tcp --dport 80 -j ACCEPT

5、最后,保存防火墙规则。可以使用以下命令将规则保存到文件中:

powershell 复制代码
service iptables save

这样,防火墙端口就开启了。请注意,这些步骤可能会因操作系统版本和配置而有所不同,建议根据实际情况进行调整。

2 操作步骤

  • Centos升级到7之后,内置的防火墙已经从iptables变成了firewalld。所以,端口的开启还是要从两种情况来说明的,即iptablesfirewalld。更多关于CentOs防火墙的最新内容,请参考Redhat官网

  • Centos7默认安装了firewalld,如果没有安装的话,可以使用 yum install firewalld firewalld-config进行安装。

  • 此方法用的事firewalld开启防火墙端口的方法。

2.1 开启查看关闭firewalld服务状态

shell 复制代码
#启动/关闭firewall
systemctl start/stop firewalld
#查看防火墙状态
systemctl status firewalld
#禁用或者启用
systemctl disable/enable firewalld
#查看 firewall目前开放的内容
firewall-cmd --list-all

1、启动/关闭防火墙 ( firewall )

powershell 复制代码
systemctl start/stop firewalld

2、查看防火墙状态

powershell 复制代码
systemctl status firewalld

3、禁用或者启用

powershell 复制代码
systemctl disable/enable firewalld

4、查看 firewall目前开放的内容

powershell 复制代码
firewall-cmd --list-all

2.2 查看端口是否开放

由下图可知,目前端口暂时未开放

2.3 新增开放端口

powershell 复制代码
firewall-cmd --add-port=8008/tcp --permanent
#或
firewall-cmd --permanent --zone=public --add-port=8008/tcp

#--zone #作用域 
#--add-port=8080/tcp #添加端口,格式为:端口/通讯协议 
#--permanent #永久生效,没有此参数重启后失效

firewall-cmd --reload   # 配置立即生效

1、开启防火墙端口

powershell 复制代码
firewall-cmd --add-port=8008/tcp --permanent

2、重启防火墙

powershell 复制代码
firewall-cmd --reload

2.4 查看开放的端口

powershell 复制代码
firewall-cmd --list-port

3 防火墙的其他指令

1.启动防火墙

powershell 复制代码
systemctl start firewalld 

2.禁用防火墙

powershell 复制代码
systemctl stop firewalld

3.设置开机启动

powershell 复制代码
systemctl enable firewalld

4.停止并禁用开机启动

powershell 复制代码
sytemctl disable firewalld

5.重启防火墙

powershell 复制代码
firewall-cmd --reload

6.查看状态

powershell 复制代码
systemctl status firewalld或者 firewall-cmd --state

7.查看版本

powershell 复制代码
firewall-cmd --version

8.查看帮助

powershell 复制代码
firewall-cmd --help

9.查看区域信息

powershell 复制代码
firewall-cmd --get-active-zones

10.查看指定接口所属区域信息

powershell 复制代码
firewall-cmd --get-zone-of-interface=eth0

11.拒绝所有包

powershell 复制代码
firewall-cmd --panic-on

12.取消拒绝状态

powershell 复制代码
firewall-cmd --panic-off

13.查看是否拒绝

powershell 复制代码
firewall-cmd --query-panic

14.将接口添加到区域(默认接口都在public)

powershell 复制代码
firewall-cmd --zone=public --add-interface=eth0(永久生效再加上 --permanent 然后reload防火墙)

15.设置默认接口区域

powershell 复制代码
firewall-cmd --set-default-zone=public(立即生效,无需重启)

16.更新防火墙规则

powershell 复制代码
firewall-cmd --reload或firewall-cmd --complete-reload(两者的区别就是第一个无需断开连接,就是firewalld特性之一动态添加规则,第二个需要断开连接,类似重启服务)

17.查看指定区域所有打开的端口

powershell 复制代码
firewall-cmd --zone=public --list-ports

18.在指定区域打开端口(记得重启防火墙)

powershell 复制代码
firewall-cmd --zone=public --add-port=80/tcp(永久生效再加上 --permanent)

说明:

--zone 作用域

--add-port=8080/tcp 添加端口,格式为:端口/通讯协议

--permanent #永久生效,没有此参数重启后失效

相关推荐
likfishdn7 分钟前
Linux的文件与目录管理
linux·运维·服务器
chian-ocean27 分钟前
Linux 文件缓冲区:高效数据访问的幕后推手
linux·运维·服务器
加油=^_^=28 分钟前
【Linux】进程优先级 | 进程调度(三)
linux·运维·服务器
吃汤圆的抹香鲸1 小时前
GoLand 安装包 绿色版 Win,Mac,Linux 包含IntelliJ全家桶 专为Go语言设计的集成开发环境(IDE)
linux·windows·macos·go·intellij-idea·go1.19
追寻光1 小时前
Cannot find a valid baseurl for repo: base/7/x86_64
linux
元气满满的热码式1 小时前
Docker数据卷操作实战
linux·运维·docker·云原生·容器
陆沙1 小时前
deepseek-r1-centos-本地服务器配置方法
linux·服务器·centos
北京耐用通信1 小时前
航空装配自动化神器Ethercat转profient网关搭配机器人精准控制
运维·机器人·自动化
吃汤圆的抹香鲸2 小时前
Rider 安装包 绿色版 Win/Mac/Linux 适合.NET和游戏开发者使用 2025全栈开发终极指南:从零配置到企业级实战
linux·运维·windows·sql·游戏·macos·.net
sz66cm2 小时前
Linux基础 -- ARM 32 位架构动态注入代码技术文档
linux·arm开发·架构