投げ銭

★当サイトへの投げ銭(PayPal)★

LINK


(無償、寄付歓迎) logo
世界中で使われるISO標準オフィスソフト(MSオフィス互換)

★LibreOfficeの導入事例★
詳細について

人気の投稿(1ヶ月間)

Ad

Ad

投げ銭

★当サイトへの投げ銭(PayPal)★

2018年3月31日土曜日

【RASPBIAN STRETCH LITE March 2018】iptablesのルールをシステム再起動後でも自動的に反映できるようにする【Raspberry Pi 2】

RASPBIAN STRETCH LITE(March 2018) で、
iptablesや、ip6tablesを用いて設定したフィルタルールが、システムの再起動後にも自動的に反映されるように設定を行った。

CentOS6なら、serivce iptables save で保存でき、また再起動時の自動復元が可能だった。
RASPBIAN STRETCH LITE でも、保存とパッケージの追加だけで再起動時の復元ができた。
ただし、作法が異なった。

そのパッケージの導入後、
iptableや、ip6tablesコマンドで変更したルールは、iptables-saveや、ip6tables-saveコマンドを用いて、設定保存ファイルに出力できる。

設定保存ファイルの内容は、サービスによってシステム起動時に自動的に反映される。

以下で、パッケージの導入から、動作確認までを扱っている。



iptables-persistentの導入を行った。

○必要なパッケージを導入した。

インストール中に、TUIが現れてiptablesの設定とip6tablesの設定を保存するかどうかを聞かれる。
いずれも、<YES>で答えた。

root@raspberrypi:/home/pi# apt-get install iptables-persistent
Reading package lists... Done
Building dependency tree     
Reading state information... Done
The following additional packages will be installed:
  netfilter-persistent
The following NEW packages will be installed:
  iptables-persistent netfilter-persistent

0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 19.5 kB of archives.
After this operation, 79.9 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian stretch/main armhf netfilter-persistent all 1.0.4+nmu2 [8,792 B]
Get:2 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian stretch/main armhf iptables-persistent all 1.0.4+nmu2 [10.7 kB]
Fetched 19.5 kB in 1s (9,889 B/s)             
Preconfiguring packages ...
Selecting previously unselected package netfilter-persistent.
(Reading database ... 34547 files and directories currently installed.)
Preparing to unpack .../netfilter-persistent_1.0.4+nmu2_all.deb ...
Unpacking netfilter-persistent (1.0.4+nmu2) ...
Selecting previously unselected package iptables-persistent.
Preparing to unpack .../iptables-persistent_1.0.4+nmu2_all.deb ...
Unpacking iptables-persistent (1.0.4+nmu2) ...
Setting up netfilter-persistent (1.0.4+nmu2) ...
Created symlink /etc/systemd/system/multi-user.target.wants/netfilter-persistent.service → /lib/systemd/system/netfilter-persistent.service.
update-rc.d: warning: start and stop actions are no longer supported; falling back to defaults
Processing triggers for systemd (232-25+deb9u2) ...
Processing triggers for man-db (2.7.6.1-2) ...
Setting up iptables-persistent (1.0.4+nmu2) ...

▲途中で、iptablesで既に設定されているルールを保存するかどうか尋ねられるので、<YES>で答えた。
これによって、システムの再起動の際に自動的にこのルールがロードされる。
ここで保存されたルールは後でも変更ができる。
Configuring iptables-persistent
Current iptables rules can be saved to the configuration file /etc/iptables/rules.v4.
These rules will then be loaded automatically during system startup.
Rules are only saved automatically during package installation.
See the manual page of iptables-save(8) for instructions on keeping the rules file up-to-date.
Save current IPv4 rules?<Yes><No>
▲ip6tablesについても同様である。
Configuring iptables-persistent
Current iptables rules can be saved to the configuration file /etc/iptables/rules.v6.
These rules will then be loaded automatically during system startup.
Rules are only saved automatically during package installation.
See the manual page of ip6tables-save(8) for instructions on keeping the rules file up-to-date.
Save current IPv6 rules?<Yes><No>
以上で導入自体は完了した。



■システム再起動後にルールが復元されるか確かめた

○実際に保存されていることを確認した。

# cat /etc/iptables/rules.v6
# Generated by ip6tables-save v1.6.0 on Fri Mar 30 15:35:44 2018
*filter
:INPUT DROP [8:736]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [18:1474]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -d fe80::/64 -p ipv6-icmp -m icmp6 --icmpv6-type 135 -j ACCEPT
-A INPUT -d fe80::/64 -p ipv6-icmp -m icmp6 --icmpv6-type 136 -j ACCEPT
-A INPUT -d ff02::/16 -p ipv6-icmp -m icmp6 --icmpv6-type 135 -j ACCEPT
-A INPUT -d ff02::/16 -p ipv6-icmp -m icmp6 --icmpv6-type 136 -j ACCEPT
COMMIT
# Completed on Fri Mar 30 15:35:44 2018

○システムを再起動してもルールが復元されているか確認した。

root@raspberrypi:/home/pi# reboot ←(注意)システムが再起動する。

