ネットワーク設定

ip, ss, NetworkManager

ipコマンド

ipコマンドはiproute2パッケージに含まれる 現代的なネットワーク管理ツール。従来のifconfig, route, arpの代替。

1# インターフェース管理
2ip addr # 全インターフェース表示
3ip addr show eth0 # 特定インターフェース
4ip link # リンク状態
5ip link set eth0 up # インターフェースUP
6ip link set eth0 down # インターフェースDOWN
7
8# IPアドレス設定
9ip addr add 192.168.1.10/24 dev eth0
10ip addr del 192.168.1.10/24 dev eth0
11
12# ルーティング
13ip route # ルーティングテーブル
14ip route add default via 192.168.1.1
15ip route add 10.0.0.0/8 via 192.168.1.254
16ip route del 10.0.0.0/8
17
18# ARP/Neighbor
19ip neigh # ARPテーブル
20ip neigh add 192.168.1.1 lladdr aa:bb:cc:dd:ee:ff dev eth0
21
22# 統計情報
23ip -s link # パケット統計

ssコマンド

ss(Socket Statistics)はソケット情報を表示する netstatの後継ツール。より高速で詳細な情報を提供。

1# 基本的な使い方
2ss -tuln # TCPとUDPのリスニングポート
3ss -tunp # 接続中ソケット(プロセス名付き)
4ss -a # 全ソケット
5
6# オプション
7# -t: TCP
8# -u: UDP
9# -l: リスニング
10# -n: 数値表示(名前解決なし)
11# -p: プロセス情報
12# -a: 全ソケット
13
14# フィルタリング
15ss -tn state established
16ss -tn 'dport = :443'
17ss -tn 'sport = :80'
18
19# 統計
20ss -s # ソケット統計サマリー
21
22# 詳細情報
23ss -ti # TCP内部情報(RTT, cwnd等)

NetworkManager

NetworkManagerはデスクトップ/サーバー向けの ネットワーク設定管理デーモン。CLIツールnmcliで操作可能。

1# 接続一覧
2nmcli connection show
3
4# デバイス一覧
5nmcli device status
6
7# 接続UP/DOWN
8nmcli connection up "Wired connection 1"
9nmcli connection down "Wired connection 1"
10
11# 新規接続作成
12nmcli connection add type ethernet con-name eth0-static ifname eth0 \
13 ip4 192.168.1.10/24 gw4 192.168.1.1
14
15# 設定変更
16nmcli connection modify eth0-static ipv4.dns "8.8.8.8 8.8.4.4"
17nmcli connection modify eth0-static ipv4.method manual
18
19# インタラクティブ編集
20nmcli connection edit "Wired connection 1"
21
22# Wi-Fi
23nmcli device wifi list
24nmcli device wifi connect "SSID" password "password"

Netplan (Ubuntu)

Ubuntu 18.04以降のネットワーク設定。YAMLファイルで定義し、 NetworkManagerまたはsystemd-networkdに適用。

1# /etc/netplan/01-netcfg.yaml
2network:
3 version: 2
4 renderer: networkd # または NetworkManager
5 ethernets:
6 eth0:
7 dhcp4: false
8 addresses:
9 - 192.168.1.10/24
10 gateway4: 192.168.1.1
11 nameservers:
12 addresses:
13 - 8.8.8.8
14 - 8.8.4.4
1# 設定適用
2sudo netplan apply
3
4# テスト(問題があれば自動ロールバック)
5sudo netplan try
6
7# デバッグ
8sudo netplan --debug apply

カーネルネットワークパラメータ

1# IP転送有効化
2sysctl -w net.ipv4.ip_forward=1
3
4# 永続化(/etc/sysctl.conf)
5net.ipv4.ip_forward = 1
6
7# 主要なパラメータ
8# net.ipv4.ip_forward - IPルーティング
9# net.ipv4.tcp_syncookies - SYN flood対策
10# net.ipv4.tcp_max_syn_backlog - SYNキュー最大値
11# net.core.somaxconn - listenバックログ最大値
12# net.core.netdev_max_backlog - 受信キュー最大値
13# net.ipv4.tcp_tw_reuse - TIME_WAIT再利用
14
15# 現在値確認
16sysctl net.ipv4.ip_forward
17
18# 全パラメータ表示
19sysctl -a | grep net.ipv4

SRE/インフラ観点

高負荷サーバーのチューニング

  • net.core.somaxconn: 65535
  • net.ipv4.tcp_max_syn_backlog: 65535
  • net.ipv4.tcp_tw_reuse: 1
  • net.core.netdev_max_backlog: 65535

トラブルシューティング

  • • リンクダウン: ip link, dmesg
  • • IP重複: arping
  • • 接続タイムアウト: ss -tnでTIME_WAIT確認