当前位置:   article > 正文

Linux防火墙和firewall-cmd命令应用

firewall-cmd

记录:341

场景:在CentOS 7.9操作系统上,操作防火墙firewalld,主要是查看、开启、关闭以及禁用等。firewall-cmd命令查看防火墙和修改防火墙相关配置。

版本:

操作系统:CentOS 7.9

1.使用systemctl命令操作防火服务

(1)查看防火墙状态

查看状态:systemctl status firewalld

解析:查看防火墙状态,如果是Active: active (running),则已经开启防火墙。如果是Active: inactive (dead),则已关闭防火墙。

(2)开启防火墙

开启防火墙:systemctl start firewalld

解析:开启防火墙。

(3)关闭防火墙

关闭防火墙:systemctl stop firewalld

解析:关闭防火墙。

(4)重启防火墙

重启防火墙:systemctl restart firewalld

解析:重启防火墙。

(5)设置开机启用防火墙

开机启用防火墙:systemctl enable firewalld

解析:开机启用防火墙。

(6)设置开机禁用防火墙

设置开机禁用防火墙:systemctl disable firewalld

解析:设置开机禁用防火墙,主机启动时,就会关闭防火墙。

2.firewall-cmd命令应用

(1)查看防火墙已放行的端口号列表

命令:firewall-cmd --zone=public --list-ports

解析:查看防火墙已放行的端口号列表。

(2)查看指定端口防火墙放行状态

命令:firewall-cmd --permanent --query-port="18080"/tcp

解析:查看端口防火墙已放行。

(3)把端口18080添加到防火墙开放端口列表

添加端口:firewall-cmd --zone=public --add-port="18080"/tcp --permanent

解析:把18080端口持久化配置到开放端口列表中;--permanent,使用永久设置选项。

(4)重新加载防火墙

命令:firewall-cmd --reload

解析:加载防火墙,使最新配置生效。

(5)查看防火墙状态

命令:firewall-cmd --state

解析:查看防火墙状态。

(6)查看防火墙版本

命令:firewall-cmd --version

解析:查看防火墙版本。

(7)查看防火墙zone

命令:firewall-cmd --get-default-zone

解析:打印连接和接口的默认zone,本例打印:public,那么在给--zone添加参数时,可以是:--zone=public。

3.firewall-cmd命令帮助手册

命令:firewall-cmd --help

