LoginSignup
10
11

More than 3 years have passed since last update.

aws ec2 の同一サーバへのアクセスについて

Last updated at Posted at 2019-11-10

はじめに

同一サーバへアクセスする際に、
どのIP/DNSを指定するとどんな動きをするのか、
意外と知らない人がいるなーと思ったので、書いてみました。

多分、ネットワーク経路のイメージができていないと躓く所になるのかなと。

前提条件

  • 以下のようなセキュリティグループ(以降、SGと表記)のみをアタッチしていること。
    ※ソースは『ec2にアクセスするクライアントのIP』のみを指定。
    ※つまり同一サーバへのアクセス許可はSGでは行わない

SG.PNG

  • /etc/hosts には以下の通り記してあること。
    ※Amazon Linux 2 AMIのデフォルトのまま
/etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost6 localhost6.localdomain6

アクセスしてみよう!

localhost(127.0.0.1)へアクセス

localhostへアクセス
[ec2-user@ip-172-31-0-10 ~]$ traceroute localhost
traceroute to localhost (127.0.0.1), 30 hops max, 60 byte packets
 1  localhost (127.0.0.1)  0.022 ms  0.004 ms  0.004 ms
[ec2-user@ip-172-31-0-10 ~]$

SGに許可が無くてもアクセスできます。
/etc/hosts にある通り、『ip: 127.0.0.1』に変換してアクセスしています。
イメージ的には:arrow_down:こんな感じ。

20191110_network_007.PNG

プライベートDNSへアクセス

プライベートDNSへアクセス
[ec2-user@ip-172-31-0-10 ~]$ traceroute ip-172-31-0-10.ap-northeast-1.compute.internal
traceroute to ip-172-31-0-10.ap-northeast-1.compute.internal (172.31.0.10), 30 hops max, 60 byte packets
 1  ip-172-31-0-10.ap-northeast-1.compute.internal (172.31.0.10)  0.019 ms  0.004 ms  0.004 ms
[ec2-user@ip-172-31-0-10 ~]$

SGに許可が無くてもアクセスできます。
AWS で予約されたDNSサーバで『プライベートDNS』:arrow_right:『プライベートIP』に変換されています。

次にifconfigでOS上で設定されているネットワークインターフェースの設定を見てみると:arrow_down:

[ec2-user@ip-172-31-0-10 ~]$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 9001
        inet 172.31.0.10  netmask 255.255.255.0  broadcast 172.31.0.255
        inet6 fxxx::8xxx:bxxx:fxxx:nnnn  prefixlen 64  scopeid 0x20<link>
        ether 0a:ca:ba:a8:27:94  txqueuelen 1000  (Ethernet)
        RX packets 800  bytes 91217 (89.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 875  bytes 89894 (87.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 64  bytes 4736 (4.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 64  bytes 4736 (4.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[ec2-user@ip-172-31-0-10 ~]$

上記の通り、OS内で宛先アドレスが自分だとわかるので、EC2インスタンスから外に出ずにアクセスできます。
そのため、SGを通さないので、アクセスできるのです。
これが別のインスタンスなら必ずSGを通りますので、許可していなければアクセスできないでしょう。

イメージ的には:arrow_down:こんな感じ。

20191110_network_008.PNG

パブリック DNS (IPv4)へアクセス

パブリックDNS(IPv4)へアクセス
[ec2-user@ip-172-31-0-10 ~]$ traceroute ec2-xx-xxx-xxx-xxx.ap-northeast-1.compute.amazonaws.com
traceroute to ec2-xx-xxx-xxx-xxx.ap-northeast-1.compute.amazonaws.com (172.31.0.10), 30 hops max, 60 byte packets
 1  ip-172-31-0-10.ap-northeast-1.compute.internal (172.31.0.10)  0.018 ms  0.005 ms  0.004 ms
[ec2-user@ip-172-31-0-10 ~]$

SGに許可が無くてもアクセスできます。
AWS で予約されたDNSサーバで『パブリック DNS (IPv4)』:arrow_right:『プライベートIP』に変換されています。
そのため、『プライベートDNSへアクセス』の時と同様のアクセスルートになります。

IPv4 パブリック IPへアクセス

IPv4パブリックIPへアクセス
[ec2-user@ip-172-31-0-10 ~]$ traceroute xx.xxx.xxx.xxx
traceroute to xx.xxx.xxx.xxx (xx.xxx.xxx.xxx), 30 hops max, 60 byte packets
 1  ec2-175-41-192-214.ap-northeast-1.compute.amazonaws.com (175.41.192.214)  18.174 ms ec2-175-41-192-216.ap-northeast-1.compute.amazonaws.com (175.41.192.216)  2.868 ms ec2-175-41-192-220.ap-northeast-1.compute.amazonaws.com (175.41.192.220)  11.470 ms
 2  * * *
 3  * * *
 ~~~~~ 中略 ~~~~~
29  * * *
30  * * *
[ec2-user@ip-172-31-0-10 ~]$

SGに許可が無いとアクセスできません。
IPv4 パブリック IPはifconfigで見つからない、かつプライベートIPでもないので、インターネットゲートウェイを通して外に探しに行きます。
しかし、SGでアクセス許可をしていないので、たどり着くことはありません。

イメージ的には:arrow_down:こんな感じ。

20191111_EC2.PNG

まとめ

自分:arrow_right:自分へアクセスする場合、

IP/DNS SG許可の要・不要 備考
127.0.0.1 不要 ifconfig による
localhost 不要 /etc/hosts による
プライベートIP 不要 ifconfig による
プライベートDNS 不要 プライベートIPと同義
IPv4 パブリック IP
パブリック DNS (IPv4) 不要 プライベートIPと同義

参考URL

10
11
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
10
11