Linuc102メモ⑩ネットワークの基礎

TCP/IPプロトコル

TCP(Transmission Control Protocol)

  • 信頼性の高い通信を実現するためにコネクション型
    相手に通信データが届いてるか確認

  • 途中で焼失したパケットを再送

  • パケットの伝送順序を整列

  • 伝送速度は遅い

IP

  • コネクションレス型
    相手にデータが届いてるか確認せずに一方的に送る

  • 伝送速度は速い

  • ネットワーク機器へのアドレス付与

  • パケットの分割や統合を行う

UDP(User Datagram Protocol)

  • コネクションレス型

  • 伝送速度は速い

  • 音声や映像のストリーミング配信に使われる

ネットワークの基本設定

etc/hostname

ホスト名を設定するファイル

[root@localhost etc]# cat hostname
localhost.localdomain

etc/hosts

ホスト名とIPアドレスとの対応を記述してある。
ただし、ホスト名の設定はetc/hostnameで設定する。

[root@localhost etc]# cat hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

etc/nsswitch.conf

名前解決やサービス名解決の際の順序が記載されてある。

[root@localhost etc]# cat nsswitch.conf
<一部省略>
hosts:      files dns myhostname
ethers:     files
netmasks:   files
networks:   files
protocols:  files
rpc:        files
services:   files sss

netgroup:   nisplus sss

publickey:  nisplus

automount:  files nisplus sss
aliases:    files nisplus

etc/resolve.conf

ドメイン名やDNSサーバ名の指定ができる。

[root@localhost etc]# cat resolv.conf
# Generated by NetworkManager
search flets-east.jp iptvf.jp
nameserver 192.168.100.1
nameserver 2001:f70:b280:4300::1

etc/services

サービス名とポート番号の対応付け

asipregistry    687/tcp                 # asipregistry
asipregistry    687/udp                 # asipregistry
realm-rusd      688/tcp                 # ApplianceWare managment protocol
realm-rusd      688/udp                 # ApplianceWare managment protocol
nmap            689/tcp                 # NMAP
nmap            689/udp                 # NMAP

etc/sysconfig

ホスト名、ネットワーク機能の有効化、ゲートウェイの設定など

NETWORKING=yes
NETWORKING_IPV6=yes
HOSTNAME=CentOS
GATEWAY=192.168.1.1

コマンドによるネットワーク設定

CentOSやRed Hat系では、ネットワーク管理をNetworkManagerによって行われている。

nmcli

NetworkManagerでは、nmcliコマンドを使ってネットワーク設定、接続の管理、状態の管理を行う。

[root@localhost sysconfig]# nmcli general status
STATE     CONNECTIVITY  WIFI-HW  WIFI  WWAN-HW  WWAN
接続済み  完全          有効     有効  有効     有効

getent

getent データベース名 [ キー ... ]
データベース名に nsswitch.conf で指定できるサービス名、検索したい情報をキーに指定することで、名前解決順序設定に従って検索した結果を得られる。
以下ではtestsvが名前解決できるかどうかを確認している。

[root@localhost etc]# cat hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.100.100 testsv.localdomain testsv
[root@localhost etc]# getent hosts testsv
192.168.100.100 testsv.localdomain testsv

ip

ネットワークインターフェースやルーティングテーブル、ARPテーブルなどを管理するコマンド。
ip 操作対象 [サブコマンド][デバイス]

[root@localhost etc]# ip route show
default via 192.168.100.1 dev ens33 proto static metric 100
192.168.100.0/24 dev ens33 proto kernel scope link src 192.168.100.9 metric 100
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1

ip link show インターフェースの状況確認

[root@CentOS7 ken]# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 00:0c:29:5e:21:e2 brd ff:ff:ff:ff:ff:ff
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default qlen 1000
    link/ether 52:54:00:a5:a3:4a brd ff:ff:ff:ff:ff:ff
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN mode DEFAULT group default qlen 1000
    link/ether 52:54:00:a5:a3:4a brd ff:ff:ff:ff:ff:ff

netstat

有効なネットワーク情報や開いているソケットの情報を表示。(オプションなし)
また、オプションを併用することで、ホストの様々なネットワーク情報を表示できる。
netstat -i ネットワークインターフェースの統計情報を表示

[root@localhost etc]# netstat -i
Kernel Interface table
Iface             MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
ens33            1500   172811      0      0 0         58810      0      0      0 BMRU
lo              65536       20      0      0 0            20      0      0      0 LRU
virbr0           1500        0      0      0 0             0      0      0      0 BMU
[root@CentOS7 ken]# netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:sunrpc          0.0.0.0:*               LISTEN
tcp        0      0 CentOS7:domain          0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN
tcp        0      0 localhost:ipp           0.0.0.0:*               LISTEN
tcp        0      0 localhost:smtp          0.0.0.0:*               LISTEN
tcp        0     36 CentOS7:ssh             192.168.100.4:65000     ESTABLISHED
tcp6       0      0 [::]:sunrpc             [::]:*                  LISTEN
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN

route

ルーティングテーブルを表示したり、追加・削除できるコマンド。

[root@localhost etc]# route -F
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 ens33
192.168.100.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
[root@localhost etc]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 ens33
192.168.100.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

ss

netstatコマンドの後継となるコマンドで、ネットワークのソケット情報を表示。
ソケットとは、プログラムとネットワークをつなげる接続口を指す。
ss -at TCPソケットをすべて表示(名前解決あり)
ss -atn TCPソケットをすべて表示(名前解決なし)

