たくろぐ!

世界一のチラ裏

ネットワークインターフェースについて調べてみた

ネットワーク難しすぎ問題

プログラミングするためには仮想環境を構築するところから始まるんだが、
この辺ってプロジェクト内でまとまったドキュメントがたいていどこでもあって
ぽちぽちするだけで意識しなくても環境が作れてしまうから手薄になりがちなんだな。

シェルスクリプトに作業書いたのを叩けるようにすれば
内部で何やってるかわからないうちにできたり、
そうでなくてもコンテナ技術やそれを応用したクラスタリングなどで
どんどん概念的になり抽象化されているよな。

そんな時代に逆行するようだがエンジニアっていう仕事は
一見遠回りしているような作業が実は近道だったりすることが
たくさんあって今回はネットワークについてしらべてみたぞ。

ホストOSとゲストOS

ホストって聞くとホストクラブを思い出すよな?(脅し)
ホストっていうのは本来もてなす側の立場のことを指していて、逆にもてなしてもらう側のことはゲストと言う。

だからホストクラブのホストは女の子をもてなして楽しませる仕事という意味になるな。

逆にゲストというのは日常的によく聞くよな。
テレビ番組で「今日のゲストはこの方です!」とか言うと思うけど、
日替わりとか週替わりで来る来客側の人を指しているよな?
彼らはもてなしてもらう側ということだな。

パソコン(以下マシン)の世界でも同じで、ご主人様パソコンの中に家来パソコンが何人もいるようなイメージだ。
親のマシンをホストOS(オペレーションシステム)、子のマシンをゲストOSって呼ぶ。

ただこの子のマシンを作るのにはちょっとしたマシンの知識とツールの理解が必要だ。

VirtualBoxって何?

バーチャルリアリティという言葉を一度は聞いたことがあるだろう。
アダルトVRというとVRゴーグルを使ってアダルトビデオを立体的に視聴することができるよな。

バーチャルボックスというのはBoxという単位で仮想(バーチャル)的にいくつもホストOSにゲストOSを作ることができるツールのことを言う。
世界のホスティングサーバのほぼ100%がLinuxOSと言われているが、そんなサーバ向けの環境を自分のマシン上に立てることができる画期的なツールだ。

有志が作成したBoxがインターネットに置いてあってそれを好きなように利用できるので簡単に手元で試すことができる。

そして環境を作ってしまえば簡単なのは間違いないのだが、作るまでが大変というところもVirtualBoxとはまた別のツールが肩代わりしてくれている。

それがVagrantだ。

Vagrantって何?

Vagrantはプロビジョニングツールとも言われていて、サーバを立てるにあたって必要な細かい設定をまとめて実行してくれる。

準備しておいたファイルを実行すると、あとはサーバが動くようによしなにやってくれるといったイメージだ。

サーバを立てるのに簡単なコマンドを実行するだけだからとても簡単だな。

早速やってみた

とまぁ細かい説明はこれくらいにして早速実際に動かしてみたぞ。

TakunoMBookpuro:~ a3ae10511$ ifconfig
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
    options=1203<RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP>
    inet 127.0.0.1 netmask 0xff000000 
    inet6 ::1 prefixlen 128 
    inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
    nd6 options=201<PERFORMNUD,DAD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
XHC1: flags=0<> mtu 0
XHC0: flags=0<> mtu 0
XHC20: flags=0<> mtu 0
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether 8c:85:90:b9:56:29 
    inet6 fe80::ad:4a0e:f1b7:5367%en0 prefixlen 64 secured scopeid 0x8 
    inet 192.168.128.129 netmask 0xffffff00 broadcast 192.168.128.255
    nd6 options=201<PERFORMNUD,DAD>
    media: autoselect
    status: active
en1: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
    options=60<TSO4,TSO6>
    ether a2:00:a4:b8:85:01 
    media: autoselect <full-duplex>
    status: inactive
en2: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
    options=60<TSO4,TSO6>
    ether a2:00:a4:b8:85:00 
    media: autoselect <full-duplex>
    status: inactive
en3: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
    options=60<TSO4,TSO6>
    ether a2:00:a4:b8:85:05 
    media: autoselect <full-duplex>
    status: inactive
en4: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
    options=60<TSO4,TSO6>
    ether a2:00:a4:b8:85:04 
    media: autoselect <full-duplex>
    status: inactive
p2p0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 2304
    ether 0e:85:90:b9:56:29 
    media: autoselect
    status: inactive
