召隆企博汇论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4636|回复: 0

Centos 7 firewall防火墙打开端口

[复制链接]

24

主题

5

回帖

199

积分

公司现有员工

积分
199
发表于 2020-1-9 08:41:07 | 显示全部楼层 |阅读模式
本帖最后由 myskya 于 2020-3-8 22:36 编辑 $ m4 H+ C1 _  i
3 n# W) \! f& c: Y1 j
一、启动服务
9 m9 r8 l1 [% Q6 d2 j- g) S如果没有安装,可以使用yum install firewall
- c! D1 z- E9 W7 T8 k9 _系统默认是已经安装了,但是没有启动
  r0 ?; |5 e+ o" T2 n" `9 O. a1、开机自启动& j8 f% b: \7 b0 U. |  O6 W: L3 k
systemctl enable firewalld1 p# y, N) r+ |( V/ V/ m
2、启动服务$ b; I( a* ^  }. B3 ~8 a
systemctl start firewalld
2 S/ M: }7 L7 _0 Q二、向防火墙添加可以外部访问的端口
$ I- g. P" Q2 x2 E2 ofirewall-cmd --zone=public --add-port=80/tcp --permanent 7 D" ?# ~6 r- U- w, u
以下是常用的端口/ k1 o" u3 N) |
firewall-cmd --zone=public --add-port=443/tcp --permanent7 y5 @9 r: ^# H; C+ f. w
firewall-cmd --zone=public --add-port=22/tcp --permanent: l9 n. [( q! Q& l4 k2 t
1、如果需要添加一段端口的话,使用以下命令
& Z# a+ Z2 o' E- c# b" l7 Qfirewall-cmd --zone=public --add-port=8000-8100/tcp --permanent0 e  O; S: a' c* a8 y2 b/ o% F
这儿是指添加101个端口3 G+ w' E7 n% o! n9 `
三、向防火墙添加相应的服务firewall-cmd --zone=public --add-service=dns --permanent
/ }+ D: V% O2 f' G; |具体的服务名称在以下路径里有一个文件就是一个service 名称
5 g2 x% {- ~( \  T8 R
/usr/lib/firewalld/services' `' F! d* O6 n5 k2 ]% v
' j# k# E) l8 N& L
四、添加完端口之后需要让防火墙生效4 m9 x4 `( q2 Q& `- G
1、重新加载
: b3 k% |5 B+ U. I. k0 ]firewall-cmd --reload6 G1 U) o; {6 t" j: Z2 C
2、查看当前已经开放的端口
; x+ c, m& A2 S9 Vfirewall-cmd --list-ports
) m3 m- \6 G/ K0 k( I5 v1 S9 Dfirewall-cmd --list-all1 B6 o; x5 N% C1 i5 r
3、删除某个不用的端口5 j$ n) m; E% D0 E" V
firewall-cmd --zone=public --remove-port=8084/tcp --permanent
! ?; G6 [9 l' b& ]; I  @注意,这儿删除只是删除一条规则,我上面加的8000-8100,其实还是可以访问8084这个端口的
8 \1 Q  l5 W8 B2 M# Z4 u, u. M
4 @& v0 J. y" e6 ?
6 y2 u8 M* i5 ~1 x9 G* P
) X1 J! Y6 G/ t6 X  n: T' x. I+ v
其他常用命令
) f' e! {8 K7 M) s7 |
/ ^, f" U$ L. h  L/ V1 [$ L
- y+ o. k$ v8 h/ I
  1. firewall-cmd --list-all-zones    #查看所有的zone信息
    " y( ~2 `& o5 J1 \
  2. firewall-cmd --get-default-zone     #查看默认zone是哪一个
    # L0 Q  _$ ]8 R" x
  3. firewall-cmd --zone=internal --change-zone=p3p1  #临时修改接口p3p1所属的zone为internal
    % D) Z+ }3 W' L2 X- ?' |9 j
  4. firewall-cmd --add-service=http    #暂时开放http
    ; d2 d  Z+ j& A( y- J) B+ m
  5. firewall-cmd --permanent --add-service=http  #永久开放http) b1 t: [9 G2 S* l  Z7 N1 z" y
  6. firewall-cmd --zone=public --add-port=80/tcp --permanent  #在public中永久开放80端口2 C  F0 b/ K# [$ b4 V
  7. firewall-cmd --permanent --zone=public --remove-service=ssh   #从public zone中移除服务
    ; f" [4 `" E# x/ S6 A. }+ `
  8. firewall-cmd --reload   #重新加载配置6 s# u7 z0 A; m; O$ i) r
  9. systemctl restart firewalld    #重启firewalld服务,使配置生效/可查
复制代码

6 D& m2 {: j0 a0 C
0 n: M8 }% R- I9 R4 _$ z) C+ h; E2 ^2 t1 C
" H! ?+ G9 m+ j4 b, L

  B( V6 \/ b  A- \  Y) L

7 [6 v2 j# c, y% J' h
+ K) I" B. }) M
! _! G0 T7 ^# H# N; s

6 U( x% Z# M7 m$ W* {( ?1 K: Z% c5 ?) p

( j. I% [5 P; [2 c) A/ [: Z; k/ }* P( ^2 [/ ?; a& x; F7 B
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|召隆企博汇 ( 粤ICP备14061395号 )

GMT+8, 2026-12-7 00:31 , Processed in 0.039072 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表