[root@CentOS7 ken]# ss -at
State      Recv-Q Send-Q Local Address:Port                 Peer Address:Port   
LISTEN     0      128        *:sunrpc                   *:*
LISTEN     0      5      192.168.122.1:domain                   *:*             
LISTEN     0      128        *:ssh                      *:*
LISTEN     0      128    127.0.0.1:ipp                      *:*                 
LISTEN     0      100    127.0.0.1:smtp                     *:*                 
ESTAB      0      0      192.168.100.9:ssh                  192.168.100.4:65000 
LISTEN     0      128     [::]:sunrpc                [::]:*
LISTEN     0      128     [::]:ssh                   [::]:*
LISTEN     0      128    [::1]:ipp                   [::]:*
LISTEN     0      128     [::]:telnet                [::]:*
LISTEN     0      100    [::1]:smtp                  [::]:*
[root@CentOS7 ken]# ss -ant
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port     
LISTEN     0      128          *:111                      *:*
LISTEN     0      5      192.168.122.1:53                       *:*             
LISTEN     0      128          *:22                       *:*
LISTEN     0      128    127.0.0.1:631                      *:*                 
LISTEN     0      100    127.0.0.1:25                       *:*                 
ESTAB      0      36     192.168.100.9:22                 192.168.100.4:65000   
LISTEN     0      128       [::]:111                   [::]:*
LISTEN     0      128       [::]:22                    [::]:*
LISTEN     0      128      [::1]:631                   [::]:*
LISTEN     0      128       [::]:23                    [::]:*
LISTEN     0      100      [::1]:25                    [::]:*

tracepath

宛先までの経路情報を確認できる。
PathMTUを確認できる。
PathMTUとは、データを送る際の最小値。

[root@CentOS7 ken]# tracepath 8.8.8.8
 1?: [LOCALHOST]                                         pmtu 1500
 1:  gateway                                              90.818ms
 1:  gateway                                               0.539ms
 2:  gateway                                               0.611ms pmtu 1280
 2:  192.0.0.1                                            66.228ms
 3:  163.139.136.70                                        5.249ms
 4:  163.139.92.4                                          6.690ms
 5:  163.139.37.98                                         3.877ms
 6:  163.139.78.2                                          5.014ms asymm  7

Netcat(nc)

TCP/UDPを使った通信を手軽に行うことのできる便利なツール。
デフォルトでは、TCPで動く。

ping

疎通確認用のコマンド。

  • -i 疎通確認用パケットの送信間隔(interval)

  • -c パケットの送信回数(count)

  • -n 結果表示の形式をホスト名ではなく、ipで出力(numeric)

[root@CentOS7 ken]# ping -c 2 -i 2 -n google.com
PING google.com (142.250.207.14) 56(84) bytes of data.
64 bytes from 142.250.207.14: icmp_seq=1 ttl=58 time=4.15 ms
64 bytes from 142.250.207.14: icmp_seq=2 ttl=58 time=3.53 ms

--- google.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 3.532/3.843/4.155/0.317 ms

クライアントのDNS設定

DNSの概要

  • DNSサーバがホスト名とIPアドレスを相互変換する

  • 正引きはホスト名→IP

  • 逆引きはIP→ホスト名

  • www(ホスト名).example.com(ドメイン名)

DNS設定ファイル

参照DNSサーバは/etc/resolve.confファイルに記されている。

[root@CentOS7 etc]# cat resolv.conf
# Generated by NetworkManager
nameserver 192.168.100.1

名前解決の順序/etc/nesswitch.confファイルに記されている。

ethers:     files
netmasks:   files
networks:   files
protocols:  files
rpc:        files
services:   files sss

netgroup:   nisplus sss

publickey:  nisplus

automount:  files nisplus sss
aliases:    files nisplus

getent hosts

こちらのコマンドを使用するとホスト一覧を取得できる。

[root@CentOS7 etc]# getent hosts
127.0.0.1       localhost localhost.localdomain localhost4 localhost4.localdomain4
127.0.0.1       localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.100.100 testsv.localdomain testsv

host

DNSサーバを使ってホストやドメイン名に関する情報を表示。
デフォルトは、ホスト→IPの変換

[root@CentOS7 etc]# host google.com
google.com has address 172.217.175.238
google.com has IPv6 address 2404:6800:4004:825::200e
google.com mail is handled by 10 smtp.google.com.

host -t mx [ドメイン名] 指定ドメインのメールサーバの情報を問いあわせる。

[root@CentOS7 etc]# host -t mx yahoo.co.jp
yahoo.co.jp mail is handled by 10 mx5.mail.yahoo.co.jp.
yahoo.co.jp mail is handled by 10 mx1.mail.yahoo.co.jp.
yahoo.co.jp mail is handled by 10 mx3.mail.yahoo.co.jp.
yahoo.co.jp mail is handled by 10 mx2.mail.yahoo.co.jp.

dig

DNSサーバに登録されている情報を詳しく表示する。

[root@CentOS7 etc]# dig google.com

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.13 <<>> google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: FORMERR, id: 11169
;; flags: qr rd; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0
;; WARNING: recursion requested but not available

;; WARNING: EDNS query returned status FORMERR - retry with '+noedns'

;; QUESTION SECTION:
;google.com.                    IN      A

;; Query time: 857 msec
;; SERVER: 192.168.100.1#53(192.168.100.1)
;; WHEN:804 07:46:39 PDT 2023
;; MSG SIZE  rcvd: 28

dig 「ドメイン名」-m 対象ドメインのメールサーバの情報を検索


この記事が気に入ったらサポートをしてみませんか?