Network Interface Configuration บน Linux — ip, nmcli, Netplan และ Bonding

การตั้งค่า Network Interface บน Linux เป็นทักษะพื้นฐานที่ System Administrator ต้องรู้ ไม่ว่าจะเป็นการดู IP Address ที่กำหนดอยู่, การเปิด/ปิด Interface, การตั้งชื่อ Interface หรือการ Troubleshoot ปัญหาเครือข่ายเบื้องต้น เครื่องมือหลักที่ใช้ในปัจจุบันคือ ip command จาก iproute2 ซึ่งทดแทน ifconfig รุ่นเก่าที่เลิกใช้งานแล้วบน Distribution สมัยใหม่

บทความนี้อธิบายการจัดการ Network Interface บน Linux ตั้งแต่การดูข้อมูล Interface, การกำหนด IP Address ชั่วคราวและถาวร, การเปิด/ปิด Interface, การตั้งค่า Bonding และ VLAN รวมถึงการใช้งาน NetworkManager และ systemd-networkd ซึ่งเป็น Service หลักสำหรับจัดการเครือข่ายบน Linux รุ่นใหม่

ดูข้อมูล Network Interface

ip command เป็นเครื่องมือหลักสำหรับจัดการ Network Interface บน Linux รุ่นใหม่ ครอบคลุมทั้งการดูข้อมูล IP, Route, และ Link

# ดู Interface ทั้งหมดพร้อม IP Address
ip addr show
ip a           # ย่อ

# ดู Interface เฉพาะตัว
ip addr show eth0
ip addr show lo    # Loopback

# ดูเฉพาะ IPv4
ip -4 addr show

# ดูเฉพาะ IPv6
ip -6 addr show

# ดู Link State (UP/DOWN, MAC address, MTU)
ip link show
ip link show eth0

# ดู Route Table
ip route show
ip route show default    # ดู Default Gateway

# ดู ARP Cache
ip neigh show

# ดูสถิติ Interface (Packets, Errors, Drops)
ip -s link show eth0

อ่านผลลัพธ์ ip addr

# ตัวอย่าง Output ของ ip addr show
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:ab:cd:ef brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic eth0
       valid_lft 86400sec preferred_lft 86400sec
    inet6 fe80::5054:ff:feab:cdef/64 scope link
       valid_lft forever preferred_lft forever

# คำอธิบาย
# eth0                    — ชื่อ Interface
# <UP,LOWER_UP>           — Interface เปิดอยู่และมีสัญญาณ Physical
# mtu 1500                — Maximum Transmission Unit
# state UP                — สถานะ UP (พร้อมรับส่งข้อมูล)
# link/ether              — MAC Address
# inet 192.168.1.100/24   — IPv4 Address / Subnet Mask (CIDR)
# brd 192.168.1.255       — Broadcast Address
# dynamic                 — ได้รับจาก DHCP
# scope global            — ใช้งานได้ทั่วทั้งเครือข่าย
# scope link              — ใช้งานได้เฉพาะใน Local Link (Link-local)

กำหนด IP Address ชั่วคราว

การกำหนด IP Address ด้วย ip command โดยตรงจะมีผลชั่วคราวเท่านั้น — หายไปหลัง Reboot เหมาะสำหรับการทดสอบ

# เพิ่ม IP Address บน Interface
sudo ip addr add 192.168.1.200/24 dev eth0

# ลบ IP Address
sudo ip addr del 192.168.1.200/24 dev eth0

# เพิ่ม IP ที่สอง (Secondary IP / Alias)
sudo ip addr add 10.0.0.50/24 dev eth0

# ตั้ง Default Gateway
sudo ip route add default via 192.168.1.1

# ลบ Default Gateway
sudo ip route del default

# เพิ่ม Route ไปยัง Network เฉพาะ
sudo ip route add 10.10.0.0/16 via 192.168.1.254

# เปิด/ปิด Interface
sudo ip link set eth0 up
sudo ip link set eth0 down