awdl0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1484
    ether ea:91:5a:27:38:bd 
    inet6 fe80::e891:5aff:fe27:38bd%awdl0 prefixlen 64 scopeid 0xe 
    nd6 options=201<PERFORMNUD,DAD>
    media: autoselect
    status: active
bridge0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    options=63<RXCSUM,TXCSUM,TSO4,TSO6>
    ether a2:00:a4:b8:85:01 
    Configuration:
        id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0
        maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200
        root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0
        ipfilter disabled flags 0x2
    member: en1 flags=3<LEARNING,DISCOVER>
            ifmaxaddr 0 port 9 priority 0 path cost 0
    member: en2 flags=3<LEARNING,DISCOVER>
            ifmaxaddr 0 port 10 priority 0 path cost 0
    member: en3 flags=3<LEARNING,DISCOVER>
            ifmaxaddr 0 port 11 priority 0 path cost 0
    member: en4 flags=3<LEARNING,DISCOVER>
            ifmaxaddr 0 port 12 priority 0 path cost 0
    nd6 options=201<PERFORMNUD,DAD>
    media: <unknown type>
    status: inactive
utun0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 2000
    inet6 fe80::1c5f:2759:82b3:efb0%utun0 prefixlen 64 scopeid 0x11 
    nd6 options=201<PERFORMNUD,DAD>
vboxnet0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether 0a:00:27:00:00:00 
    inet 192.168.33.1 netmask 0xffffff00 broadcast 192.168.33.255
en5: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether ac:de:48:00:11:22 
    inet6 fe80::aede:48ff:fe00:1122%en5 prefixlen 64 scopeid 0x7 
    nd6 options=201<PERFORMNUD,DAD>
    media: autoselect
    status: active

ifconfigはネットワークアダプターを表示させるためのコマンドだ。

最近だとip aを推奨しているがまだまだこの業界でも普通に使ってるのをみるな。

Macはデフォルトではip aは使えなかった。

MacWindowsLinuxでは表示が異なるが、
Macの場合は

lo0 = loopback
gif0 = Software Network Interface
stf0 = 6to4 tunnel interface
en0 = Ethernet 0
fw0 = Firewire
en1 = Ethernet 1
vmnet8 = Virtual Interface
vmnet1 = Virtual Interface

とあるように、ネットワークインターフェースを一覧表示してくれる。

ネットワークインターフェースとは自分と他の通信端末を繋ぐ部分のこと。

TakunoMBookpuro:~ a3ae10511$ networksetup -listallhardwareports

Hardware Port: Wi-Fi
Device: en0
Ethernet Address: 8c:85:90:b9:56:29

Hardware Port: Bluetooth PAN
Device: en6
Ethernet Address: 8c:85:90:b4:b6:9d

Hardware Port: Thunderbolt 1
Device: en1
Ethernet Address: a2:00:a4:b8:85:01

Hardware Port: Thunderbolt 2
Device: en2
Ethernet Address: a2:00:a4:b8:85:00

Hardware Port: Thunderbolt 3
Device: en3
Ethernet Address: a2:00:a4:b8:85:05

Hardware Port: Thunderbolt 4
Device: en4
Ethernet Address: a2:00:a4:b8:85:04

Hardware Port: Thunderbolt Bridge
Device: bridge0
Ethernet Address: a2:00:a4:b8:85:01

VLAN Configurations
===================
TakunoMBookpuro:~ a3ae10511$ netstat -rn
Routing tables

Internet:
Destination        Gateway            Flags        Refs      Use   Netif Expire
default            192.168.128.1      UGSc          153        0     en0
127                127.0.0.1          UCS             0        0     lo0
127.0.0.1          127.0.0.1          UH              1   251404     lo0
169.254            link#8             UCS             0        0     en0
192.168.33         link#19            UC              1        0 vboxnet
192.168.33.1       a:0:27:0:0:0       UHLWIi          1       55     lo0
192.168.128        link#8             UCS             0        0     en0
192.168.128.1/32   link#8             UCS             2        0     en0
192.168.128.1      36:de:34:c7:93:a6  UHLWIir        16       86     en0   1167
192.168.128.129/32 link#8             UCS             0        0     en0
224.0.0/4          link#8             UmCS            2        0     en0
224.0.0.251        1:0:5e:0:0:fb      UHmLWI          0        0     en0
239.255.255.250    1:0:5e:7f:ff:fa    UHmLWI          0       24     en0
255.255.255.255/32 link#8             UCS             0        0     en0

