投げ銭

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

LINK


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

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

人気の投稿(1ヶ月間)

Ad

Ad

投げ銭

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

2018年3月16日金曜日

【Linux CentOS 7】firewalld でなく、CentOS6のようにiptablesサービスを使う手順


CentOS 7 では、firewalld がデフォルトになっている。


# systemctl status firewalld
firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: active (running) <略>
これによって、以下に挙げるようにルールが適用されている。
そのルールを見ると、プログラミングみたいに階層構造があって、ひたすら長い。
単純なサーバーだし、CentOS6みたいにもっと単純なのがいい。

自分は、CentOS6のiptablesサービスに慣れているので、
CentOS7でもそれと同じように設定したい。
インターネットに接続するサーバーの設定なので、自分で作成したルールも持っている。
それを適用したいのである。(ただし、インターフェイス名は修正する必要がある。)

不慣れなことをして、隙をつくってしまったら意味ない。



■せっかくなので、firewalldによって設定されているルールを一部だけ確認した。


# iptables -L -v
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
       
    2   104 ACCEPT     all  --  any    any     anywhere             anywhere             ctstate RELATED,ESTABLISHED
    0     0 ACCEPT     all  --  lo     any     anywhere             anywhere         
    0     0 INPUT_direct  all  --  any    any     anywhere             anywhere         
    0     0 INPUT_ZONES_SOURCE  all  --  any    any     anywhere             anywhere         
    0     0 INPUT_ZONES  all  --  any    any     anywhere             anywhere         
    0     0 DROP       all  --  any    any     anywhere             anywhere             ctstate INVALID
    0     0 REJECT     all  --  any    any     anywhere             anywhere             reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
       
    0     0 ACCEPT     all  --  any    any     anywhere             anywhere             ctstate RELATED,ESTABLISHED
    0     0 ACCEPT     all  --  lo     any     anywhere             anywhere         
    0     0 FORWARD_direct  all  --  any    any     anywhere             anywhere         
    0     0 FORWARD_IN_ZONES_SOURCE  all  --  any    any     anywhere             anywhere         
    0     0 FORWARD_IN_ZONES  all  --  any    any     anywhere             anywhere         
    0     0 FORWARD_OUT_ZONES_SOURCE  all  --  any    any     anywhere             anywhere         
    0     0 FORWARD_OUT_ZONES  all  --  any    any     anywhere             anywhere         
    0     0 DROP       all  --  any    any     anywhere             anywhere             ctstate INVALID
    0     0 REJECT     all  --  any    any     anywhere             anywhere             reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 2 packets, 104 bytes)
 pkts bytes target     prot opt in     out     source               destination
       
    2   104 OUTPUT_direct  all  --  any    any     anywhere             anywhere

このように、最初に、INPUT、FORWARD、OUTPUTチェインが定義されていて、
続いて、各チェインで参照されているターゲットが定義されている
(以下、一部のみ抜粋)

Chain INPUT_ZONES (1 references)
 pkts bytes target     prot opt in     out     source               destination       
    0     0 IN_public  all  --  wlp4s0 any     anywhere             anywhere            [goto]
    0     0 IN_public  all  --  +      any     anywhere             anywhere            [goto]
Chain INPUT_ZONES_SOURCE (1 references)
 pkts bytes target     prot opt in     out     source               destination       
Chain INPUT_direct (1 references)
 pkts bytes target     prot opt in     out     source               destination       
Chain IN_public (2 references)
 pkts bytes target     prot opt in     out     source               destination       
    0     0 IN_public_log  all  --  any    any     anywhere             anywhere         
    0     0 IN_public_deny  all  --  any    any     anywhere             anywhere         
    0     0 IN_public_allow  all  --  any    any     anywhere             anywhere         
    0     0 ACCEPT     icmp --  any    any     anywhere             anywhere         
Chain IN_public_allow (1 references)
 pkts bytes target     prot opt in     out     source               destination       
    0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere             tcp dpt:ssh ctstate NEW
Chain IN_public_deny (1 references)
 pkts bytes target     prot opt in     out     source               destination       
Chain IN_public_log (1 references)
 pkts bytes target     prot opt in     out     source               destination

以上は、INPUTチェインをルートとして定義を追ったものである。
ACCEPTだけで受けずに、まず別のチェインで受けることで、階層的に定義がされていることがわかる