# เปลี่ยน MTU
sudo ip link set eth0 mtu 9000    # Jumbo Frames

# Flush IP ทั้งหมดออกจาก Interface
sudo ip addr flush dev eth0

ตั้งค่า IP ถาวรด้วย NetworkManager (nmcli)

NetworkManager เป็น Service ที่ใช้งานกันแพร่หลายบน RHEL, Rocky Linux, Ubuntu Desktop และ Server รุ่นใหม่ การตั้งค่าผ่าน nmcli จะ Persist ข้ามการ Reboot

# ดูสถานะ NetworkManager
systemctl status NetworkManager

# ดู Connection ทั้งหมด
nmcli connection show

# ดู Device ทั้งหมด
nmcli device show
nmcli device status

# ดูรายละเอียด Interface เฉพาะ
nmcli device show eth0

# ดู Connection ที่ Active อยู่
nmcli connection show --active
# ตั้ง Static IP บน Connection ที่มีอยู่แล้ว
# (ชื่อ Connection มักเป็นชื่อ Interface เช่น "eth0" หรือ "Wired connection 1")
nmcli connection modify "eth0" \
    ipv4.method manual \
    ipv4.addresses "192.168.1.100/24" \
    ipv4.gateway "192.168.1.1" \
    ipv4.dns "8.8.8.8,8.8.4.4"

# Reload การตั้งค่า
nmcli connection down "eth0" && nmcli connection up "eth0"

# สร้าง Connection ใหม่แบบ Static IP
nmcli connection add type ethernet ifname eth1 con-name "static-eth1" \
    ipv4.method manual \
    ipv4.addresses "10.0.0.10/24" \
    ipv4.gateway "10.0.0.1" \
    ipv4.dns "1.1.1.1"

# เปลี่ยนจาก Static กลับเป็น DHCP
nmcli connection modify "eth0" ipv4.method auto ipv4.addresses "" ipv4.gateway "" ipv4.dns ""
nmcli connection up "eth0"

# ลบ Connection
nmcli connection delete "static-eth1"

ตั้งค่า IP ถาวรด้วย Netplan (Ubuntu)

Ubuntu 18.04 ขึ้นไปใช้ Netplan เป็น Network Configuration Layer ที่เขียนด้วย YAML และ Render ออกมาเป็น Configuration ของ NetworkManager หรือ systemd-networkd

# ไฟล์ Config อยู่ที่ /etc/netplan/
ls /etc/netplan/
# ปกติมีไฟล์ชื่อเช่น: 00-installer-config.yaml

# ดู Config ปัจจุบัน
cat /etc/netplan/00-installer-config.yaml
# ตัวอย่าง Netplan Config แบบ Static IP
# /etc/netplan/00-installer-config.yaml

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
      addresses:
        - 192.168.1.100/24
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses:
          - 8.8.8.8
          - 8.8.4.4
# ตัวอย่าง Netplan Config แบบ DHCP
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: true

# ตัวอย่างมี 2 Interface (eth0 static, eth1 DHCP)
network:
  version: 2
  ethernets:
    eth0:
      dhcp4: false
      addresses: [192.168.1.10/24]
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8]
    eth1:
      dhcp4: true
# ทดสอบ Config โดยไม่ Apply จริง (ลอง 120 วินาที แล้ว Rollback อัตโนมัติ)
sudo netplan try

# Apply Config
sudo netplan apply

# Debug ปัญหา Netplan
sudo netplan --debug apply

ชื่อ Interface บน Linux รุ่นใหม่ (Predictable Network Interface Names)

Linux รุ่นใหม่ใช้ชื่อ Interface แบบ Predictable ที่อ้างอิงจาก Hardware Location แทนชื่อแบบ eth0, eth1 เพื่อให้ชื่อคงที่ไม่เปลี่ยนแปลงหลัง Reboot

