• Linux防火墙和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=
    16. Set log denied value
    17. Automatic Helpers Options
    18. --get-automatic-helpers
    19. Print the automatic helpers value
    20. --set-automatic-helpers=
    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=
    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=
    34. Print name of the zone the interface is bound to [P]
    35. --get-zone-of-source=<source>[/]||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= Add a new zone [P only]
    39. --new-zone-from-file= [--name=]
    40. Add a new zone from file with optional name [P only]
    41. --delete-zone= Delete an existing zone [P only]
    42. --load-zone-defaults=
    43. Load zone default settings [P only] [Z]
    44. --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=
    48. Set the zone target [P only] [Z]
    49. --info-zone= Print information about a zone
    50. --path-zone= Print file path of a zone [P only]
    51. IPSet Options
    52. --get-ipset-types Print the supported ipset types
    53. --new-ipset= --type=type> [--option=[=]]..
    54. Add a new ipset [P only]
    55. --new-ipset-from-file= [--name=]
    56. Add a new ipset from file with optional name [P only]
    57. --delete-ipset=
    58. Delete an existing ipset [P only]
    59. --load-ipset-defaults=
    60. Load ipset default settings [P only]
    61. --info-ipset= Print information about an ipset
    62. --path-ipset= Print file path of an ipset [P only]
    63. --get-ipsets Print predefined ipsets
    64. --ipset= --set-description=
    65. Set new description to ipset [P only]
    66. --ipset= --get-description
    67. Print description for ipset [P only]
    68. --ipset= --set-short=
    69. Set new short description to ipset [P only]
    70. --ipset= --get-short
    71. Print short description for ipset [P only]
    72. --ipset= --add-entry=
    73. Add a new entry to an ipset [P]
    74. --ipset= --remove-entry=
    75. Remove an entry from an ipset [P]
    76. --ipset= --query-entry=
    77. Return whether ipset has an entry [P]
    78. --ipset= --get-entries
    79. List entries of an ipset [P]
    80. --ipset= --add-entries-from-file=
    81. Add a new entries to an ipset [P]
    82. --ipset= --remove-entries-from-file=
    83. Remove entries from an ipset [P]
    84. IcmpType Options
    85. --new-icmptype=
    86. Add a new icmptype [P only]
    87. --new-icmptype-from-file= [--name=]
    88. Add a new icmptype from file with optional name [P only]
    89. --delete-icmptype=
    90. Delete an existing icmptype [P only]
    91. --load-icmptype-defaults=
    92. Load icmptype default settings [P only]
    93. --info-icmptype=
    94. Print information about an icmptype
    95. --path-icmptype=
    96. Print file path of an icmptype [P only]
    97. --icmptype= --set-description=
    98. Set new description to icmptype [P only]
    99. --icmptype= --get-description
    100. Print description for icmptype [P only]
    101. --icmptype= --set-short=
    102. Set new short description to icmptype [P only]
    103. --icmptype= --get-short
    104. Print short description for icmptype [P only]
    105. --icmptype= --add-destination=
    106. Enable destination for ipv in icmptype [P only]
    107. --icmptype= --remove-destination=
    108. Disable destination for ipv in icmptype [P only]
    109. --icmptype= --query-destination=
    110. Return whether destination ipv is enabled in icmptype [P only]
    111. --icmptype= --get-destinations
    112. List destinations in icmptype [P only]
    113. Service Options
    114. --new-service=
    115. Add a new service [P only]
    116. --new-service-from-file= [--name=]
    117. Add a new service from file with optional name [P only]
    118. --delete-service=
    119. Delete an existing service [P only]
    120. --load-service-defaults=
    121. Load icmptype default settings [P only]
    122. --info-service=
    123. Print information about a service
    124. --path-service=
    125. Print file path of a service [P only]
    126. --service= --set-description=
    127. Set new description to service [P only]
    128. --service= --get-description
    129. Print description for service [P only]
    130. --service= --set-short=
    131. Set new short description to service [P only]
    132. --service= --get-short
    133. Print short description for service [P only]
    134. --service= --add-port=[-]/
    135. Add a new port to service [P only]
    136. --service= --remove-port=[-]/
    137. Remove a port from service [P only]
    138. --service= --query-port=[-]/
    139. Return whether the port has been added for service [P only]
    140. --service= --get-ports
    141. List ports of service [P only]
    142. --service= --add-protocol=
    143. Add a new protocol to service [P only]
    144. --service= --remove-protocol=
    145. Remove a protocol from service [P only]
    146. --service= --query-protocol=
    147. Return whether the protocol has been added for service [P only]
    148. --service= --get-protocols
    149. List protocols of service [P only]
    150. --service= --add-source-port=[-]/
    151. Add a new source port to service [P only]
    152. --service= --remove-source-port=[-]/
    153. Remove a source port from service [P only]
    154. --service= --query-source-port=[-]/
    155. Return whether the source port has been added for service [P only]
    156. --service= --get-source-ports
    157. List source ports of service [P only]
    158. --service= --add-module=
    159. Add a new module to service [P only]
    160. --service= --remove-module=
    161. Remove a module from service [P only]
    162. --service= --query-module=
    163. Return whether the module has been added for service [P only]
    164. --service= --get-modules
    165. List modules of service [P only]
    166. --service= --set-destination=:
      [/]
    167. Set destination for ipv to address in service [P only]
    168. --service= --remove-destination=
    169. Disable destination for ipv i service [P only]
    170. --service= --query-destination=:
      [/]
    171. Return whether destination ipv is set for service [P only]
    172. --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= 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=
    181. Set new description to zone [P only] [Z]
    182. --get-description Print description for zone [P only] [Z]
    183. --set-short=
    184. Set new short description to zone [P only] [Z]
    185. --get-short Print short description for zone [P only] [Z]
    186. --add-service=
    187. Add a service for a zone [P] [Z] [T]
    188. --remove-service=
    189. Remove a service from a zone [P] [Z]
    190. --query-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=[-]/
    194. Add the port for a zone [P] [Z] [T]
    195. --remove-port=[-]/
    196. Remove the port from a zone [P] [Z]
    197. --query-port=[-]/
    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=
    201. Add the protocol for a zone [P] [Z] [T]
    202. --remove-protocol=
    203. Remove the protocol from a zone [P] [Z]
    204. --query-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=[-]/
    208. Add the source port for a zone [P] [Z] [T]
    209. --remove-source-port=[-]/
    210. Remove the source port from a zone [P] [Z]
    211. --query-source-port=[-]/
    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=
    215. Add an ICMP block for a zone [P] [Z] [T]
    216. --remove-icmp-block=
    217. Remove the ICMP block from a zone [P] [Z]
    218. --query-icmp-block=
    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=[-]:proto=[:toport=[-]][:toaddr=
      [/]]
    230. Add the IPv4 forward port for a zone [P] [Z] [T]
    231. --remove-forward-port=port=[-]:proto=[:toport=[-]][:toaddr=
      [/]]
    232. Remove the IPv4 forward port from a zone [P] [Z]
    233. --query-forward-port=port=[-]:proto=[:toport=[-]][:toaddr=
      [/]]
    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=
    242. Add rich language rule 'rule' for a zone [P] [Z] [T]
    243. --remove-rich-rule=
    244. Remove rich language rule 'rule' from a zone [P] [Z]
    245. --query-rich-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=
    251. Bind the to a zone [P] [Z]
    252. --change-interface=
    253. Change zone the is bound to [P] [Z]
    254. --query-interface=
    255. Query whether is bound to a zone [P] [Z]
    256. --remove-interface=
    257. Remove binding of 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>[/]||ipset:
    261. Bind the source to a zone [P] [Z]
    262. --change-source=<source>[/]||ipset:
    263. Change zone the source is bound to [Z]
    264. --query-source=<source>[/]||ipset:
    265. Query whether the source is bound to a zone [P] [Z]
    266. --remove-source=<source>[/]||ipset:
    267. Remove binding of the source from a zone [P] [Z]
    268. Helper Options
    269. --new-helper= --module= [--family=]
    270. Add a new helper [P only]
    271. --new-helper-from-file= [--name=]
    272. Add a new helper from file with optional name [P only]
    273. --delete-helper=
    274. Delete an existing helper [P only]
    275. --load-helper-defaults=
    276. Load helper default settings [P only]
    277. --info-helper= Print information about an helper
    278. --path-helper= Print file path of an helper [P only]
    279. --get-helpers Print predefined helpers
    280. --helper= --set-description=
    281. Set new description to helper [P only]
    282. --helper= --get-description
    283. Print description for helper [P only]
    284. --helper= --set-short=
    285. Set new short description to helper [P only]
    286. --helper= --get-short
    287. Print short description for helper [P only]
    288. --helper= --add-port=[-]/
    289. Add a new port to helper [P only]
    290. --helper= --remove-port=[-]/
    291. Remove a port from helper [P only]
    292. --helper= --query-port=[-]/
    293. Return whether the port has been added for helper [P only]
    294. --helper= --get-ports
    295. List ports of helper [P only]
    296. --helper= --set-module=
    297. Set module to helper [P only]
    298. --helper= --get-module
    299. Get module from helper [P only]
    300. --helper= --set-family={ipv4|ipv6|}
    301. Set family for helper [P only]
    302. --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}
    309. Get all chains added to the table [P]
    310. --add-chain {ipv4|ipv6|eb}
    311. Add a new chain to the table [P]
    312. --remove-chain {ipv4|ipv6|eb}
    313. Remove the chain from the table [P]
    314. --query-chain {ipv4|ipv6|eb}
    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}
    319. Get all rules added to chain in table [P]
    320. --add-rule {ipv4|ipv6|eb}
    321. ...
    322. Add rule to chain in table [P]
    323. --remove-rule {ipv4|ipv6|eb}
    324. ...
    325. Remove rule with priority from chain in table [P]
    326. --remove-rules {ipv4|ipv6|eb}
    327. Remove rules from chain in table [P]
    328. --query-rule {ipv4|ipv6|eb}
    329. ...
    330. Return whether a rule with priority has been added to
    331. chain in table [P]
    332. --passthrough {ipv4|ipv6|eb} ...
    333. Pass a command through (untracked by firewalld)
    334. --get-all-passthroughs
    335. Get all tracked passthrough rules [P]
    336. --get-passthroughs {ipv4|ipv6|eb} ...
    337. Get tracked passthrough rules [P]
    338. --add-passthrough {ipv4|ipv6|eb} ...
    339. Add a new tracked passthrough rule [P]
    340. --remove-passthrough {ipv4|ipv6|eb} ...
    341. Remove a tracked passthrough rule [P]
    342. --query-passthrough {ipv4|ipv6|eb} ...
    343. Return whether the tracked passthrough rule has been
    344. added [P]
    345. Lockdown Options
    346. --lockdown-on Enable lockdown.
    347. --lockdown-off Disable lockdown.
    348. --query-lockdown Query whether lockdown is enabled
    349. Lockdown Whitelist Options
    350. --list-lockdown-whitelist-commands
    351. List all command lines that are on the whitelist [P]
    352. --add-lockdown-whitelist-command=<command>
    353. Add the command to the whitelist [P]
    354. --remove-lockdown-whitelist-command=<command>
    355. Remove the command from the whitelist [P]
    356. --query-lockdown-whitelist-command=<command>
    357. Query whether the command is on the whitelist [P]
    358. --list-lockdown-whitelist-contexts
    359. List all contexts that are on the whitelist [P]
    360. --add-lockdown-whitelist-context=
    361. Add the context context to the whitelist [P]
    362. --remove-lockdown-whitelist-context=
    363. Remove the context from the whitelist [P]
    364. --query-lockdown-whitelist-context=
    365. Query whether the context is on the whitelist [P]
    366. --list-lockdown-whitelist-uids
    367. List all user ids that are on the whitelist [P]
    368. --add-lockdown-whitelist-uid=
    369. Add the user id uid to the whitelist [P]
    370. --remove-lockdown-whitelist-uid=
    371. Remove the user id uid from the whitelist [P]
    372. --query-lockdown-whitelist-uid=
    373. Query whether the user id uid is on the whitelist [P]
    374. --list-lockdown-whitelist-users
    375. List all user names that are on the whitelist [P]
    376. --add-lockdown-whitelist-user=
    377. Add the user name user to the whitelist [P]
    378. --remove-lockdown-whitelist-user=
    379. Remove the user name user from the whitelist [P]
    380. --query-lockdown-whitelist-user=
    381. Query whether the user name user is on the whitelist [P]
    382. Panic Options
    383. --panic-on Enable panic mode
    384. --panic-off Disable panic mode
    385. --query-panic Query whether panic mode is enabled
    386. 以上,感谢。

      2022年11月27日

    387. 相关阅读:
      数据结构题型7-删除结点方式1
      计算机毕业设计(附源码)python智慧门诊综合管理系统
      Raw格式的图片理解、读取、转换、显示、对raw10应用和COLOR_BayerBG2RGB理解
      Dubbo3.0新特性
      C语言经典100例题(50)--#include 的应用练习
      【CSS】笔记1-基础选择器、样式引入
      以沙箱的方式运行容器:安全容器Kata Containers
      Linux常用命令——帮助命令
      为什么我抓不到baidu的数据包
      解决vs code终端无法执行命令的问题
    388. 原文地址:https://blog.csdn.net/zhangbeizhen18/article/details/128062730