UFW : 基本操作2023/07/13 |
|
UFW (Uncomplicated FireWall) の基本操作です。
|
|
| [1] | UFW をインストールします。 |
|
root@dlp:~# apt -y install ufw |
| [2] | UFW は nftables/iptables を設定するためのフロントエンドツールです。 Debian 12 では、UFW のデフォルトバックエンドは nftables となっています。 |
|
root@dlp:~# update-alternatives --config iptables There are 2 choices for the alternative iptables (providing /usr/sbin/iptables). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/sbin/iptables-nft 20 auto mode 1 /usr/sbin/iptables-legacy 10 manual mode 2 /usr/sbin/iptables-nft 20 manual mode Press <enter> to keep the current choice[*], or type selection number: |
| [3] | UFW を利用するには、サービスを起動しておく必要があります。 さらに、サービスは起動していても、デフォルトでは無効化されているため、機能を利用するには有効化する必要があります。 |
|
root@dlp:~# systemctl enable --now ufw Synchronizing state of ufw.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable ufwroot@dlp:~# systemctl status ufw
* ufw.service - Uncomplicated firewall
Loaded: loaded (/lib/systemd/system/ufw.service; enabled; preset: enabled)
Active: active (exited) since Wed 2023-07-12 18:50:17 CDT; 1min 23s ago
Docs: man:ufw(8)
Process: 1254 ExecStart=/lib/ufw/ufw-init start quiet (code=exited, status=>
Main PID: 1254 (code=exited, status=0/SUCCESS)
CPU: 775us
# 現在の状態 root@dlp:~# ufw status Status: inactive # 有効化 root@dlp:~# ufw enable Firewall is active and enabled on system startup
root@dlp:~#
ufw status Status: active # 無効化する場合は以下 root@dlp:~# ufw disable Firewall stopped and disabled on system startup |
| [4] | UFW で サービス/ポート を許可する場合の基本操作です。 |
|
# デフォルトはインカミング通信全て拒否 root@dlp:~# ufw status verbose Status: active Logging: on (low) Default: deny (incoming), allow (outgoing), disabled (routed) New profiles: skip # 例として SSH を許可 root@dlp:~# ufw allow ssh Rule added Rule added (v6) # 例として HTTP を許可 root@dlp:~# ufw allow http Rule added Rule added (v6) # 例として 2049/tcp を許可 root@dlp:~# ufw allow 2049/tcp Rule added Rule added (v6)root@dlp:~# ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip
To Action From
-- ------ ----
22/tcp ALLOW IN Anywhere
80/tcp ALLOW IN Anywhere
2049/tcp ALLOW IN Anywhere
22/tcp (v6) ALLOW IN Anywhere (v6)
80/tcp (v6) ALLOW IN Anywhere (v6)
2049/tcp (v6) ALLOW IN Anywhere (v6)
# * [ufw allow (service name)] では [/etc/services] で定義されたポートが許可される
|
| [5] | 追加したルールを削除する場合の基本操作です。 |
|
root@dlp:~# ufw status verbose Status: active Logging: on (low) Default: deny (incoming), allow (outgoing), disabled (routed) New profiles: skip To Action From -- ------ ---- 22/tcp ALLOW IN Anywhere 80/tcp ALLOW IN Anywhere 2049/tcp ALLOW IN Anywhere 22/tcp (v6) ALLOW IN Anywhere (v6) 80/tcp (v6) ALLOW IN Anywhere (v6) 2049/tcp (v6) ALLOW IN Anywhere (v6) # 例として SSH を削除 root@dlp:~# ufw delete allow ssh Rule deleted Rule deleted (v6) # 例として 80/tcp を削除 root@dlp:~# ufw delete allow 80/tcp Rule deleted Rule deleted (v6) # ルール番号付きで状態を表示 root@dlp:~# ufw status numbered
Status: active
To Action From
-- ------ ----
[ 1] 2049/tcp ALLOW IN Anywhere
[ 2] 2049/tcp (v6) ALLOW IN Anywhere (v6)
# ルール番号を指定してルールを削除 root@dlp:~# ufw delete 2
Deleting:
allow 2049/tcp
Proceed with operation (y|n)? y
Rule deleted (v6)
# ルールを全て削除して初期化し、UFW を無効化する場合は以下 root@dlp:~# ufw reset
Resetting all rules to installed defaults. Proceed with operation (y|n)? y
Backing up 'user.rules' to '/etc/ufw/user.rules.20230712_185512'
Backing up 'before.rules' to '/etc/ufw/before.rules.20230712_185512'
Backing up 'after.rules' to '/etc/ufw/after.rules.20230712_185512'
Backing up 'user6.rules' to '/etc/ufw/user6.rules.20230712_185512'
Backing up 'before6.rules' to '/etc/ufw/before6.rules.20230712_185512'
Backing up 'after6.rules' to '/etc/ufw/after6.rules.20230712_185512'
root@dlp:~# ufw status Status: inactive |
| [6] | 接続回数の制限をかけたり、送信元や送信先を指定してサービスの許可をする場合の基本操作です。 |
|
# 例として [10.0.0.215] からの SSH を許可 root@dlp:~# ufw allow from 10.0.0.215 to any port ssh Rule added # 例として [10.0.0.215] から [10.0.0.30] への [80/tcp] を許可 root@dlp:~# ufw allow from 10.0.0.215 to 10.0.0.30 port 80 proto tcp Rule added # 例として [10.0.0.220] からの SSH を制限 # * 30 秒間に 6 回以上の連続 SSH は拒否される root@dlp:~# ufw limit from 10.0.0.220 to any port ssh Rule added ufw status verbose Status: active Logging: on (low) Default: deny (incoming), allow (outgoing), disabled (routed) New profiles: skip To Action From -- ------ ---- 22/tcp ALLOW IN 10.0.0.215 10.0.0.30 80/tcp ALLOW IN 10.0.0.215 22/tcp LIMIT IN 10.0.0.220 # limit を設定した場合は以下のようなルールセットが登録される root@dlp:~# nft list ruleset | grep 'dport 22'
# Warning: table ip filter is managed by iptables-nft, do not touch!
ip saddr 10.0.0.215 tcp dport 22 counter packets 0 bytes 0 accept
# Warning: table ip6 filter is managed by iptables-nft, do not touch!
ip saddr 10.0.0.220 tcp dport 22 ct state new xt match recent counter packets 0 bytes 0
ip saddr 10.0.0.220 tcp dport 22 ct state new xt match recent counter packets 0 bytes 0 jump ufw-user-limit
ip saddr 10.0.0.220 tcp dport 22 counter packets 0 bytes 0 jump ufw-user-limit-accept
|
| [7] | ICMP の 許可/拒否 設定を調整したい場合は、設定ファイルを編集します。 UFW デフォルトはインカミング通信全て拒否ですが、ICMP 関連はデフォルトで許可されています。 |
|
root@dlp:~#
vi /etc/ufw/before.rules # 以下の設定により ICMP 関連は許可されている # 拒否したい場合は下行を全てコメントにすれば OK # ok icmp codes for INPUT # -A ufw-before-input -p icmp --icmp-type destination-unreachable -j ACCEPT # -A ufw-before-input -p icmp --icmp-type time-exceeded -j ACCEPT # -A ufw-before-input -p icmp --icmp-type parameter-problem -j ACCEPT # -A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT # ok icmp code for FORWARD # -A ufw-before-forward -p icmp --icmp-type destination-unreachable -j ACCEPT # -A ufw-before-forward -p icmp --icmp-type time-exceeded -j ACCEPT # -A ufw-before-forward -p icmp --icmp-type parameter-problem -j ACCEPT # -A ufw-before-forward -p icmp --icmp-type echo-request -j ACCEPT # 設定内容をリロード root@dlp:~# ufw reload Firewall reloaded # 特定の IP アドレス/ネットワーク からの [echo-request] を許可する場合は以下 # * 許可ホストからの Ping に応答する
root@dlp:~#
vi /etc/ufw/before.rules # 例として [10.0.0.0/24] からの [echo-request] は許可 # ok icmp codes for INPUT # -A ufw-before-input -p icmp --icmp-type destination-unreachable -j ACCEPT # -A ufw-before-input -p icmp --icmp-type time-exceeded -j ACCEPT # -A ufw-before-input -p icmp --icmp-type parameter-problem -j ACCEPT # -A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT -A ufw-before-input -p icmp --icmp-type echo-request -s 10.0.0.0/24 -j ACCEPT # ok icmp code for FORWARD # -A ufw-before-forward -p icmp --icmp-type destination-unreachable -j ACCEPT # -A ufw-before-forward -p icmp --icmp-type time-exceeded -j ACCEPT # -A ufw-before-forward -p icmp --icmp-type parameter-problem -j ACCEPT # -A ufw-before-forward -p icmp --icmp-type echo-request -j ACCEPTroot@dlp:~# ufw reload Firewall reloaded |
| Sponsored Link |
|
|