# รูปแบบชื่อ Interface ที่พบบ่อย:
# en   — Ethernet
# wl   — Wireless LAN
# ww   — Wireless WAN
#
# ตามด้วย:
# o<index>        — onboard (เช่น eno1)
# p<slot>s<port> — PCI slot (เช่น enp3s0, enp0s3)
# s<slot>        — hotplug slot (เช่น ens3, ens5)
#
# ตัวอย่าง:
# eno1     — Ethernet onboard port 1 (Dell, HP servers)
# enp0s3   — Ethernet PCI bus 0, slot 3 (VirtualBox)
# ens3     — Ethernet slot 3 (KVM/QEMU VMs)
# eth0     — ชื่อแบบเก่า (ใช้ได้ถ้า Rename หรือ Disable predictable naming)

# ดูชื่อ Interface ทั้งหมดในระบบ
ls /sys/class/net/

# ดูว่า Interface ผูกกับ PCI Device ใด
udevadm info -q all /sys/class/net/eth0 | grep ID_NET

ตั้งค่า Network Interface ใน systemd-networkd

systemd-networkd เป็นทางเลือกสำหรับ Server ที่ไม่ใช้ NetworkManager โดยตรง มักพบใน Ubuntu Server ที่ตั้งค่าผ่าน Netplan ด้วย renderer: networkd

# เปิดใช้งาน systemd-networkd
sudo systemctl enable --now systemd-networkd
sudo systemctl enable --now systemd-resolved

# ไฟล์ Config อยู่ที่ /etc/systemd/network/
# ตัวอย่างไฟล์ /etc/systemd/network/10-eth0.network

[Match]
Name=eth0

[Network]
Address=192.168.1.100/24
Gateway=192.168.1.1
DNS=8.8.8.8
DNS=8.8.4.4

# ตัวอย่างแบบ DHCP
[Match]
Name=eth0

[Network]
DHCP=yes

# Reload Config
sudo networkctl reload
sudo networkctl status eth0

Network Bonding — รวม Interface เพื่อ Redundancy หรือ Speed

Network Bonding รวม Interface หลายตัวเข้าด้วยกัน มี Mode หลักคือ active-backup (Failover), balance-rr (Round-robin load balance) และ 802.3ad (LACP — ต้องการ Switch รองรับ)

# ตัวอย่าง Bonding ด้วย nmcli (active-backup)
# สร้าง Bond Interface
nmcli connection add type bond con-name bond0 ifname bond0 \
    bond.options "mode=active-backup,miimon=100"

# เพิ่ม Slave Interface เข้า Bond
nmcli connection add type ethernet con-name bond0-slave1 ifname eth0 master bond0
nmcli connection add type ethernet con-name bond0-slave2 ifname eth1 master bond0

# กำหนด IP ให้ Bond Interface
nmcli connection modify bond0 ipv4.method manual \
    ipv4.addresses "192.168.1.100/24" \
    ipv4.gateway "192.168.1.1"

# เปิด Bond Interface
nmcli connection up bond0

# ตรวจสอบสถานะ Bond
cat /proc/net/bonding/bond0
# Bond Modes ที่ใช้บ่อย:
# mode=0 (balance-rr)    — Round-robin ส่งข้อมูลสลับ Interface
# mode=1 (active-backup) — ใช้ Interface เดียว อีกตัว Standby (Failover)
# mode=4 (802.3ad/LACP)  — Link Aggregation ต้องการ Managed Switch
# mode=5 (balance-tlb)   — Adaptive Transmit Load Balancing ไม่ต้อง Switch พิเศษ
# mode=6 (balance-alb)   — Adaptive Load Balancing ครบทั้ง Transmit + Receive

VLAN Interface

# สร้าง VLAN Interface ด้วย ip command (ชั่วคราว)
sudo ip link add link eth0 name eth0.100 type vlan id 100
sudo ip addr add 192.168.100.1/24 dev eth0.100
sudo ip link set eth0.100 up

# ลบ VLAN Interface
sudo ip link delete eth0.100

