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 #永久生效,没有此参数重启后失效

相关推荐
FreeBuf_几秒前
SesameOp 恶意软件滥用 OpenAI Assistants API 实现与 C2 服务器的隐蔽通信
运维·服务器·网络
Elias不吃糖16 分钟前
第四天学习总结:C++ 文件系统 × Linux 自动化 × Makefile 工程化
linux·c++·学习
噜啦噜啦嘞好16 分钟前
Linux进程信号
linux·运维·服务器
REDcker1 小时前
Linux 进程资源占用分析指南
linux·运维·chrome
samroom1 小时前
Linux系统管理与常用命令详解
linux·运维·服务器
PKNLP1 小时前
07.docker介绍与常用命令
运维·docker·容器
Mxsoft6192 小时前
电力系统AR远程运维与数字孪生交互技术
运维·ar
一叶之秋14122 小时前
Linux基本指令
linux·运维·服务器
码割机2 小时前
Linux服务器安装jdk和maven详解
java·linux·maven
亚林瓜子2 小时前
在amazon linux 2023上面源码手动安装tesseract5.5.1
linux·运维·服务器·ocr·aws·ec2