0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Oracle Cloudでイングレス・ルールを設定しても指定ポートにアクセスできないときの解決方法

0
Posted at

はじめに

備忘録
どなたかのお役に立てれば幸いです。

問題点と前提

具体的には、Oracle Cloudのインスタンスに紐づいているVCNのサブネットのセキュリティ・リストで、イングレス・ルールに許可したいポートを指定したにも関わらず一向にアクセスできませんでした。
ちなみに設定が適応されてないだけかと思って再起動なども試しましたが、ダメでした。

前提として、イングレス・ルールは以下のようになっていました。

ステートレス ソース IPプロトコル 宛先ポート範囲 ICMPタイプとコード 説明
いいえ 0.0.0.0/0 TCP 22 (該当なし) SSHリモートログイン
いいえ 0.0.0.0/0 ICMP (該当なし) 3, 4 宛先到達不可 (フラグメンテーション)
いいえ 10.0.0.0/16 ICMP (該当なし) 3 VCN内部からの宛先到達不可
いいえ 0.0.0.0/0 TCP 80 (該当なし) 👉 自分で追加した項目
いいえ 0.0.0.0/0 TCP 3000 (該当なし) 👉 自分で追加した項目(アクセス確認用)

また、エグレス・ルールは以下のようになっていました。

ステートレス ソース IPプロトコル 宛先ポート範囲 説明
いいえ 0.0.0.0/0 すべて すべて すべてのポートのすべてのトラフィック

つまり、基本的に外部からのアクセスは全て拒否するが、イングレス・ルールに追加したものだけは許可する、という設定になっていました。

解決方法

結論から言うと、iptablesが原因で、アクセスできていませんでした。
Oracle CloudのGUIからイングレス・ルールを追加しても、インスタンス中のiptablesが別で設定されていると、影響を受けるようです。
そのため、iptablesの設定を見直すことで解決しました。

解決前は、以下のような設定になっていました。
つまり、自分で追加した項目であるポート803000へのTCP通信はREJECTされていました。

$ 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
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
:

そこで、以下のコマンドを実行して、iptablesACCEPTの設定を追加しました。

sudo iptables -I INPUT 5 -p tcp --dport 80 -j ACCEPT
sudo iptables -I INPUT 5 -p tcp --dport 3000 -j ACCEPT

再びiptablesコマンドで設定を確認してみます。

$ 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             anywhere             tcp dpt:http
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:3000
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
:

無事、設定が追加されていることが確認できました。

ただしこのままだと、インスタンスを再起動したときに設定が消えてしまうので、設定を保存して再起動後も自動で読み込まれるようにする必要があります。

以下のコマンドを実行します。

$ sudo apt-get update
$ sudo apt-get install iptables-persistent

インストール中に、Save current IPv4 rules?と聞かれたら、YESと答えると、設定を保存できます。IPv6についても聞かれるかもしれません。

もし聞かれなかったら、手動で追加する必要があります。

$ sudo netfilter-persistent save
run-parts: executing /usr/share/netfilter-persistent/plugins.d/15-ip4tables save
run-parts: executing /usr/share/netfilter-persistent/plugins.d/25-ip6tables save

保存した設定は/etc/iptables/rules.v4で確認できます。

$ sudo cat /etc/iptables/rules.v4
:
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 3000 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
:

インスタンスを再起動してみると、設定が引き継がれていることを確認できます。

$ 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             anywhere             tcp dpt:http
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:3000
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
:
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?