CentOS7のファイアウォール構成

CentOS7のファイアウォール構成

1.ファイアウォールの基本的な使い方

1.ファイアウォールを開始します

[root@localhost ~]# systemctl start firewalld.service

2.ファイアウォールをオフにする

[root@localhost ~]# systemctl stop firewalld.service

3.ファイアウォールを再起動します

[root@localhost ~]# systemctl restart firewalld.service

4.ファイアウォールをリロードします

[root@localhost ~]# systemctl reload firewalld.service

5. firewalldサービスのステータスを表示する

[root@localhost ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2020-08-07 04:48:14 EDT; 1min 19s ago
     Docs: man:firewalld(1)
  Process: 1860 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS)
 Main PID: 1764 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─1764 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

Aug 07 04:48:13 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
Aug 07 04:48:14 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
Aug 07 04:48:14 localhost.localdomain firewalld[1764]: WARNING: AllowZoneDrifting is enabled. This is considered an inse... now.
Aug 07 04:48:48 localhost.localdomain systemd[1]: Reloading firewalld - dynamic firewall daemon.
Aug 07 04:48:48 localhost.localdomain systemd[1]: Reloaded firewalld - dynamic firewall daemon.
Aug 07 04:48:48 localhost.localdomain firewalld[1764]: WARNING: AllowZoneDrifting is enabled. This is considered an inse... now.
Hint: Some lines were ellipsized, use -l to show in full.

6.起動時にファイアウォールを無効にする

[root@localhost ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.

7.起動時にファイアウォールを有効にする

[root@localhost ~]# systemctl enable firewalld.service
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.

2、firewall-cmdを構成する

1.ヘルプを見る

[root@localhost ~]# firewall-cmd --help

Usage: firewall-cmd [OPTIONS...]

General Options
  -h, --help           Prints a short help text and exists
  -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
  --check-config       Check permanent configuration for errors
............

2.バージョンを確認する

[root@localhost ~]# firewall-cmd --version
0.6.3

3.ステータスを表示する

[root@localhost ~]# firewall-cmd --state
running

4.開いているすべてのポートを表示する

[root@localhost ~]# firewall-cmd --list-port
3306/tcp

5.ファイアウォールルールを更新する

[root@localhost ~]# firewall-cmd --reload
success

6.ファイアウォールルールを表示する

[root@localhost ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: dhcpv6-client ssh
  ports: 3306/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

7.ポート3306を開く

[root@localhost ~]# firewall-cmd --permanent --add-port=3306/tcp
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: dhcpv6-client ssh
  ports: 8080/tcp 3306/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

説明:
(1)–permanent:設定が永続的であることを意味します;
(2)reload:ファイアウォールをリロードします。

8.ポート3306を削除します

[root@localhost ~]# firewall-cmd --permanent --remove-port 3306/tcp
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: dhcpv6-client ssh
  ports: 8080/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

説明:
(1)–permanent:設定が永続的であることを意味します;
(2)reload:ファイアウォールをリロードします。

おすすめ

転載: blog.csdn.net/weixin_44377973/article/details/107867351