# สร้าง VLAN ถาวรด้วย nmcli
nmcli connection add type vlan con-name vlan100 ifname eth0.100 \
    dev eth0 id 100 \
    ipv4.method manual \
    ipv4.addresses "192.168.100.1/24"
nmcli connection up vlan100

# ตัวอย่าง Netplan VLAN
network:
  version: 2
  vlans:
    eth0.100:
      id: 100
      link: eth0
      addresses: [192.168.100.1/24]

ตรวจสอบและ Debug Network Interface

# ดูสถิติ Packet ทุก Interface
ip -s link

# ดู Interface ที่มีปัญหา (error/drop สูง)
ip -s link show eth0
# RX errors, dropped = ปัญหาฝั่งรับ
# TX errors, dropped = ปัญหาฝั่งส่ง

# ตรวจ Physical Link State
cat /sys/class/net/eth0/operstate    # up หรือ down
cat /sys/class/net/eth0/speed        # ความเร็ว Mbps
cat /sys/class/net/eth0/duplex       # full หรือ half

# ดู MAC Address
cat /sys/class/net/eth0/address
ip link show eth0 | grep ether

# ดู Driver ที่ใช้
ethtool -i eth0

# ดูการตั้งค่า Speed/Duplex/Auto-negotiation
ethtool eth0

# ดู Interface Statistics แบบละเอียด
ethtool -S eth0

# ทดสอบ Connectivity
ping -c 4 8.8.8.8
ping -I eth0 -c 4 8.8.8.8    # ระบุ Interface

# ดู Routing Decision สำหรับ IP ปลายทาง
ip route get 8.8.8.8

Script ตรวจสอบ Network Interface

#!/bin/bash
# /usr/local/bin/net-check.sh
# ตรวจสอบสถานะ Network Interface และแจ้งเตือนถ้ามีปัญหา

ALERT_EMAIL="[email protected]"

for iface in $(ls /sys/class/net/ | grep -v lo); do
    state=$(cat /sys/class/net/$iface/operstate 2>/dev/null)

    if [ "$state" != "up" ]; then
        echo "Interface $iface is $state on $(hostname)" \
            | mail -s "[Alert] Network Interface Down: $iface" "$ALERT_EMAIL"
        echo "WARNING: $iface is $state"
    else
        # ตรวจ Error Rate
        errors=$(ip -s link show $iface | awk '/RX:/{getline; print $3}')
        echo "OK: $iface is up (RX errors: $errors)"
    fi
done

สรุป

ip addr และ ip link เป็นคำสั่งหลักสำหรับดูและจัดการ Network Interface บน Linux รุ่นใหม่แทน ifconfig ที่เลิกใช้แล้ว การตั้งค่าชั่วคราวทำได้ด้วย ip addr add และ ip route add ส่วนการตั้งค่าถาวรขึ้นอยู่กับ Distribution — Ubuntu ใช้ Netplan, RHEL/Rocky ใช้ NetworkManager ผ่าน nmcli Network Bonding รวม Interface เพื่อ Redundancy หรือ Load Balancing ส่วน VLAN แบ่ง Network Logical บน Interface เดียวกัน การ Debug ใช้ ip -s link และ ethtool เพื่อดู Error Statistics

แนะนำบริการ DE

การตั้งค่า Network Interface บน Server จริงต้องการสิทธิ์ Root และ Console Access สำหรับกรณีที่ Network ขัดข้องระหว่างการตั้งค่า Cloud VPS ของ DE ให้ Root Access เต็มรูปแบบพร้อม Out-of-band Console ผ่าน Web Panel ช่วยให้กู้คืนการตั้งค่าเครือข่ายได้แม้ SSH จะใช้งานไม่ได้ชั่วคราว

สำหรับผู้ที่ต้องการโฮสต์เว็บไซต์โดยไม่ต้องจัดการ Network Configuration เอง Cloud Hosting ของ DE มีทีม Support พร้อมดูแลระบบเครือข่ายให้ครบวงจร