root@raspberrypi:/home/pi# ip6tables -L -v
Chain INPUT (policy DROP 10 packets, 1598 bytes)
 pkts bytes target     prot opt in     out     source               destination       
    0     0 ACCEPT     all      any    any     anywhere             anywhere             state RELATED,ESTABLISHED
    0     0 ACCEPT     ipv6-icmp    any    any     anywhere             fe80::/64            ipv6-icmp neighbour-solicitation
    0     0 ACCEPT     ipv6-icmp    any    any     anywhere             fe80::/64            ipv6-icmp neighbour-advertisement
    0     0 ACCEPT     ipv6-icmp    any    any     anywhere             ff02::/16            ipv6-icmp neighbour-solicitation
    0     0 ACCEPT     ipv6-icmp    any    any     anywhere             ff02::/16            ipv6-icmp neighbour-advertisement
Chain FORWARD (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination       
Chain OUTPUT (policy ACCEPT 17 packets, 1954 bytes)
 pkts bytes target     prot opt in     out     source               destination
上記の通り、設定が復元されていた。


■後でルールを変更して有効化するか確認した。

(1)iptablesコマンドでルール変更する場合

○ルールを変更した。

FORWARDのポリシーをDROPに変更した。
root@raspberrypi:/home/pi# iptables -P FORWARD DROP


○変更したルールを保存した。

iptables-saveコマンドは設定内容をテキストに出力するだけ。
root@raspberrypi:/home/pi# iptables-save
# Generated by iptables-save v1.6.0 on Fri Mar 30 15:56:21 2018
*filter
:INPUT ACCEPT [22:1504]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [12:1136]
COMMIT
# Completed on Fri Mar 30 15:56:21 2018
したがって、このiptables-saveコマンドの出力を、先のルールファイルに書き込みした。
root@raspberrypi:/home/pi# iptables-save > /etc/iptables/rules.v4

書き込んだ内容を確認した。
root@raspberrypi:/home/pi# cat /etc/iptables/rules.v4
# Generated by iptables-save v1.6.0 on Fri Mar 30 15:56:37 2018
*filter
:INPUT ACCEPT [35:2392]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [20:2112]
COMMIT
# Completed on Fri Mar 30 15:56:37 2018

○システムを再起動してもルールが復元されているか確認した。

root@raspberrypi:/home/pi# reboot ←(注意)システムが再起動する。

root@raspberrypi:/home/pi# iptables -L -v
Chain INPUT (policy ACCEPT 87 packets, 9107 bytes)
 pkts bytes target     prot opt in     out     source               destination       
Chain FORWARD (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination       
Chain OUTPUT (policy ACCEPT 65 packets, 8767 bytes)
 pkts bytes target     prot opt in     out     source               destination
上記の通り、設定が復元されていた。


(2)直にルール設定ファイルを変更する場合

○直接、ルール設定ファイルを変更した。

root@raspberrypi:/home/pi# vi /etc/iptables/rules.v6
# Generated by ip6tables-save v1.6.0 on Fri Mar 30 15:35:44 2018
*filter
:INPUT DROP [8:736]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [18:1474]
-A INPUT -m state --state ESTABLISHED -j ACCEPT
-A INPUT -m state --state RELATED -j ACCEPT

-A INPUT -d fe80::/64 -p ipv6-icmp -m icmp6 --icmpv6-type 135 -j ACCEPT
-A INPUT -d fe80::/64 -p ipv6-icmp -m icmp6 --icmpv6-type 136 -j ACCEPT
-A INPUT -d ff02::/16 -p ipv6-icmp -m icmp6 --icmpv6-type 135 -j ACCEPT
-A INPUT -d ff02::/16 -p ipv6-icmp -m icmp6 --icmpv6-type 136 -j ACCEPT
COMMIT
# Completed on Fri Mar 30 15:35:44 2018

○この変更を反映させた。

サービスをrestartした。
root@raspberrypi:/home/pi# systemctl restart netfilter-persistent

反映されているか確認した。
root@raspberrypi:/home/pi# ip6tables -L -v
Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination       
    0     0 ACCEPT     all      any    any     anywhere             anywhere             state ESTABLISHED
    0     0 ACCEPT     all      any    any     anywhere             anywhere             state RELATED

    0     0 ACCEPT     ipv6-icmp    any    any     anywhere             fe80::/64            ipv6-icmp neighbour-solicitation
    0     0 ACCEPT     ipv6-icmp    any    any     anywhere             fe80::/64            ipv6-icmp neighbour-advertisement
    0     0 ACCEPT     ipv6-icmp    any    any     anywhere             ff02::/16            ipv6-icmp neighbour-solicitation
    0     0 ACCEPT     ipv6-icmp    any    any     anywhere             ff02::/16            ipv6-icmp neighbour-advertisement
Chain FORWARD (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination       
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
システムを再起動しても、変更は保持され復元された。


新品価格
¥5,980から
(2018/3/30 01:45時点)

新品価格
¥2,160から
(2018/3/30 01:42時点)

<参考>
・Persistent IPtables on Raspberry Pi (Raspbian)
< http://blog.mxard.com/persistent-iptables-on-raspberry-pi-raspbian > 2018年3月31日

・Make the configuration of iptables persistent (Debian)
< http://www.microhowto.info/howto/make_the_configuration_of_iptables_persistent_on_debian.html > 2018年3月31日


投げ銭

★当サイトへの投げ銭(PayPal)★

Ad

Ad