简单实用的firewalld命令

一、查看防火墙是否打开

systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since 三 2023-04-19 21:06:31 CST; 7 months 19 days ago
     Docs: man:firewalld(1)
 Main PID: 708 (firewalld)
    Tasks: 2
   Memory: 892.0K
   CGroup: /system.slice/firewalld.service
           └─708 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
#active (running)表示已经打开,inactive (dead)表示未打开。
#firewalld默认是打开并且是开机自启的,当然我们也可以通过命令来进行设置。

systemctl enable --now firewalld         #开机自启,永久关闭把enable换成disable即可
Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.           

二、查询、开放、关闭端口

之前已经写过防火墙的详细介绍,有兴趣可以去看看,地址:https://blog.csdn.net/2301_76875445/article/details/130801308?spm=1001.2014.3001.5501

firewall-cmd --query-port=80/tcp  
yes                                               #单个端口查看,yes为80端口已打开,no则为未打开

firewall-cmd --list-ports
10514/udp 5432/tcp 8868-8873/tcp 9090/tcp 80/tcp  
firewall-cmd --list-ports --zone=public           
10514/udp 5432/tcp 8868-8873/tcp 9090/tcp 80/tcp  #显示指定区域内允许访问的所有端口号,不加--zone=区域名,默认显示的public

firewall-cmd --add-port=8080/tcp --permanent
sucess                                            #success表示端口已成功打开,不加--permanent重启防火墙时则规则失效。

firewall-cmd --remove-port=80/tcp
yes                                               #yes表示端口已成功关闭,

firewall-cmd --reload                             #重启防火墙(修改规则后要重启防火墙)

以上的前提是你的设备已经监听该端口,否则就算打开了规则也是链接不上的。

三、查看已监听端口

netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1340/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1056/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1055/cupsd          
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      6104/postmaster     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1305/master         
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      71335/sshd: root@pt 
tcp        0      0 127.0.0.1:6011          0.0.0.0:*               LISTEN      5764/sshd: root@pts 
tcp        0      0 127.0.0.1:6012          0.0.0.0:*               LISTEN      72570/sshd: root@pt 
tcp        0      0 0.0.0.0:8868            0.0.0.0:*               LISTEN      6375/argus_server   
tcp        0      0 0.0.0.0:8870            0.0.0.0:*               LISTEN      6381/argus_agentd   
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      608/rpcbind         
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6494/nginx: master  
tcp6       0      0 :::22                   :::*                    LISTEN      1056/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1055/cupsd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      1305/master         
tcp6       0      0 ::1:6010                :::*                    LISTEN      71335/sshd: root@pt 
tcp6       0      0 ::1:6011                :::*                    LISTEN      5764/sshd: root@pts 
tcp6       0      0 ::1:6012                :::*                    LISTEN      72570/sshd: root@pt 
tcp6       0      0 :::9090                 :::*                    LISTEN      6435/java           
tcp6       0      0 :::10086                :::*                    LISTEN      6405/java           
tcp6       0      0 :::8871                 :::*                    LISTEN      6276/docker-proxy   
tcp6       0      0 :::8873                 :::*                    LISTEN      7493/docker-proxy   
tcp6       0      0 :::111                  :::*                    LISTEN      608/rpcbind

当然ss -lntp也可以,但是没有这个直观。
Local :访问端口的方式,0.0.0.0 是对外开放端口,说明80端口外面可以访问;127.0.0.1 说明只能对本机访问,外面访问不了此端口;
Address:端口
Foregin Address:对外开放,一般都为0.0.0.0:*
Program name:此端口是那个程序在用,程序挂载此端口
重点说明 0.0.0.0 是对外开放,通过服务域名、ip可以访问的端口
::: 这三个: 的前两个”::“,是“0:0:0:0:0:0:0:0”的缩写,相当于IPv6的“0.0.0.0”,就是本机的所有IPv6地址,第三个:是IP和端口的分隔符

四、验证

telnet 192.168.119.30 80  #telnet加IP端口即可,IP与端口之间为空格,不要用冒号,进入即为成功。

一般是开通对外访问的端口,建议使用能ping通该台主机的Windows主机进行验证。

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值