CentOS 6/7 防火墙设置 | 开启/关闭防火墙和端口

栏目: 服务器 · 发布时间: 7年前

内容简介:CentOS 默认防火墙状态是开启的,所有端口状态都是关闭的,无法直接使用。因此很多时候为了服务能正常使用,我们需要手动关闭防火墙,或者打开指定防火墙端口以供使用。CentOS 7 防火墙为 firewalld,旧版本的 CentOS 6 防火墙则是用的 iptables,因此操作指令也有所不同。关闭防火墙

前言

CentOS 默认防火墙状态是开启的,所有端口状态都是关闭的,无法直接使用。因此很多时候为了服务能正常使用,我们需要手动关闭防火墙,或者打开指定防火墙端口以供使用。

CentOS 7 防火墙为 firewalld,旧版本的 CentOS 6 防火墙则是用的 iptables,因此操作指令也有所不同。

CentOS 开启/关闭防火墙

CentOS 7

关闭防火墙

# 关闭
systemctl stop firewalld
 
# 禁止开机自启
sytemctl disable firewalld

开启防火墙

# 开启
systemctl start firewalld
 
# 设置开机自启
systemctl enable firewalld

查看防火墙状态

# 查看状态
firewall-cmd --state
 
# 重启
firewall-cmd --reload

CentOS 6

关闭防火墙

# 关闭
service iptables stop
 
# 禁止开机自启
chkconfig iptables off

开启防火墙

# 开启
service iptables start
 
# 设置开机自启
chkconfig iptables on

查看防火墙状态

# 查看状态
service iptables status
 
# 重启
service iptables restart

CentOS 开放/关闭防火墙端口

CentOS 7

开放/关闭指定端口

# 开启 80 端口,协议 tcp,--permanent 永久生效
firewall-cmd --zone=public --add-port=80/tcp --permanent
 
# 关闭端口
firewall-cmd --zone=public --remove-port=80/tcp --permanent
 
# 查看端口状态
firewall-cmd --query-port=80/tcp

CentOS 6

开放/关闭指定端口

# 开启 80 端口,协议 tcp
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
 
# 关闭端口
iptables -I INPUT -p tcp --dport 80 -j DROP
 
# 保存
service iptables save

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Math Adventures with Python

Math Adventures with Python

Peter Farrell / No Starch Press / 2018-11-13 / GBP 24.99

Learn math by getting creative with code! Use the Python programming language to transform learning high school-level math topics like algebra, geometry, trigonometry, and calculus! In Math Adventu......一起来看看 《Math Adventures with Python》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具