メインコンテンツへスキップ

CentOS 7 でiptablesを使う

    DB Server や Redis Server を独立させて使うのに iptables を使ってます。CentOS7はfirewalldがデフォルトなのですが、それはまた別の機会に、、、

    まずはfirewalldを止める

    $ sudo systemctl stop firewalld
    $ sudo systemctl disable firewalld
    Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
    Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
    $ sudo systemctl status firewalld
    ● firewalld.service - firewalld - dynamic firewall daemon
      Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
      Active: inactive (dead)
        Docs: man:firewalld(1)
    
    8月 10 11:49:48 localhost.localdomain systemd[1]: Starting firewalld - dyna...
    8月 10 11:49:50 localhost.localdomain systemd[1]: Started firewalld - dynam...
    8月 10 23:42:53 150-95-204-21 systemd[1]: Stopping firewalld - dynamic fir....
    8月 10 23:42:54 150-95-204-21 systemd[1]: Stopped firewalld - dynamic fire....
    Hint: Some lines were ellipsized, use -l to show in full.

    これで Active: inactive (dead) になったみたいですね。

    iptablesのインストール

    $ sudo yum -y install iptables-services
    読み込んだプラグイン:fastestmirror, langpacks
    Loading mirror speeds from cached hostfile
    epel/x86_64/metalink                                     | 8.6 kB     00:00     
    epel-debuginfo/x86_64/metalink                           | 8.9 kB     00:00     
    epel-source/x86_64/metalink                              | 8.6 kB     00:00     
    * base: ty1.mirror.newmediaexpress.com
    * epel: nrt.edge.kernel.org
    * epel-debuginfo: nrt.edge.kernel.org
    * epel-source: nrt.edge.kernel.org
    * extras: ty1.mirror.newmediaexpress.com
    * updates: ty1.mirror.newmediaexpress.com
    base                                                     | 3.6 kB     00:00     
    epel                                                     | 5.3 kB     00:00     
    epel-debuginfo                                           | 3.0 kB     00:00     
    epel-source                                              | 4.1 kB     00:00     
    extras                                                   | 3.4 kB     00:00     
    updates                                                  | 3.4 kB     00:00     
    依存性の解決をしています
    --> トランザクションの確認を実行しています。
    ---> パッケージ iptables-services.x86_64 0:1.4.21-28.el7 を インストール
    --> 依存性解決を終了しました。
    
    依存性を解決しました
    
    ================================================================================
    Package                  アーキテクチャー
                                           バージョン            リポジトリー
                                                                              容量
    ================================================================================
    インストール中:
    iptables-services        x86_64        1.4.21-28.el7         base         52 k
    
    トランザクションの要約
    ================================================================================
    インストール  1 パッケージ
    
    総ダウンロード容量: 52 k
    インストール容量: 26 k
    Downloading packages:
    iptables-services-1.4.21-28.el7.x86_64.rpm                 |  52 kB   00:00     
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
     インストール中          : iptables-services-1.4.21-28.el7.x86_64          1/1 
     検証中                  : iptables-services-1.4.21-28.el7.x86_64          1/1 
    
    インストール:
     iptables-services.x86_64 0:1.4.21-28.el7                                      
    
    完了しました!
    

    起動とその確認

    $ sudo systemctl start iptables
    $ sudo systemctl enable iptables
    Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
    $ sudo systemctl status iptables
    ● iptables.service - IPv4 firewall with iptables
      Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled)
      Active: active (exited) since 土 2019-08-10 23:45:25 JST; 13s ago
    Main PID: 4115 (code=exited, status=0/SUCCESS)
    
    8月 10 23:45:25 150-95-204-21 systemd[1]: Starting IPv4 firewall with iptables...
    8月 10 23:45:25 150-95-204-21 iptables.init[4115]: iptables: Applying firewall rules: [  OK  ]
    8月 10 23:45:25 150-95-204-21 systemd[1]: Started IPv4 firewall with iptables.

    Active: active (exited) になりました。

    iptablesの設定

    $ sudo vi /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 -p tcp -m tcp -s [Redis接続を許可したいIP] --dport 6379 -j ACCEPT
    -A INPUT -j REJECT --reject-with icmp-host-prohibited
    -A FORWARD -j REJECT --reject-with icmp-host-prohibited
    COMMIT

    上記の例ではRedisサーバー(Port:6379)に接続する元をIPで制限しています。

    設定の反映と確認

    $ sudo service iptables reload
    Redirecting to /bin/systemctl reload iptables.service
    $ sudo iptables -L
    Chain INPUT (policy ACCEPT)
    target     prot opt source               destination         
    ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
    ACCEPT     icmp --  anywhere             anywhere            
    ACCEPT     all  --  anywhere             anywhere            
    ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
    ACCEPT     tcp  --  [接続許可されたホスト]  anywhere             tcp dpt:6379
    REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
    
    Chain FORWARD (policy ACCEPT)
    target     prot opt source               destination         
    REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
    
    Chain OUTPUT (policy ACCEPT)
    target     prot opt source               destination