Internet6:
Destination                             Gateway                         Flags         Netif Expire
default                                 fe80::%utun0                    UGcI          utun0
::1                                     ::1                             UHL             lo0
fe80::%lo0/64                           fe80::1%lo0                     UcI             lo0
fe80::1%lo0                             link#1                          UHLI            lo0
fe80::%en5/64                           link#7                          UCI             en5
fe80::aede:48ff:fe00:1122%en5           ac:de:48:0:11:22                UHLI            lo0
fe80::aede:48ff:fe33:4455%en5           ac:de:48:33:44:55               UHLWIi          en5
fe80::%en0/64                           link#8                          UCI             en0
fe80::ad:4a0e:f1b7:5367%en0             8c:85:90:b9:56:29               UHLI            lo0
fe80::%awdl0/64                         link#14                         UCI           awdl0
fe80::e891:5aff:fe27:38bd%awdl0         ea:91:5a:27:38:bd               UHLI            lo0
fe80::%utun0/64                         fe80::1c5f:2759:82b3:efb0%utun0 UcI           utun0
fe80::1c5f:2759:82b3:efb0%utun0         link#17                         UHLI            lo0
ff01::%lo0/32                           ::1                             UmCI            lo0
ff01::%en5/32                           link#7                          UmCI            en5
ff01::%en0/32                           link#8                          UmCI            en0
ff01::%awdl0/32                         link#14                         UmCI          awdl0
ff01::%utun0/32                         fe80::1c5f:2759:82b3:efb0%utun0 UmCI          utun0
ff02::%lo0/32                           ::1                             UmCI            lo0
ff02::%en5/32                           link#7                          UmCI            en5
ff02::%en0/32                           link#8                          UmCI            en0
ff02::%awdl0/32                         link#14                         UmCI          awdl0
ff02::%utun0/32                         fe80::1c5f:2759:82b3:efb0%utun0 UmCI          utun0

TakunoMBookpuro:~ a3ae10511$ netstat -aL
Current listen queue sizes (qlen/incqlen/maxqlen)
Listen         Local Address         
0/0/128        *.55512                
0/0/128        *.55512                
0/0/10         *.61500                
0/0/10         *.61500                
0/0/5          localhost.ipp          
0/0/5          localhost.ipp          

Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "ubuntu/xenial64"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
  config.vm.network "forwarded_port", guest: 8000, host: 9080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"
  config.vm.synced_folder "./tmp", "/home/vagrant/tmp"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end

vagrant側のifconfig

vagrant@ubuntu-xenial:~$ ifconfig
enp0s3    Link encap:Ethernet  HWaddr 02:2e:d6:ba:66:f8
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::2e:d6ff:feba:66f8/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1148 errors:0 dropped:0 overruns:0 frame:0
          TX packets:690 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:428654 (428.6 KB)  TX bytes:86297 (86.2 KB)

enp0s8    Link encap:Ethernet  HWaddr 08:00:27:c6:5b:9a
          inet addr:192.168.33.10  Bcast:192.168.33.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fec6:5b9a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3 errors:0 dropped:0 overruns:0 frame:0
          TX packets:14 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:330 (330.0 B)  TX bytes:1156 (1.1 KB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

vagrant@ubuntu-xenial:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 02:2e:d6:ba:66:f8 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::2e:d6ff:feba:66f8/64 scope link
       valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:c6:5b:9a brd ff:ff:ff:ff:ff:ff
    inet 192.168.33.10/24 brd 192.168.33.255 scope global enp0s8
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fec6:5b9a/64 scope link
       valid_lft forever preferred_lft forever

ネットワークインターフェースの種類

NAT

基本情報技術者の試験勉強でIPマスカレード(NAPT)と一緒に習ったやつ。
NAPTは複数のノードでグローバルIPを共有しようってやつ。
こいつはプライベートIPとグローバルIPを1対1で繋げるネットワークインターフェース。

内部的には各ノード同士は通信できないが、外部にはそれぞれが通信できる。
外部からは接続ができない。

NATネットワーク

内部ネットワーク

ホストオンリーアダプター

ブリッジアダプター

おまけ

ping
route
traceroute
arp
ipconfig
netstat
ifconfig
nslookup

参考にした

zorinos.seesaa.net

qiita.com

success.tracpath.com

CentOS7.2 64bit インストール時にVirtualBoxでNAT、ホストオンリーアダプターを使用する場合のネットワークの設定 | kakiro-web カキローウェブ