解析:查看firewall-cmd支持全部命令和选项,在实际工作中,查看这个手册应该是必备之选。

  1. Usage: firewall-cmd [OPTIONS...]
  2. General Options
  3. -h, --help Prints a short help text and exists
  4. -V, --version Print the version string of firewalld
  5. -q, --quiet Do not print status messages
  6. Status Options
  7. --state Return and print firewalld state
  8. --reload Reload firewall and keep state information
  9. --complete-reload Reload firewall and lose state information
  10. --runtime-to-permanent
  11. Create permanent from runtime configuration
  12. --check-config Check permanent configuration for errors
  13. Log Denied Options
  14. --get-log-denied Print the log denied value
  15. --set-log-denied=<value>
  16. Set log denied value
  17. Automatic Helpers Options
  18. --get-automatic-helpers
  19. Print the automatic helpers value
  20. --set-automatic-helpers=<value>
  21. Set automatic helpers value
  22. Permanent Options
  23. --permanent Set an option permanently
  24. Usable for options marked with [P]
  25. Zone Options
  26. --get-default-zone Print default zone for connections and interfaces
  27. --set-default-zone=<zone>
  28. Set default zone
  29. --get-active-zones Print currently active zones
  30. --get-zones Print predefined zones [P]
  31. --get-services Print predefined services [P]
  32. --get-icmptypes Print predefined icmptypes [P]
  33. --get-zone-of-interface=<interface>
  34. Print name of the zone the interface is bound to [P]
  35. --get-zone-of-source=<source>[/<mask>]|<MAC>|ipset:<ipset>
  36. Print name of the zone the source is bound to [P]
  37. --list-all-zones List everything added for or enabled in all zones [P]
  38. --new-zone=<zone> Add a new zone [P only]
  39. --new-zone-from-file=<filename> [--name=<zone>]
  40. Add a new zone from file with optional name [P only]
  41. --delete-zone=<zone> Delete an existing zone [P only]
  42. --load-zone-defaults=<zone>
  43. Load zone default settings [P only] [Z]
  44. --zone=<zone> Use this zone to set or query options, else default zone
  45. Usable for options marked with [Z]
  46. --get-target Get the zone target [P only] [Z]
  47. --set-target=<target>
  48. Set the zone target [P only] [Z]
  49. --info-zone=<zone> Print information about a zone
  50. --path-zone=<zone> Print file path of a zone [P only]
  51. IPSet Options
  52. --get-ipset-types Print the supported ipset types
  53. --new-ipset=<ipset> --type=<ipset type> [--option=<key>[=<value>]]..
  54. Add a new ipset [P only]
  55. --new-ipset-from-file=<filename> [--name=<ipset>]
  56. Add a new ipset from file with optional name [P only]
  57. --delete-ipset=<ipset>
  58. Delete an existing ipset [P only]
  59. --load-ipset-defaults=<ipset>
  60. Load ipset default settings [P only]
  61. --info-ipset=<ipset> Print information about an ipset
  62. --path-ipset=<ipset> Print file path of an ipset [P only]
  63. --get-ipsets Print predefined ipsets
  64. --ipset=<ipset> --set-description=<description>
  65. Set new description to ipset [P only]
  66. --ipset=<ipset> --get-description
  67. Print description for ipset [P only]
  68. --ipset=<ipset> --set-short=<description>
  69. Set new short description to ipset [P only]
  70. --ipset=<ipset> --get-short
  71. Print short description for ipset [P only]
  72. --ipset=<ipset> --add-entry=<entry>
  73. Add a new entry to an ipset [P]
  74. --ipset=<ipset> --remove-entry=<entry>
  75. Remove an entry from an ipset [P]
  76. --ipset=<ipset> --query-entry=<entry>
  77. Return whether ipset has an entry [P]
  78. --ipset=<ipset> --get-entries
  79. List entries of an ipset [P]
  80. --ipset=<ipset> --add-entries-from-file=<entry>
  81. Add a new entries to an ipset [P]
  82. --ipset=<ipset> --remove-entries-from-file=<entry>
  83. Remove entries from an ipset [P]
  84. IcmpType Options
  85. --new-icmptype=<icmptype>
  86. Add a new icmptype [P only]
  87. --new-icmptype-from-file=<filename> [--name=<icmptype>]
  88. Add a new icmptype from file with optional name [P only]
  89. --delete-icmptype=<icmptype>
  90. Delete an existing icmptype [P only]
  91. --load-icmptype-defaults=<icmptype>
  92. Load icmptype default settings [P only]
  93. --info-icmptype=<icmptype>
  94. Print information about an icmptype
  95. --path-icmptype=<icmptype>
  96. Print file path of an icmptype [P only]
  97. --icmptype=<icmptype> --set-description=<description>
  98. Set new description to icmptype [P only]
  99. --icmptype=<icmptype> --get-description
  100. Print description for icmptype [P only]
  101. --icmptype=<icmptype> --set-short=<description>
  102. Set new short description to icmptype [P only]
  103. --icmptype=<icmptype> --get-short
  104. Print short description for icmptype [P only]
  105. --icmptype=<icmptype> --add-destination=<ipv>
  106. Enable destination for ipv in icmptype [P only]
  107. --icmptype=<icmptype> --remove-destination=<ipv>
  108. Disable destination for ipv in icmptype [P only]
  109. --icmptype=<icmptype> --query-destination=<ipv>
  110. Return whether destination ipv is enabled in icmptype [P only]
  111. --icmptype=<icmptype> --get-destinations
  112. List destinations in icmptype [P only]
  113. Service Options
  114. --new-service=<service>
  115. Add a new service [P only]
  116. --new-service-from-file=<filename> [--name=<service>]
  117. Add a new service from file with optional name [P only]
  118. --delete-service=<service>
  119. Delete an existing service [P only]
  120. --load-service-defaults=<service>
  121. Load icmptype default settings [P only]
  122. --info-service=<service>
  123. Print information about a service
  124. --path-service=<service>
  125. Print file path of a service [P only]
  126. --service=<service> --set-description=<description>
  127. Set new description to service [P only]
  128. --service=<service> --get-description
  129. Print description for service [P only]
  130. --service=<service> --set-short=<description>
  131. Set new short description to service [P only]
  132. --service=<service> --get-short
  133. Print short description for service [P only]
  134. --service=<service> --add-port=<portid>[-<portid>]/<protocol>
  135. Add a new port to service [P only]
  136. --service=<service> --remove-port=<portid>[-<portid>]/<protocol>
  137. Remove a port from service [P only]
  138. --service=<service> --query-port=<portid>[-<portid>]/<protocol>
  139. Return whether the port has been added for service [P only]
  140. --service=<service> --get-ports
  141. List ports of service [P only]
  142. --service=<service> --add-protocol=<protocol>
  143. Add a new protocol to service [P only]
  144. --service=<service> --remove-protocol=<protocol>
  145. Remove a protocol from service [P only]
  146. --service=<service> --query-protocol=<protocol>
  147. Return whether the protocol has been added for service [P only]
  148. --service=<service> --get-protocols
  149. List protocols of service [P only]
  150. --service=<service> --add-source-port=<portid>[-<portid>]/<protocol>
  151. Add a new source port to service [P only]
  152. --service=<service> --remove-source-port=<portid>[-<portid>]/<protocol>
  153. Remove a source port from service [P only]
  154. --service=<service> --query-source-port=<portid>[-<portid>]/<protocol>
  155. Return whether the source port has been added for service [P only]
  156. --service=<service> --get-source-ports
  157. List source ports of service [P only]
  158. --service=<service> --add-module=<module>
  159. Add a new module to service [P only]
  160. --service=<service> --remove-module=<module>
  161. Remove a module from service [P only]
  162. --service=<service> --query-module=<module>
  163. Return whether the module has been added for service [P only]
  164. --service=<service> --get-modules
  165. List modules of service [P only]
  166. --service=<service> --set-destination=<ipv>:<address>[/<mask>]
  167. Set destination for ipv to address in service [P only]
  168. --service=<service> --remove-destination=<ipv>
  169. Disable destination for ipv i service [P only]
  170. --service=<service> --query-destination=<ipv>:<address>[/<mask>]
  171. Return whether destination ipv is set for service [P only]
  172. --service=<service> --get-destinations
  173. List destinations in service [P only]
  174. Options to Adapt and Query Zones
  175. --list-all List everything added for or enabled in a zone [P] [Z]
  176. --list-services List services added for a zone [P] [Z]
  177. --timeout=<timeval> Enable an option for timeval time, where timeval is
  178. a number followed by one of letters 's' or 'm' or 'h'
  179. Usable for options marked with [T]
  180. --set-description=<description>
  181. Set new description to zone [P only] [Z]
  182. --get-description Print description for zone [P only] [Z]
  183. --set-short=<description>
  184. Set new short description to zone [P only] [Z]
  185. --get-short Print short description for zone [P only] [Z]
  186. --add-service=<service>
  187. Add a service for a zone [P] [Z] [T]
  188. --remove-service=<service>
  189. Remove a service from a zone [P] [Z]
  190. --query-service=<service>
  191. Return whether service has been added for a zone [P] [Z]
  192. --list-ports List ports added for a zone [P] [Z]
  193. --add-port=<portid>[-<portid>]/<protocol>
  194. Add the port for a zone [P] [Z] [T]
  195. --remove-port=<portid>[-<portid>]/<protocol>
  196. Remove the port from a zone [P] [Z]
  197. --query-port=<portid>[-<portid>]/<protocol>
  198. Return whether the port has been added for zone [P] [Z]
  199. --list-protocols List protocols added for a zone [P] [Z]
  200. --add-protocol=<protocol>
  201. Add the protocol for a zone [P] [Z] [T]
  202. --remove-protocol=<protocol>
  203. Remove the protocol from a zone [P] [Z]
  204. --query-protocol=<protocol>
  205. Return whether the protocol has been added for zone [P] [Z]
  206. --list-source-ports List source ports added for a zone [P] [Z]
  207. --add-source-port=<portid>[-<portid>]/<protocol>
  208. Add the source port for a zone [P] [Z] [T]
  209. --remove-source-port=<portid>[-<portid>]/<protocol>
  210. Remove the source port from a zone [P] [Z]
  211. --query-source-port=<portid>[-<portid>]/<protocol>
  212. Return whether the source port has been added for zone [P] [Z]
  213. --list-icmp-blocks List Internet ICMP type blocks added for a zone [P] [Z]
  214. --add-icmp-block=<icmptype>
  215. Add an ICMP block for a zone [P] [Z] [T]
  216. --remove-icmp-block=<icmptype>
  217. Remove the ICMP block from a zone [P] [Z]
  218. --query-icmp-block=<icmptype>
  219. Return whether an ICMP block has been added for a zone
  220. [P] [Z]
  221. --add-icmp-block-inversion
  222. Enable inversion of icmp blocks for a zone [P] [Z]
  223. --remove-icmp-block-inversion
  224. Disable inversion of icmp blocks for a zone [P] [Z]
  225. --query-icmp-block-inversion
  226. Return whether inversion of icmp blocks has been enabled
  227. for a zone [P] [Z]
  228. --list-forward-ports List IPv4 forward ports added for a zone [P] [Z]
  229. --add-forward-port=port=<portid>[-<portid>]:proto=<protocol>[:toport=<portid>[-<portid>]][:toaddr=<address>[/<mask>]]
  230. Add the IPv4 forward port for a zone [P] [Z] [T]
  231. --remove-forward-port=port=<portid>[-<portid>]:proto=<protocol>[:toport=<portid>[-<portid>]][:toaddr=<address>[/<mask>]]
  232. Remove the IPv4 forward port from a zone [P] [Z]
  233. --query-forward-port=port=<portid>[-<portid>]:proto=<protocol>[:toport=<portid>[-<portid>]][:toaddr=<address>[/<mask>]]
  234. Return whether the IPv4 forward port has been added for
  235. a zone [P] [Z]
  236. --add-masquerade Enable IPv4 masquerade for a zone [P] [Z] [T]
  237. --remove-masquerade Disable IPv4 masquerade for a zone [P] [Z]
  238. --query-masquerade Return whether IPv4 masquerading has been enabled for a
  239. zone [P] [Z]
  240. --list-rich-rules List rich language rules added for a zone [P] [Z]
  241. --add-rich-rule=<rule>
  242. Add rich language rule 'rule' for a zone [P] [Z] [T]
  243. --remove-rich-rule=<rule>
  244. Remove rich language rule 'rule' from a zone [P] [Z]
  245. --query-rich-rule=<rule>
  246. Return whether a rich language rule 'rule' has been
  247. added for a zone [P] [Z]
  248. Options to Handle Bindings of Interfaces
  249. --list-interfaces List interfaces that are bound to a zone [P] [Z]
  250. --add-interface=<interface>
  251. Bind the <interface> to a zone [P] [Z]
  252. --change-interface=<interface>
  253. Change zone the <interface> is bound to [P] [Z]
  254. --query-interface=<interface>
  255. Query whether <interface> is bound to a zone [P] [Z]
  256. --remove-interface=<interface>
  257. Remove binding of <interface> from a zone [P] [Z]
  258. Options to Handle Bindings of Sources
  259. --list-sources List sources that are bound to a zone [P] [Z]
  260. --add-source=<source>[/<mask>]|<MAC>|ipset:<ipset>
  261. Bind the source to a zone [P] [Z]
  262. --change-source=<source>[/<mask>]|<MAC>|ipset:<ipset>
  263. Change zone the source is bound to [Z]
  264. --query-source=<source>[/<mask>]|<MAC>|ipset:<ipset>
  265. Query whether the source is bound to a zone [P] [Z]
  266. --remove-source=<source>[/<mask>]|<MAC>|ipset:<ipset>
  267. Remove binding of the source from a zone [P] [Z]
  268. Helper Options
  269. --new-helper=<helper> --module=<module> [--family=<family>]
  270. Add a new helper [P only]
  271. --new-helper-from-file=<filename> [--name=<helper>]
  272. Add a new helper from file with optional name [P only]
  273. --delete-helper=<helper>
  274. Delete an existing helper [P only]
  275. --load-helper-defaults=<helper>
  276. Load helper default settings [P only]
  277. --info-helper=<helper> Print information about an helper
  278. --path-helper=<helper> Print file path of an helper [P only]
  279. --get-helpers Print predefined helpers
  280. --helper=<helper> --set-description=<description>
  281. Set new description to helper [P only]
  282. --helper=<helper> --get-description
  283. Print description for helper [P only]
  284. --helper=<helper> --set-short=<description>
  285. Set new short description to helper [P only]
  286. --helper=<helper> --get-short
  287. Print short description for helper [P only]
  288. --helper=<helper> --add-port=<portid>[-<portid>]/<protocol>
  289. Add a new port to helper [P only]
  290. --helper=<helper> --remove-port=<portid>[-<portid>]/<protocol>
  291. Remove a port from helper [P only]
  292. --helper=<helper> --query-port=<portid>[-<portid>]/<protocol>
  293. Return whether the port has been added for helper [P only]
  294. --helper=<helper> --get-ports
  295. List ports of helper [P only]
  296. --helper=<helper> --set-module=<module>
  297. Set module to helper [P only]
  298. --helper=<helper> --get-module
  299. Get module from helper [P only]
  300. --helper=<helper> --set-family={ipv4|ipv6|}
  301. Set family for helper [P only]
  302. --helper=<helper> --get-family
  303. Get module from helper [P only]
  304. Direct Options
  305. --direct First option for all direct options
  306. --get-all-chains
  307. Get all chains [P]
  308. --get-chains {ipv4|ipv6|eb} <table>
  309. Get all chains added to the table [P]
  310. --add-chain {ipv4|ipv6|eb} <table> <chain>
  311. Add a new chain to the table [P]
  312. --remove-chain {ipv4|ipv6|eb} <table> <chain>
  313. Remove the chain from the table [P]
  314. --query-chain {ipv4|ipv6|eb} <table> <chain>
  315. Return whether the chain has been added to the table [P]
  316. --get-all-rules
  317. Get all rules [P]
  318. --get-rules {ipv4|ipv6|eb} <table> <chain>
  319. Get all rules added to chain in table [P]
  320. --add-rule {ipv4|ipv6|eb} <table> <chain> <priority> <arg>...
  321. Add rule to chain in table [P]
  322. --remove-rule {ipv4|ipv6|eb} <table> <chain> <priority> <arg>...
  323. Remove rule with priority from chain in table [P]
  324. --remove-rules {ipv4|ipv6|eb} <table> <chain>
  325. Remove rules from chain in table [P]
  326. --query-rule {ipv4|ipv6|eb} <table> <chain> <priority> <arg>...
  327. Return whether a rule with priority has been added to
  328. chain in table [P]
  329. --passthrough {ipv4|ipv6|eb} <arg>...
  330. Pass a command through (untracked by firewalld)
  331. --get-all-passthroughs
  332. Get all tracked passthrough rules [P]
  333. --get-passthroughs {ipv4|ipv6|eb} <arg>...
  334. Get tracked passthrough rules [P]
  335. --add-passthrough {ipv4|ipv6|eb} <arg>...
  336. Add a new tracked passthrough rule [P]
  337. --remove-passthrough {ipv4|ipv6|eb} <arg>...
  338. Remove a tracked passthrough rule [P]
  339. --query-passthrough {ipv4|ipv6|eb} <arg>...
  340. Return whether the tracked passthrough rule has been
  341. added [P]
  342. Lockdown Options
  343. --lockdown-on Enable lockdown.
  344. --lockdown-off Disable lockdown.
  345. --query-lockdown Query whether lockdown is enabled
  346. Lockdown Whitelist Options
  347. --list-lockdown-whitelist-commands
  348. List all command lines that are on the whitelist [P]
  349. --add-lockdown-whitelist-command=<command>
  350. Add the command to the whitelist [P]
  351. --remove-lockdown-whitelist-command=<command>
  352. Remove the command from the whitelist [P]
  353. --query-lockdown-whitelist-command=<command>
  354. Query whether the command is on the whitelist [P]
  355. --list-lockdown-whitelist-contexts
  356. List all contexts that are on the whitelist [P]
  357. --add-lockdown-whitelist-context=<context>
  358. Add the context context to the whitelist [P]
  359. --remove-lockdown-whitelist-context=<context>
  360. Remove the context from the whitelist [P]
  361. --query-lockdown-whitelist-context=<context>
  362. Query whether the context is on the whitelist [P]
  363. --list-lockdown-whitelist-uids
  364. List all user ids that are on the whitelist [P]
  365. --add-lockdown-whitelist-uid=<uid>
  366. Add the user id uid to the whitelist [P]
  367. --remove-lockdown-whitelist-uid=<uid>
  368. Remove the user id uid from the whitelist [P]
  369. --query-lockdown-whitelist-uid=<uid>
  370. Query whether the user id uid is on the whitelist [P]
  371. --list-lockdown-whitelist-users
  372. List all user names that are on the whitelist [P]
  373. --add-lockdown-whitelist-user=<user>
  374. Add the user name user to the whitelist [P]
  375. --remove-lockdown-whitelist-user=<user>
  376. Remove the user name user from the whitelist [P]
  377. --query-lockdown-whitelist-user=<user>
  378. Query whether the user name user is on the whitelist [P]
  379. Panic Options
  380. --panic-on Enable panic mode
  381. --panic-off Disable panic mode
  382. --query-panic Query whether panic mode is enabled

以上,感谢。

2022年11月27日

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/701954
推荐阅读
相关标签
  

闽ICP备14008679号