Linux服务器端口开放

防火墙安装和启动

centos 7及以上默认安装了防火墙

如果没有安装,输入一下命令进行安装

yum -y install firewalld

查看防火墙状态

systemctl status firewalld

● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)  
   Active: active (running) since 日 2020-12-13 03:58:50 CET; 58s ago
     Docs: man:firewalld(1)
 Main PID: 30841 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─30841 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
  • enabled 开启状态
  • disabled 关闭状态

或者

firewall-cmd --state
  • running 正在运行
  • not running 已经停止

开启防火墙

systemctl start firewalld.service

关闭防火墙

systemctl stop firewalld.service

设置开机启动防火墙

systemctl stop firewalld.service

设置开机关闭防火墙

systemctl disable firewalld.service

基本参数

  • zone 作用域
  • add 添加策略
  • remove 移除策略
  • --permanent 代表永久生效,没有此参数重启后失效
  • protocol 网络协议 一般为tpc/udp
  • port 端口

    • 80 单个端口(80)
    • 8000-9000 从8000-9000端口
  • accept 接受
  • reject 拒绝
  • address

    • 1.1.1.1 单个ip
    • 1.1.1.1/24 IP段 从IP1.1.1.1-1.1.1.24

万变不离其宗,多尝试才是硬道理

开放端口

永久开放某个端口

firewall-cmd --zone=public --add-port=80/tcp --permanent           # 开启80端口
firewall-cmd --zone=public --add-port=8000-9000/tcp --permanent    # 开启8000-9000端口
firewall-cmd --reload        # 重新加载防火墙策略

临时开放某个端口(重启后失效)

firewall-cmd --zone=public --add-port=80/tcp     # 临时开启80端口
firewall-cmd --reload                            # 重新加载防火墙策略

切记最后的reload不要忘记,以重启防火墙策略

删除某个已经开放的端口(80)

firewall-cmd --zone=public --remove-port=80/tcp --permanent   # 删除80端口
firewall-cmd --reload       

阻止某个IP(1.1.1.1)的访问

firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="1.1.1.1" reject" 
firewall-cmd --reload       

仅允许特定的IP(1.1.1.1)访问特定的端口(80)

firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="1.1.1.1" port protocol="tcp" port="80" accept"
firewall-cmd --reload  

仅拒绝特定的IP(8.8.8.8)访问特定的端口(80)

firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="8.8.8.8" port protocol="tcp" port="80" reject"

启动服务

启用某个服务

# firewall-cmd --zone=public --add-service=https // 临时启动
# firewall-cmd --permanent --zone=public --add-service=https // 永久启动

仅允许特定的IP(1.1.1.1/24)访问特定的服务(http)

firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="1.1.1.1/24" service name="http" accept"     

#### 仅拒绝特定的IP(8.8.8.8/24)访问特定的服务(http)
firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="8.8.8.8/24" service name="http" reject"

Firewall配置文档

路径/etc/firewalld/zones/public.xml

cat /etc/firewalld/zones/public.xml             //打开文件
<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="ssh"/>
  <service name="dhcpv6-client"/>
  <service name="https"/>
  <port protocol="tcp" port="80"/>
  <port protocol="tcp" port="8000-9000"/>       // 开启的端口
  <rule family="ipv4">
    <source address="1.1.1.1"/>                 // 决绝特定的IP访问
    <reject/> 
  </rule>
  <rule family="ipv4">
    <source address="1.1.1.1"/>              
    <port protocol="tcp" port="80"/>   
    <accept/>                                   // 允许特定的IP(1.1.1.1)访问特定的端口(80)                
  </rule>
  <rule family="ipv4">
    <source address="8.8.8.8"/>
    <port protocol="tcp" port="80"/>            // 拒绝特定的IP(8.8.8.8)访问特定的端口(80) 
    <reject/>                                    
  </rule>  
  <rule family="ipv4">
    <source address="1.1.1.1/24"/>
    <service name="http"/>
    <reject/>                                   // 允许特定的IP(.1.1.1/24)访问特定的服务(http) 
  </rule>
  <rule family="ipv4">
    <source address="8.8.8.8/24"/>
    <service name="http"/>
    <reject/>                                   // 拒绝特定的IP(8.8.8.8/24)访问特定的服务(http)
  </rule>
</zone>

个人感觉firewalld使用起来比iptables简单,CentOS 7默认使用firewalld作为防火墙也肯定是有原因的,因此赶紧把这篇文章收藏了肯定没坏处(此处应有一个坏笑的表情)。上面仅提供了firewalld的基本使用方法,应对大部分情况应该足够了,如果需要详细了解参数含义可网上自行搜索,或者在下面留言,我很乐意为你解答

Last modification:December 12th, 2020 at 07:49 pm
如果觉得我的文章对你有用,请随意赞赏