2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CentOSファイアーウォールのfirewalldをiptablesに変更する。

Last updated at Posted at 2019-07-12

あらすじ

先輩に教えてもらったキーワードを元にサーバーを構築する備忘録。
前回でcentOSをサーバーに入れることに成功したので、初期設定を行う。
今回はファイアーウォールを変更する。

firewalld・iptables

CentOSで使われているファイアーウォールです、前身はiptableだそうな。今回は練習ということでiptablesに変更します。

firewalldを停止しよう

前身のiptableに変更しますがfirewalldは特に削除する必要はありません。止めるだけでOK。

# systemctl status firewalld

これで状態を確認できます。activeであれば緑色のランプがついてるはずです。
動作しているのがわかれば、停止しちゃいましょう。

//止めます
# systemctl stop firewalld
//自動起動も止めます
# systemctl disable firewalld

//確認して、inactiveであればOK
# systemctl status firewalld

おやすみFirewalld。

iptablesのインストール

# yum -y install iptables-services
//バージョン確認
# iptables --version
//起動、自動起動を開始
# systemctl start iptables
# systemctl enable iptables
//activeか確認してください。
# systemctl status iptables

ポートの開放

設定ファイルが作成されているので編集してポートを開放しましょう。
viで編集します。

# vi /etc/sysconfig/iptables


//vi----------------------------------------------

# 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 チェイン -s どこから -d どこに -p どうやって --dport どこに(port) -j どうする
-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
# TCP
COMMIT


# dport 22の上に追加する(どちらかでok)
# 1.特定のIPアドレスのみ全ポート開放(使用頻度の高いもので)------
-A INPUT -p tcp -m state -s 111.222.3.4/24 --state NEW -j ACCEPT

# 2.80番ポート開放------
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

iptablesをサービス登録

# systemctl enable iptables.service

# systemctl list-unit-files | grep iptables iptables.service                            

# systemctl start iptables.service

設定が反映されてるか見てみましょう。

# iptables -nL

//追加した設定があればOK
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  --  111.222.3.4/24       0.0.0.0/0            state NEW
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
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 -y install nmap

# nmap  111.222.3.4 #(サーバのIPアドレスを指定すること)

これでファイアーウォールの変更と、ポートの開放ができました。次回もどんどん他のをいれていきます。

参考文献

CentOS7でfirewalldの代わりにiptablesを使用する
CentOS にポートスキャンツール Nmap をインストールして使ってみる

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?