CentOS7でfirewalldの代わりにiptablesを使用する
CentOS7ではファイアウォールはiptablesからfirewalldに変更されたようだ。
7系でもyumインストールすれば従来のiptablesも利用事は出来るとの事なので、iptablesを使用する事にした。
環境
firewolldの起動状態を確認。
Activeとなっており起動してる。
[root@localhost httpd-2.4.25]# systemctl status firewalld ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) Active: active (running) since 木 2017-01-12 22:09:24 JST; 2 days ago Docs: man:firewalld(1) Main PID: 609 (firewalld) CGroup: /system.slice/firewalld.service └─609 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid 1月 12 22:09:18 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon... 1月 12 22:09:24 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
firewalldを停止する
systemctl stop firewalld systemctl disable firewalld systemctl status firewalld
停止を確認
inactiveとなってる。
[root@localhost httpd-2.4.25]# 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) 1月 12 22:09:18 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon... 1月 12 22:09:24 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon. 1月 15 12:21:57 localhost.localdomain systemd[1]: Stopping firewalld - dynamic firewall daemon... 1月 15 12:21:57 localhost.localdomain systemd[1]: Stopped firewalld - dynamic firewall daemon.
iptablesのインストール
yum -y install iptables-services
インストール確認
[root@localhost ~]# iptables --version iptables v1.4.21
systemctl start iptables systemctl enable iptables
起動してる事を確認
[root@localhost httpd-2.4.25]# 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 日 2017-01-15 12:23:41 JST; 8s ago Main PID: 85483 (code=exited, status=0/SUCCESS) 1月 15 12:23:40 localhost.localdomain systemd[1]: Starting IPv4 firewall with iptables... 1月 15 12:23:41 localhost.localdomain iptables.init[85483]: iptables: Applying firewall rules: [ OK ] 1月 15 12:23:41 localhost.localdomain systemd[1]: Started IPv4 firewall with iptables.
iptables設定ファイルが生成されてる
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
80ポートを開放する為、下記を追記
※22番ポート開放の後ろの行に追加
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
iptablesをサービス登録
[root@localhost ~]# systemctl enable iptables.service [root@localhost ~]# systemctl list-unit-files | grep iptables iptables.service enabled
iptables起動
[root@localhost ~]# systemctl start iptables.service
設定が反映されてる事を確認
[root@localhost ~]# iptables -nL Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:80 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT) target prot opt source destination REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT) target prot opt source destination
適用後は設定したポートが開いてるか他のサーバーから確認。
※nmapが無い場合はyumインストール
[root@localhost ~]# nmap 192.168.10.111 Starting Nmap 5.51 ( http://nmap.org ) at 2017-01-15 12:29 JST Nmap scan report for 192.168.10.111 Host is up (0.0000030s latency). Not shown: 997 closed ports PORT STATE SERVICE 22/tcp open ssh 80/tcp open http 8008/tcp open http Nmap done: 1 IP address (1 host up) scanned in 13.04 seconds