Firewalld服务
firewalld服务管理
防火墙 Firewall 是由 Firewalld 这处守护进程来进行管理的。我可以使用 systemctl 命令来操作。
systemctl -help 可以查看关于systemctl的全部命令,也可以用man或者cat去查看
systemctl [OPTIONS...] COMMAND ...
Unit Commands:
is-active PATTERN... 判断服务是否启用
is-failed PATTERN... 判断服务是否失败
status [PATTERN...|PID...] 查看服务状态
show [PATTERN...|JOB...] 显示服务属性详细信息
start UNIT... 启动服务
stop UNIT... 停止服务
reload UNIT... 服务重载
restart UNIT... 重新启动服务
kill UNIT... 关闭服务
clean UNIT... 清理服务、缓存、状态等
Unit File Commands:
enable [UNIT...|PATH...] 将服务加入到启动开机列表中
disable UNIT... 将服务从开机启动列表中移除
mask UNIT... 将服务庶罩,即不能实现开机启动
unmask UNIT... 取消服务庶罩,即可以实现开机启动
Manager State Commands:
daemon-reload 重新加载守护进程
使用示例:
# 1. 启动防火墙
[root@dns ~]# systemctl start firewalld
# 2. 查看防火墙状态
[root@dns ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; >
Active: active (running) since Sat 2025-04-12 13:38:58 CST; 1h 9min >
Docs: man:firewalld(1)
Main PID: 864 (firewalld)
Tasks: 2 (limit: 24565)
Memory: 42.5M
CPU: 600ms
CGroup: /system.slice/firewalld.service
└─864 /usr/bin/python3 -s /usr/sbin/firewalld --nofork --nop>
Apr 12 13:38:54 dns systemd[1]: Starting firewalld - dynamic firewall dae>
Apr 12 13:38:58 dns systemd[1]: Started firewalld - dynamic firewall daem>
# 3. 关闭防火墙
[root@dns ~]# systemctl stop firewalld
# 4. 查看防火墙状态
[root@dns ~]# systemctl is-active firewalld.service
inactive
# 5. 将防火墙加入开机启动列表
[root@dns ~]# systemctl start firewalld.service
[root@dns ~]# systemctl enable firewalld
需要注意的是,实际上不一定要先启动防火墙才能设置开机自启。你可以直接执行systemctl enable firewalld,即便firewalld当前处于关闭状态,系统在下次启动时也会自动开启它。
# 6. 将防火墙从开机列表中移除
[root@dns ~]# systemctl stop firewalld.service
[root@dns ~]# systemctl disable firewalld.service
Removed "/etc/systemd/system/multi-user.target.wants/firewalld.service".
Removed "/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service".
# 7. 启动防火墙的同时将其加入到开机列表
[root@dns ~]# systemctl enable --now firewalld.service
Created symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service → /usr/lib/systemd/system/firewalld.service.
Created symlink /etc/systemd/system/multi-user.target.wants/firewalld.service → /usr/lib/systemd/system/firewalld.service.
enable:将 firewalld 服务添加到开机启动列表中。这样在系统下次启动时,firewalld 服务会自动启动。
--now:立即启动 firewalld 服务。即使服务当前处于停止状态,执行该命令后,服务会马上开始运行。
# 8. 停止防火墙的同时将其从开机列表中移除
[root@dns ~]# systemctl disable --now firewalld.service
Removed "/etc/systemd/system/multi-user.target.wants/firewalld.service".
Removed "/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service".
disable:此操作会将 firewalld 服务从开机启动列表中移除。也就是说,当系统下次启动时,firewalld 服务不会自动开启。
--now:这个选项的作用是立即停止当前正在运行的 firewalld 服务。若该服务当前处于运行状态,执行此命令后,它会马上停止工作。
Firewall-cmd
我们操作防火墙其实就是在使用 firewall-cmd 命令来向 nfstable 中加入相应的规则。我们先来看一下这个命令的格式:
[root@dns ~]# firewall-cmd --help
Usage: firewall-cmd [OPTIONS...]
General Options
-h, --help Prints a short help text and exits
-V, --version Print the version string of firewalld
-q, --quiet Do not print status messages
Status Options
--state Return and print firewalld state
--reload Reload firewall and keep state information
--complete-reload Reload firewall and lose state information
--runtime-to-permanent
Create permanent from runtime configuration
--reset-to-defaults
Reset configuration to firewalld's default configuration
--check-config Check permanent configuration for errors
Log Denied Options
--get-log-denied Print the log denied value
--set-log-denied=<value>
Set log denied value
Permanent Options
--permanent Set an option permanently
Usable for options marked with [P]
Zone Options
--get-default-zone Print default zone for connections and interfaces
--set-default-zone=<zone>
Set default zone
--get-active-zones Print currently active zones
--get-zones Print predefined zones [P]
--get-services Print predefined services [P]
--get-icmptypes Print predefined icmptypes [P]
--get-zone-of-interface=<interface>
Print name of the zone the interface is bound to [P]
--get-zone-of-source=<source>[/<mask>]|<MAC>|ipset:<ipset>
Print name of the zone the source is bound to [P]
--list-all-zones List everything added for or enabled in all zones [P]
--new-zone=<zone> Add a new zone [P only]
--new-zone-from-file=<filename> [--name=<zone>]
Add a new zone from file with optional name [P only]
--delete-zone=<zone> Delete an existing zone [P only]
--load-zone-defaults=<zone>
Load zone default settings [P only]
--zone=<zone> Use this zone to set or query options, else default zone
Usable for options marked with [Z]
--info-zone=<zone> Print information about a zone
--path-zone=<zone> Print file path of a zone [P only]
Policy Options
--get-policies Print predefined policies
--get-active-policies
Print currently active policies
--list-all-policies List everything added for or enabled in all policies
--new-policy=<policy>
Add a new empty policy
--new-policy-from-file=<filename> [--name=<policy>]
Add a new policy from file with optional name override [P only]
--delete-policy=<policy>
Delete an existing policy
--load-policy-defaults=<policy>
Load policy default settings
--policy=<policy> Use this policy to set or query options
Usable for options marked with [O]
--info-policy=<policy>
Print information about a policy
--path-policy=<policy>
Print file path of a policy
IPSet Options
--get-ipset-types Print the supported ipset types
--new-ipset=<ipset> --type=<ipset type> [--option=<key>[=<value>]]..
Add a new ipset [P only]
--new-ipset-from-file=<filename> [--name=<ipset>]
Add a new ipset from file with optional name [P only]
--delete-ipset=<ipset>
Delete an existing ipset [P only]
--load-ipset-defaults=<ipset>
Load ipset default settings [P only]
--info-ipset=<ipset> Print information about an ipset
--path-ipset=<ipset> Print file path of an ipset [P only]
--get-ipsets Print predefined ipsets
--ipset=<ipset> --set-description=<description>
Set new description to ipset [P only]
--ipset=<ipset> --get-description
Print description for ipset [P only]
--ipset=<ipset> --set-short=<description>
Set new short description to ipset [P only]
--ipset=<ipset> --get-short
Print short description for ipset [P only]
--ipset=<ipset> --add-entry=<entry>
Add a new entry to an ipset [P]
--ipset=<ipset> --remove-entry=<entry>
Remove an entry from an ipset [P]
--ipset=<ipset> --query-entry=<entry>
Return whether ipset has an entry [P]
--ipset=<ipset> --get-entries
List entries of an ipset [P]
--ipset=<ipset> --add-entries-from-file=<entry>
Add a new entries to an ipset [P]
--ipset=<ipset> --remove-entries-from-file=<entry>
Remove entries from an ipset [P]
IcmpType Options
--new-icmptype=<icmptype>
Add a new icmptype [P only]
--new-icmptype-from-file=<filename> [--name=<icmptype>]
Add a new icmptype from file with optional name [P only]
--delete-icmptype=<icmptype>
Delete an existing icmptype [P only]
--load-icmptype-defaults=<icmptype>
Load icmptype default settings [P only]
--info-icmptype=<icmptype>
Print information about an icmptype
--path-icmptype=<icmptype>
Print file path of an icmptype [P only]
--icmptype=<icmptype> --set-description=<description>
Set new description to icmptype [P only]
--icmptype=<icmptype> --get-description
Print description for icmptype [P only]
--icmptype=<icmptype> --set-short=<description>
Set new short description to icmptype [P only]
--icmptype=<icmptype> --get-short
Print short description for icmptype [P only]
--icmptype=<icmptype> --add-destination=<ipv>
Enable destination for ipv in icmptype [P only]
--icmptype=<icmptype> --remove-destination=<ipv>
Disable destination for ipv in icmptype [P only]
--icmptype=<icmptype> --query-destination=<ipv>
Return whether destination ipv is enabled in icmptype [P only]
--icmptype=<icmptype> --get-destinations
List destinations in icmptype [P only]
Service Options
--new-service=<service>
Add a new service [P only]
--new-service-from-file=<filename> [--name=<service>]
Add a new service from file with optional name [P only]
--delete-service=<service>
Delete an existing service [P only]
--load-service-defaults=<service>
Load icmptype default settings [P only]
--info-service=<service>
Print information about a service
--path-service=<service>
Print file path of a service [P only]
--service=<service> --set-description=<description>
Set new description to service [P only]
--service=<service> --get-description
Print description for service [P only]
--service=<service> --set-short=<description>
Set new short description to service [P only]
--service=<service> --get-short
Print short description for service [P only]
--service=<service> --add-port=<portid>[-<portid>]/<protocol>
Add a new port to service [P only]
--service=<service> --remove-port=<portid>[-<portid>]/<protocol>
Remove a port from service [P only]
--service=<service> --query-port=<portid>[-<portid>]/<protocol>
Return whether the port has been added for service [P only]
--service=<service> --get-ports
List ports of service [P only]
--service=<service> --add-protocol=<protocol>
Add a new protocol to service [P only]
--service=<service> --remove-protocol=<protocol>
Remove a protocol from service [P only]
--service=<service> --query-protocol=<protocol>
Return whether the protocol has been added for service [P only]
--service=<service> --get-protocols
List protocols of service [P only]
--service=<service> --add-source-port=<portid>[-<portid>]/<protocol>
Add a new source port to service [P only]
--service=<service> --remove-source-port=<portid>[-<portid>]/<protocol>
Remove a source port from service [P only]
--service=<service> --query-source-port=<portid>[-<portid>]/<protocol>
Return whether the source port has been added for service [P only]
--service=<service> --get-source-ports
List source ports of service [P only]
--service=<service> --add-helper=<helper>
Add a new helper to service [P only]
--service=<service> --remove-helper=<helper>
Remove a helper from service [P only]
--service=<service> --query-helper=<helper>
Return whether the helper has been added for service [P only]
--service=<service> --get-service-helpers
List helpers of service [P only]
--service=<service> --set-destination=<ipv>:<address>[/<mask>]
Set destination for ipv to address in service [P only]
--service=<service> --remove-destination=<ipv>
Disable destination for ipv i service [P only]
--service=<service> --query-destination=<ipv>:<address>[/<mask>]
Return whether destination ipv is set for service [P only]
--service=<service> --get-destinations
List destinations in service [P only]
--service=<service> --add-include=<service>
Add a new include to service [P only]
--service=<service> --remove-include=<service>
Remove a include from service [P only]
--service=<service> --query-include=<service>
Return whether the include has been added for service [P only]
--service=<service> --get-includes
List includes of service [P only]
Options to Adapt and Query Zones and Policies
--list-all List everything added for or enabled [P] [Z] [O]
--timeout=<timeval> Enable an option for timeval time, where timeval is
a number followed by one of letters 's' or 'm' or 'h'
Usable for options marked with [T]
--set-description=<description>
Set new description [P only] [Z] [O]
--get-description Print description [P only] [Z] [O]
--get-target Get the target [P only] [Z] [O]
--set-target=<target>
Set the target [P only] [Z] [O]
--set-short=<description>
Set new short description [Z] [O]
--get-short Print short description [P only] [Z] [O]
--list-services List services added [P] [Z]
--add-service=<service>
Add a service [P] [Z] [O] [T]
--remove-service=<service>
Remove a service [P] [Z] [O]
--query-service=<service>
Return whether service has been added [P] [Z] [O]
--list-ports List ports added [P] [Z] [O]
--add-port=<portid>[-<portid>]/<protocol>
Add the port [P] [Z] [O] [T]
--remove-port=<portid>[-<portid>]/<protocol>
Remove the port [P] [Z] [O]
--query-port=<portid>[-<portid>]/<protocol>
Return whether the port has been added [P] [Z] [O]
--list-protocols List protocols added [P] [Z] [O]
--add-protocol=<protocol>
Add the protocol [P] [Z] [O] [T]
--remove-protocol=<protocol>
Remove the protocol [P] [Z] [O]
--query-protocol=<protocol>
Return whether the protocol has been added [P] [Z] [O]
--list-source-ports List source ports added [P] [Z] [O]
--add-source-port=<portid>[-<portid>]/<protocol>
Add the source port [P] [Z] [O] [T]
--remove-source-port=<portid>[-<portid>]/<protocol>
Remove the source port [P] [Z] [O]
--query-source-port=<portid>[-<portid>]/<protocol>
Return whether the source port has been added [P] [Z] [O]
--list-icmp-blocks List Internet ICMP type blocks added [P] [Z] [O]
--add-icmp-block=<icmptype>
Add an ICMP block [P] [Z] [O] [T]
--remove-icmp-block=<icmptype>
Remove the ICMP block [P] [Z] [O]
--query-icmp-block=<icmptype>
Return whether an ICMP block has been added [P] [Z] [O]
--list-forward-ports List IPv4 forward ports added [P] [Z] [O]
--add-forward-port=port=<portid>[-<portid>]:proto=<protocol>[:toport=<portid>[-<portid>]][:toaddr=<address>[/<mask>]]
Add the IPv4 forward port [P] [Z] [O] [T]
--remove-forward-port=port=<portid>[-<portid>]:proto=<protocol>[:toport=<portid>[-<portid>]][:toaddr=<address>[/<mask>]]
Remove the IPv4 forward port [P] [Z] [O]
--query-forward-port=port=<portid>[-<portid>]:proto=<protocol>[:toport=<portid>[-<portid>]][:toaddr=<address>[/<mask>]]
Return whether the IPv4 forward port has been added [P] [Z] [O]
--add-masquerade Enable IPv4 masquerade [P] [Z] [O] [T]
--remove-masquerade Disable IPv4 masquerade [P] [Z] [O]
--query-masquerade Return whether IPv4 masquerading has been enabled [P] [Z] [O]
--list-rich-rules List rich language rules added [P] [Z] [O]
--add-rich-rule=<rule>
Add rich language rule 'rule' [P] [Z] [O] [T]
--remove-rich-rule=<rule>
Remove rich language rule 'rule' [P] [Z] [O]
--query-rich-rule=<rule>
Return whether a rich language rule 'rule' has been
added [P] [Z] [O]
Options to Adapt and Query Zones
--add-icmp-block-inversion
Enable inversion of icmp blocks for a zone [P] [Z]
--remove-icmp-block-inversion
Disable inversion of icmp blocks for a zone [P] [Z]
--query-icmp-block-inversion
Return whether inversion of icmp blocks has been enabled
for a zone [P] [Z]
--add-forward Enable forwarding of packets between interfaces and
sources in a zone [P] [Z] [T]
--remove-forward Disable forwarding of packets between interfaces and
sources in a zone [P] [Z]
--query-forward Return whether forwarding of packets between interfaces
and sources has been enabled for a zone [P] [Z]
Options to Adapt and Query Policies
--get-priority Get the priority [P only] [O]
--set-priority=<priority>
Set the priority [P only] [O]
--list-ingress-zones
List ingress zones that are bound to a policy [P] [O]
--add-ingress-zone=<zone>
Add the ingress zone to a policy [P] [O]
--remove-ingress-zone=<zone>
Remove the ingress zone from a policy [P] [O]
--query-ingress-zone=<zone>
Query whether the ingress zone has been adedd to a
policy [P] [O]
--list-egress-zones
List egress zones that are bound to a policy [P] [O]
--add-egress-zone=<zone>
Add the egress zone to a policy [P] [O]
--remove-egress-zone=<zone>
Remove the egress zone from a policy [P] [O]
--query-egress-zone=<zone>
Query whether the egress zone has been adedd to a
policy [P] [O]
Options to Handle Bindings of Interfaces
--list-interfaces List interfaces that are bound to a zone [P] [Z]
--add-interface=<interface>
Bind the <interface> to a zone [P] [Z]
--change-interface=<interface>
Change zone the <interface> is bound to [P] [Z]
--query-interface=<interface>
Query whether <interface> is bound to a zone [P] [Z]
--remove-interface=<interface>
Remove binding of <interface> from a zone [P] [Z]
Options to Handle Bindings of Sources
--list-sources List sources that are bound to a zone [P] [Z]
--add-source=<source>[/<mask>]|<MAC>|ipset:<ipset>
Bind the source to a zone [P] [Z]
--change-source=<source>[/<mask>]|<MAC>|ipset:<ipset>
Change zone the source is bound to [Z]
--query-source=<source>[/<mask>]|<MAC>|ipset:<ipset>
Query whether the source is bound to a zone [P] [Z]
--remove-source=<source>[/<mask>]|<MAC>|ipset:<ipset>
Remove binding of the source from a zone [P] [Z]
Direct Options
--direct First option for all direct options
--get-all-chains
Get all chains [P]
--get-chains {ipv4|ipv6|eb} <table>
Get all chains added to the table [P]
--add-chain {ipv4|ipv6|eb} <table> <chain>
Add a new chain to the table [P]
--remove-chain {ipv4|ipv6|eb} <table> <chain>
Remove the chain from the table [P]
--query-chain {ipv4|ipv6|eb} <table> <chain>
Return whether the chain has been added to the table [P]
--get-all-rules
Get all rules [P]
--get-rules {ipv4|ipv6|eb} <table> <chain>
Get all rules added to chain in table [P]
--add-rule {ipv4|ipv6|eb} <table> <chain> <priority> <arg>...
Add rule to chain in table [P]
--remove-rule {ipv4|ipv6|eb} <table> <chain> <priority> <arg>...
Remove rule with priority from chain in table [P]
--remove-rules {ipv4|ipv6|eb} <table> <chain>
Remove rules from chain in table [P]
--query-rule {ipv4|ipv6|eb} <table> <chain> <priority> <arg>...
Return whether a rule with priority has been added to
chain in table [P]
--passthrough {ipv4|ipv6|eb} <arg>...
Pass a command through (untracked by firewalld)
--get-all-passthroughs
Get all tracked passthrough rules [P]
--get-passthroughs {ipv4|ipv6|eb} <arg>...
Get tracked passthrough rules [P]
--add-passthrough {ipv4|ipv6|eb} <arg>...
Add a new tracked passthrough rule [P]
--remove-passthrough {ipv4|ipv6|eb} <arg>...
Remove a tracked passthrough rule [P]
--query-passthrough {ipv4|ipv6|eb} <arg>...
Return whether the tracked passthrough rule has been
added [P]
Lockdown Options 禁用选项
--lockdown-on Enable lockdown.
--lockdown-off Disable lockdown.
--query-lockdown Query whether lockdown is enabled
Lockdown Whitelist Options 禁用白名单
--list-lockdown-whitelist-commands
List all command lines that are on the whitelist [P]
--add-lockdown-whitelist-command=<command>
Add the command to the whitelist [P]
--remove-lockdown-whitelist-command=<command>
Remove the command from the whitelist [P]
--query-lockdown-whitelist-command=<command>
Query whether the command is on the whitelist [P]
--list-lockdown-whitelist-contexts
List all contexts that are on the whitelist [P]
--add-lockdown-whitelist-context=<context>
Add the context context to the whitelist [P]
--remove-lockdown-whitelist-context=<context>
Remove the context from the whitelist [P]
--query-lockdown-whitelist-context=<context>
Query whether the context is on the whitelist [P]
--list-lockdown-whitelist-uids
List all user ids that are on the whitelist [P]
--add-lockdown-whitelist-uid=<uid>
Add the user id uid to the whitelist [P]
--remove-lockdown-whitelist-uid=<uid>
Remove the user id uid from the whitelist [P]
--query-lockdown-whitelist-uid=<uid>
Query whether the user id uid is on the whitelist [P]
--list-lockdown-whitelist-users
List all user names that are on the whitelist [P]
--add-lockdown-whitelist-user=<user>
Add the user name user to the whitelist [P]
--remove-lockdown-whitelist-user=<user>
Remove the user name user from the whitelist [P]
--query-lockdown-whitelist-user=<user>
Query whether the user name user is on the whitelist [P]
Panic Options 用于处理急增流量
--panic-on Enable panic mode
--panic-off Disable panic mode
--query-panic Query whether panic mode is enabled
使用示例:
1.区域操作
1. 增加区域
[root@dns ~]# firewall-cmd --permanent --new-zone=test
success
作用:使用 --permanent 选项表示此配置会永久保存,重启防火墙后依然有效。--new-zone=test 用于创建一个名为 test 的新防火墙区域。在 firewalld 中,区域是一种将网络接口和源地址进行分组的机制,不同区域可以有不同的防火墙规则,方便对不同的网络环境或用途进行管理。
2. 重新加载让配置生效
[root@dns ~]# firewall-cmd --reload
success
作用:当使用 --permanent 选项修改了防火墙配置后,需要重新加载防火墙配置才能使新的配置生效。--reload 命令会在不中断现有连接的情况下重新加载 firewalld 的配置文件。
3. 查看所有区域
[root@dns ~]# firewall-cmd --get-zones
block dmz drop external home internal nm-shared public test trusted work
作用:--get-zones 命令用于显示当前 firewalld 中所有已定义的区域。通过查看所有区域,可以确认新创建的 test 区域是否已经成功添加到系统中。
4. 查看默认区域
[root@dns ~]# firewall-cmd --get-default-zone
public
作用:--get-default-zone 命令用于获取当前系统的默认防火墙区域。默认区域是指当网络接口或源地址没有明确指定所属区域时,会使用的区域。通常,新添加的网络接口会默认使用这个区域的规则。
5. 设置默认区域为 test
[root@dns ~]# firewall-cmd --set-default-zone=test
success
[root@dns ~]# firewall-cmd --reload
success
[root@dns ~]# firewall-cmd --get-default-zone
test
作用:--set-default-zone=test 命令将系统的默认防火墙区域设置为 test。之后,新添加的网络接口如果没有指定所属区域,将应用 test 区域的防火墙规则。同样,修改默认区域后需要使用 --reload 命令使配置生效。
6. 删除区域
[root@dns ~]# firewall-cmd --permanent --delete-zone=test
作用:
资源清理:当某个区域不再需要时,使用 --permanent 选项结合 --delete-zone=test 命令可以将名为 test 的区域从 firewalld 中永久删除。这样可以清理系统中不必要的配置,减少管理的复杂性。
避免混淆:如果存在不再使用的区域,可能会在后续的防火墙管理中造成混淆,尤其是在查看和修改规则时。删除这些无用的区域可以确保系统中只保留有实际用途的区域配置。
安全考虑:如果某个区域的规则配置存在安全风险或者已经不符合当前的安全策略,删除该区域可以避免潜在的安全问题。删除后,需要重新加载防火墙配置(使用 --reload 命令)才能使删除操作生效。
注意:如果不加permanent也行,不过进行的操作只在当前的会话有效,不用reload也能生效哈。
可能遇到的问题:
当你在使用 firewalld 命令时遇到 Error: INVALID_ZONE: test 错误,这表明你指定的区域 test 是无效的。下面为你分析可能的原因以及对应的解决办法:
可能的原因
区域未创建:你可能尝试对一个根本未创建的区域进行操作。例如,在执行修改规则、设置默认区域等操作前,没有使用 firewall-cmd --permanent --new-zone=test 命令创建 test 区域。
配置未生效:即便你已经创建了 test 区域,但如果没有使用 firewall-cmd --reload 命令重新加载防火墙配置,那么新创建的区域不会生效,在后续操作时就会被认为是无效区域。
区域已删除:如果你之前已经使用 firewall-cmd --permanent --delete-zone=test 命令删除了 test 区域,并且重新加载了配置,那么再次对该区域进行操作时就会出现此错误。
2.对服务的操作:
1. 查看所有信息
[root@dns ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens160
sources:
services: cockpit dhcpv6-client ssh
ports:
protocols:
forward: yes
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
作用:--list-all 选项用于显示当前活动区域(这里是 public 区域)的所有防火墙配置信息。这包括目标策略、关联的网络接口、允许的服务、开放的端口、转发规则等。通过查看这些信息,管理员可以全面了解当前防火墙的状态和配置,便于进行后续的调整和管理。
2. 查看已允许的服务列表
[root@dns ~]# firewall-cmd --list-services
cockpit dhcpv6-client ssh
作用:--list-services 选项专门用于列出当前区域中允许通过防火墙的服务列表。这有助于管理员快速了解哪些服务是被允许访问的,方便进行安全审计和服务管理。
3. 将 http 服务添加到服务允许列表中
[root@dns ~]# firewall-cmd --permanent --add-service=http
success
[root@dns ~]# firewall-cmd --reload
success
[root@dns ~]# firewall-cmd --list-services
cockpit dhcpv6-client http ssh
作用:
--permanent 选项表示该配置将被永久保存,即使防火墙服务重启或系统重启,配置也不会丢失。
--add-service=http 用于将 http 服务添加到允许的服务列表中。这意味着防火墙将允许外部网络对本机的 http 服务(通常是端口 80)进行访问。
--reload 命令用于重新加载防火墙配置,使新添加的服务规则生效。
4. 将 https 和 dns 服务加入到服务允许列表中
[root@dns ~]# firewall-cmd --permanent --add-service=https --add-service=dns
success
[root@dns ~]# firewall-cmd --reload
success
作用:
同样使用 --permanent 选项确保配置持久化。
--add-service=https 和 --add-service=dns 分别将 https 服务(通常是端口 443)和 dns 服务(通常是端口 53)添加到允许的服务列表中,允许外部网络对这些服务进行访问。
再次使用 --reload 命令使新配置生效。
5. 查询服务
[root@dns ~]# firewall-cmd --query-service=http
yes
作用:--query-service=http 用于查询 http 服务是否在允许的服务列表中。如果返回 yes,表示该服务已被允许通过防火墙;如果返回 no,则表示该服务未被允许。
6. 删除 http 服务
bash
[root@dns ~]# firewall-cmd --permanent --remove-service=http
success
[root@dns ~]# firewall-cmd --reload
success
[root@dns ~]# firewall-cmd --query-service=http
no
作用:
--permanent 选项确保删除操作是永久性的。
--remove-service=http 用于将 http 服务从允许的服务列表中移除,这意味着防火墙将不再允许外部网络对本机的 http 服务进行访问。
--reload 命令用于重新加载防火墙配置,使删除操作生效。
最后使用 --query-service=http 进行验证,返回 no 表示 http 服务已成功从允许列表中删除。
3.对端口的操作
1. 查看已允许的端口列表
[root@dns ~]# firewall-cmd --list-ports
作用:此命令用于查看当前防火墙允许通过的端口列表。端口是网络通信的通道,通过查看允许的端口列表,系统管理员可以清楚知晓哪些端口是开放的,以便进行网络安全审计和管理。若发现有异常开放的端口,可及时采取措施关闭。
2. 将 53 端口加入到允许的列表中
[root@dns ~]# firewall-cmd --permanent --add-port=53/tcp --add-port=53/udp
success
[root@dns ~]# firewall-cmd --reload
success
[root@dns ~]# firewall-cmd --list-ports
53/tcp 53/udp
--permanent 选项:表明此配置会被永久保存。即便防火墙服务重启或者系统重启,该配置依然有效。
--add-port=53/tcp --add-port=53/udp:将 TCP 协议的 53 端口和 UDP 协议的 53 端口添加到防火墙允许通过的端口列表中。在网络中,53 端口常用于 DNS(域名系统)服务,开放这两个端口可让外部网络能够与本地的 DNS 服务进行通信。
--reload 命令:重新加载防火墙配置,使新添加的端口规则生效。因为使用 --permanent 修改配置后,需重新加载才能让修改在当前运行的防火墙中起作用。
3. 将 53 端口从允许列表中移除
[root@dns ~]# firewall-cmd --permanent --remove-port=53/tcp
success
[root@dns ~]# firewall-cmd --reload
success
[root@dns ~]# firewall-cmd --list-ports
53/udp
--permanent 选项:保证删除操作是永久性的,重启防火墙或系统后该端口仍会保持被移除的状态。
--remove-port=53/tcp:将 TCP 协议的 53 端口从防火墙允许通过的端口列表中移除,即不再允许外部网络通过 TCP 协议访问本地的 53 端口。
--reload 命令:重新加载防火墙配置,使删除操作生效。
再次执行 --list-ports:验证 TCP 协议的 53 端口已被移除,此时列表中仅剩下 UDP 协议的 53 端口。
4. 查询端口
[root@dns ~]# firewall-cmd --query-port=70 - 80/tcp
no
作用:--query-port=70 - 80/tcp 用于查询 TCP 协议的 70 到 80 端口范围是否在防火墙允许通过的端口列表中。返回 no 表示该端口范围未被允许通过防火墙,返回 yes 则表示允许。通过此命令,管理员可快速确认某个端口或端口范围是否开放,便于进行网络配置和安全检查。
疑问点:
允许80端口和允许http服务作用是一样的吗
两者的联系
常见关联:在互联网中,HTTP 服务默认使用 80 端口进行通信。当你允许防火墙开放 80 端口时,通常是为了让外部客户端能够访问本地的 HTTP 服务。例如,你在服务器上部署了一个基于 Apache 或者 Nginx 的网站,这属于 HTTP 服务,为了让用户能够通过浏览器访问该网站,就需要在防火墙中允许 80 端口的流量通过。
实现访问:允许 80 端口是运行 HTTP 服务并让外部用户能够访问的一个必要条件。如果防火墙阻止了 80 端口的流量,那么即使服务器上的 HTTP 服务正常运行,外部用户也无法连接到该服务。
两者的区别
概念不同
允许 80 端口:这是一个网络层面的配置操作,主要是针对防火墙而言。它意味着防火墙不会对通过 80 端口的入站或出站流量进行阻止,允许数据包在该端口上进行传输。
运行 HTTP 服务:这是一个应用层面的操作,指的是在服务器上启动了一个能够处理 HTTP 请求的服务程序,如 Apache、Nginx 或 Tomcat 等。这些服务程序会监听 80 端口(默认情况下),接收客户端发送的 HTTP 请求,并返回相应的响应。
操作范围不同
允许 80 端口:只涉及到网络访问控制,不关心服务器上是否真正运行着 HTTP 服务。即使允许了 80 端口,如果服务器上没有运行 HTTP 服务,客户端访问该端口时也不会得到有效的响应。
运行 HTTP 服务:重点在于服务器上应用程序的启动和运行。即使防火墙没有开放 80 端口,HTTP 服务仍然可以在服务器内部正常运行,只是外部客户端无法访问而已。
其他用途
允许 80 端口:80 端口并不一定只能用于 HTTP 服务。在某些特殊情况下,其他应用程序也可能会使用 80 端口进行通信。例如,一些自定义的网络服务可能会选择使用 80 端口来避免被防火墙阻止。
运行 HTTP 服务:HTTP 服务也不一定只能使用 80 端口。可以通过配置 HTTP 服务程序,让它监听其他端口,如 8080、8888 等。在这种情况下,就需要相应地在防火墙中开放对应的端口,而不是 80 端口。
综上所述,允许 80 端口和运行 HTTP 服务虽然有紧密的联系,但它们代表了不同层面的操作,具有不同的含义和作用。
允许80端口和允许http服务是一样的吗
允许 80 端口和允许 HTTP 服务有相似之处,但并不完全一样,下面从多个方面为你详细分析:
相同点
目的关联:在多数常规场景下,二者目的一致。HTTP 服务默认使用 TCP 协议的 80 端口进行数据传输。当你允许 80 端口时,通常是希望外部客户端能够访问服务器上的 HTTP 服务;允许 HTTP 服务时,防火墙也会开放 80 端口来让流量通过,从而达成 HTTP 服务的正常访问。
访问效果:在默认配置下,无论你选择允许 80 端口还是允许 HTTP 服务,最终结果都是让客户端可以通过网络访问服务器上运行的 HTTP 服务。
不同点
操作层面
允许 80 端口:这属于网络层面的操作,关注点在于防火墙对特定端口的流量控制。它只是简单地让数据包可以在 80 端口进出,不关心该端口上实际运行的是什么服务。即使 80 端口被允许,但服务器上可能并未运行 HTTP 服务,或者运行的是其他使用 80 端口的自定义服务。
允许 HTTP 服务:这是服务层面的操作,防火墙会基于服务的规则来配置。它不仅会开放 80 端口,还可能会根据 HTTP 服务的特性设置一些额外的规则,如对 HTTP 请求方法、头部信息等进行过滤。
灵活性和扩展性
允许 80 端口:较为简单直接,但缺乏灵活性。如果后续需要对 HTTP 服务的访问进行更细致的控制,仅允许 80 端口就无法满足需求。
允许 HTTP 服务:具有更好的扩展性和管理性。防火墙可以根据 HTTP 服务的具体规则进行配置,方便进行安全策略的调整和优化。例如,你可以设置只允许特定 IP 地址的客户端访问 HTTP 服务,或者限制特定时间段内的请求次数等。
端口使用情况
允许 80 端口:只是针对 80 端口本身。如果 HTTP 服务被配置为使用其他端口(如 8080 端口),那么允许 80 端口并不能让客户端访问到该 HTTP 服务。
允许 HTTP 服务:即使 HTTP 服务监听的不是默认的 80 端口,防火墙也能根据服务的配置开放相应的端口,保证服务的正常访问。