■firewallでなく従来のように、iptablesコマンドで一つずつルールを設定する。

しかし、ここでは自分で従来のようにルールを管理したいので、
firewalldを無効にし、自分で定義を行いたい。

(注意)
ファイアウォールの定義を変更するため、リモートターミナルは切断される。
必ずローカルでコンソールから設定を行う。


○iptablesを導入した。

# yum install iptables-services
インストール:
  iptables-services.x86_64 0:1.4.21-18.3.el7_4
依存性を更新しました:
  iptables.x86_64 0:1.4.21-18.3.el7_4                                     
完了しました!

# yum info iptables-services
名前                : iptables-services
アーキテクチャー    : x86_64
バージョン          : 1.4.21
リリース            : 18.3.el7_4
容量                : 25 k
リポジトリー        : installed
提供元リポジトリー  : updates
要約                : iptables and ip6tables services for iptables
URL                 : http://www.netfilter.org/
ライセンス          : GPLv2
説明                : iptables services for IPv4 and IPv6
                    :
                    : This package provides the services iptables and ip6tables that have been split
                    : out of the base package since they are not active by default anymore.

(注意)
以下の手順にすすむ前に、無防備の状態を避けるため、
安全なローカルネットワーク環境で行うか、
予め、LAN接続を断つか、無効にしておく。


○firewalld を停止し、無効にする。

systemctl stop firewalld
systemctl disable firewalld

disableにすることで、システムを再起動しても firewalld は起動しない。


○代わって、iptables サービスを起動し、有効化する。

# systemctl start iptables
# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
enableにすることで、システム起動時に iptablesサービス が起動する。

(注意)
ip6tables と、iptablesは別個のサービスである。
もし同様にして、ip6tablesを設定しているなら、
ip6tablesの起動(systemctl start ip6tables)と、
自動起動(systemctl enable ip6tables)の設定も忘れないこと。


この状態で、ルールは自動的に次のようになっていた。

# iptables -L -v
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination       
 4623 2499K ACCEPT     all  --  any    any     anywhere             anywhere             state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  any    any     anywhere             anywhere         
  252 16372 ACCEPT     all  --  lo     any     anywhere             anywhere         
    0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere             state NEW tcp dpt:ssh
    3   577 REJECT     all  --  any    any     anywhere             anywhere             reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination       
    0     0 REJECT     all  --  any    any     anywhere             anywhere             reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 5231 packets, 1654K bytes)
 pkts bytes target     prot opt in     out     source               destination
あるいは、コンフフィグ直接ファイルを表示する。

# cat /etc/sysconfig/iptables
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

この状態で一応ネットに接続することができた。


○以下では、自分で作成するルールを設定する。
(設定したい自分のルールをテキストで用意してから望みましょう。)

先ず、ネットワークを不通にする。(ネットが全く使えなくなる。)

# iptables -F ; iptables -P INPUT DROP ; iptables -P FORWARD DROP ; iptables -P OUTPUT DROP

ここで、ルールの内容を表示させた。

# iptables -L -v
Chain INPUT (policy DROP 0 packets, 0 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 DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
CentOS6 で見慣れたルールが現れた。

さらに設定を続ける。
CentOS7では、CentOS6のときと違って、-i で指定するインターフェースが、
独自のものになっていることに注意する。(例えば、-i eth0 でない。)

# iptables <略>
# iptables <略>
<省略>


○設定を終えたら、ルールを保存する。

# iptables-save > /etc/sysconfig/iptables

ルールを保存しなければ、再起動時に設定内容が復元されないので注意。

念の為、システムを再起動して、ルールが適用されているか確認した。
問題なく復元され、有効化されていた。

filterだけでなく、natルールも保存するには次のページを参考にしてください。
http://akira-arets.blogspot.jp/2018/04/linux-centos7-natrules-savingrestoring.html


以上

<参考>
・CentOS7でのiptablesの設定忘れるのでメモ
< https://qiita.com/miosee/items/0599baa3a01301265a43 > 2018年3月16日

・iptables を設定する
< http://dejune.net/deblog/raspberrypi/post/20131016222940.html > 2018年3月16日

・22.8. iptables の設定を保持する(iptables-save と iptables-restore)
< http://www.turbolinux.co.jp/products/server/11s/user_guide/iptablesboot.html > 2018年3月16日

投げ銭

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

Ad

Ad