防火墙命令firewall-cmd基本用法
安装和管理firewalld
|
|
基础命令
一些常用的命令如下:
- 查看防火墙状态
命令:firewall-cmd --list-all
12345678910111213141516$ sudo firewall-cmd --list-allpublic (active)target: defaulticmp-block-inversion: nointerfaces: ens33sources:services: ssh dhcpv6-clientports:protocols:masquerade: noforward-ports:source-ports:icmp-blocks:rich rules:$
列出支持的服务
命令:firewall-cmd --get-services
123$ sudo firewall-cmd --get-servicesRH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client bgp bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client dns docker-registry docker-swarm dropbox-lansync elasticsearch freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ganglia-client ganglia-master git gre high-availability http https imap imaps ipp ipp-client ipsec irc ircs iscsi-target jenkins kadmin kerberos kibana klogin kpasswd kprop kshell ldap ldaps libvirt libvirt-tls managesieve mdns minidlna mongodb mosh mountd ms-wbt mssql murmur mysql nfs nfs3 nmea-0183 nrpe ntp openvpn ovirt-imageio ovirt-storageconsole ovirt-vmconsole pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster quassel radius redis rpc-bind rsh rsyncd samba samba-client sane sip sips smtp smtp-submission smtps snmp snmptrap spideroak-lansync squid ssh syncthing syncthing-gui synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client upnp-client vdsm vnc-server wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server zabbix-agent zabbix-server$添加某个服务
命令:firewall-cmd --add-service=<service> --permanent
1sudo firewall-cmd --add-service=http --permanent删除某个服务
命令:firewall-cmd --remove-service=<service> --permanent
1sudo firewall-cmd --remove-service=dhcpv6-client --permanent添加某个端口
命令:firewall-cmd --add-port=<portid>[-<portid>]/<protocol> --permanent
123456789101112131415161718# 添加rtmp端口$ sudo firewall-cmd --add-port=1935/tcp --permanent$ sudo firewall-cmd --reload$ sudo firewall-cmd --list-allpublic (active)target: defaulticmp-block-inversion: nointerfaces: ens33sources:services: ssh dhcpv6-client httpports: 1935/tcpprotocols:masquerade: noforward-ports:source-ports:icmp-blocks:rich rules:$删除某个端口
命令:firewall-cmd --remove-port=<portid>[-<portid>]/<protocol> --permanent
重启防火墙
添加或删除服务和端口后,需要重新load防火墙,才能生效
命令:firewall-cmd --reload
1sudo firewall-cmd --reload
permanent参数
规则添加命令中--permanent
参数的说明:
- 不添加
--permanent
, 表示规则是临时的,对当前系统即时生效。重启防火墙服务后不会保留该规则。 - 添加
--permanent
, 表示规则永久写入,但是不对当前环境生效,需要firewall-cmd --reload
后才能生效。
命令firewall-cmd --list-all
查看的是当前有效的规则。所以如果只添加了--permanent
,但没有firewall-cmd --reload
,因为规则未生效,所以不会在firewall-cmd --list-all
的输出中出现。
自定义service文件
service文件介绍
firewalld内置了一些常用的services,对应的配置文件保存在/usr/lib/firewalld/services
中,使用命令firewall-cmd --add-service=<service>
可以很方便地添加这些服务。
很多情况下,我们要开放的端口不在这些预定义的服务中,此时有两个办法来实现
- 使用
--add-port=<portid>[-<portid>]/<protocol>
来直接添加端口 - 自建services配置文件,并添加到firewalld中
推荐使用自建services的方式,使用services方式的优点
- 一个配置文件对应一个服务,更清晰
- 配置文件中,可以添加服务描述
- 配置文件中,可以添加多个端口和协议,如预置的rsyncd.xml中,就同时添加了873端口的TCP和UDP协议。
自建service文件
firewalld用到的所有service配置文件的存放路径是/usr/lib/firewalld/services
,但自建的service文件不要直接写入该目录,而应该在/etc/firewalld/services/
中进行设置。
下面是一个root下新建rtmp service的例子。
|
|
端口转发
转发到本地
命令: firewall-cmd --add-forward-port=port=<portid>[-<portid>]:proto=<protocol>[:toport=<portid>[-<portid>]][:toaddr=<address>[/<mask>]]
例子: 转发80端口流量到12345端口
转发到其他机器
转发到其他机器时,需要开启masquerade(伪装)功能。
Masquerading will forward packets that are not directed to an IP address associated to the system itself onto the intended destination. The source IP address of the packets that are sent through our system will be changed to the IP address of our system, rather than the IP address of the original traffic source. Responses to these packets will then go through our system and the destination address will be modified so that the traffic will be sent back to the original host that initiated the traffic.
使用firewall-cmd --add-masquerade
开启masquerade。
例子: 转发本地80端口到IP 10.0.0.2上的8080端口中
Rich rules细粒度控制
FirewallD的Rich rules 可以提供更细粒度的控制。可以使用man 5 firewalld.richlanguage
来查看rich rules的语法,
例子:
允许来自192.168.0.14的所有IPv4流量
拒绝来自192.168.1.10的基于TCP的请求访问22端口, 也就是拒绝192.168.1.10进行ssh
只允许来自192.168.187.128/25的请求访问22端口,拒绝其余的ssh登录
允许来自10.1.0.3的TCP请求到80端口,并且转发到本地的6532端口。
将来自172.31.4.2的访问80端口的TCP请求转发到主机172.31.4.2的8080端口。
列出所有的rich rules
firewall系列命令介绍
转载自: firewall防火墙
查看firewalld的状态
1firewall-cmd --state查看活动分区类别
1firewall-cmd --get-active-zones查看当前分配的接口
1firewall-cmd --get-zone-of-interface=ens33查看分配的区域的所有接口:
1firewall-cmd --zone=public --list-interfaces找出公共区域的所有设置
12firewall-cmd --zone=public --list-allfirewall-cmd --list-all关闭|开启所有的输入和输出的数据包(禁用)
123456# 关闭所有输入输出的数据包firewall-cmd --panic-on# 开启再次输入输出的数据包firewall-cmd --panic-off# 查看panic模式的状态(yes启用 no退出)firewall-cmd --query-panic重新加载防火墙
1234# 重新加载防火墙,不中断用户连接(不丢失状态信息)firewall-cmd --reload# 重新加载防火墙并中断用户连接(丢失状态信息),防火墙出现严重问题才执行firewall-cmd --complete-reload为分区增加接口
12# 把em1增加到公共分区,增加--permanent选择并重新加载防火墙,是之永久生效firewall-cmd --zone=public --add-interface=em1设置分区(将一个端口加入分区)
123456789# 设置默认分区,立即生效,不需要重新加载防火墙firewall-cmd --set-default-zone=public# 将一个端口加入分区firewall-cmd --zone=public --permanent --add-port=8080/tcpfirewall-cmd --reload# 查看开发的端口firewall-cmd --zone=public --list-ports# 删除firewll-cmd --zone=pubic --remove --remove-port=8080/tcp将一个服务(http)加入到分区
1234567# 加入firewall-cmd --permanent --zone=work --add-service=httpfirewall-cmd --reload# 移除firewall-cmd --permanent --zone=work --remove-service=httpfirewall-cmd --reload# 注意,这并不会中断已经建立的连接。如果您打算中断,您可以使用 --complete-reload 选项,但这不仅仅中断您已经移除的服务,还会中断所有已经建立的连接。配置伪装 IP 地址
123456# 查询是否可用firewall-cmd --zone=external --query-masquerade# 允许伪装IPfirewall-cmd --zone=external --add-masquerade# 禁用伪装IPfirewall-cmd --zone=external --remove-masquerade配置端口转发,地址转发
1234567# 将22端口转发到3753端口,使用tcp协议firewall-cmd --zone=external --add-forward-port=port=22:proto=tcp:toport=3753# 原本发往22端口的程序包现在被转发到地址是192.0.2.55下相同的端口firewall-cmd --zone=external --add-forward-port=port=22:proto=tcp:toaddr=192.0.2.55# 将22端口的数据包转发到192.0.2.55下的2055端口firewall-cmd --zone=external /--add-forward-port=port=22:proto=tcp:toport=2055:toaddr=192.0.2.55使用直接接口增加一个自定义规则
12345678# 增加一个自定义规则到 “IN_public_allow” 链里:firewall-cmd --direct --add-rule ipv4 filter IN_public_allow \0 -m tcp -p tcp --dport 666 -j ACCEPT# 从 “IN_public_allow” 链移除一个自定义规则:firewall-cmd --direct --remove-rule ipv4 filter IN_public_allow \0 -m tcp -p tcp --dport 666 -j ACCEPT# 列出 “IN_public_allow” 链中的规则:firewall-cmd --direct --get-rules ipv4 filter IN_public_allow配置rich language语法
格式:123456# 添加firewall-cmd [--zone=zone] --add-rich-rule='rule' [--timeout 9=seconds]# 移除firewall-cmd [--zone=zone] --remove-rich-rule='rule'# 检查是否存在firewall-cmd [--zone=zone] --query-rich-rule='rule'
多规则结构:
多规则命令:
运行来自主机192.168.1.3的所有IPv4流量
拒绝来自主机192.168.1.4到22端口的tcp流量
允许主机10.1.0.3到80端口的IPv4的TCP流量,并转发到65530端口上