ตั้งค่า Netplan บน Ubuntu VPS กำหนด Static IP และ Network
ตั้งแต่ Ubuntu 18.04 เป็นต้นมา Canonical เปลี่ยนระบบจัดการ network configuration มาใช้ Netplan แทน /etc/network/interfaces เดิม ทำให้หลายคนที่ย้ายจาก Ubuntu รุ่นเก่าหรือมาจาก Debian บริสุทธิ์ต้องปรับตัวใหม่ทั้งหมด Netplan ใช้ไฟล์ YAML เป็น config และ generate ไฟล์ให้ backend renderer อย่าง NetworkManager หรือ systemd-networkd ทำงานต่อ ซึ่งทำให้มีความยืดหยุ่นสูงมากในการกำหนด bonding, VLAN, bridge และ routing พิเศษต่างๆ แต่ถ้าไม่รู้ syntax YAML ที่ถูกต้อง ก็อาจทำให้ network ล่มได้ง่ายๆ
ทำความรู้จัก Netplan Architecture
Netplan ทำงาน 2 ชั้น คือ configuration layer (YAML ที่เราเขียน) และ renderer ที่ไปจัดการ network จริง:
| Component | หน้าที่ | ใช้เมื่อ |
|---|---|---|
| Netplan YAML | กำหนด config ส่วนกลาง | เสมอ |
| systemd-networkd | renderer สำหรับ server | Ubuntu Server (default) |
| NetworkManager | renderer สำหรับ desktop | Ubuntu Desktop / cloud-init |
ไฟล์ config ของ Netplan อยู่ที่ /etc/netplan/ — มักมีชื่อเป็น 00-installer-config.yaml หรือ 50-cloud-init.yaml ขึ้นอยู่กับว่า Ubuntu ถูก install มาอย่างไร
# ดูไฟล์ config ที่มีอยู่
ls /etc/netplan/
# ดู config ปัจจุบัน
cat /etc/netplan/*.yaml
ดูชื่อ Network Interface
ก่อน config ต้องรู้ชื่อ interface ก่อน Ubuntu ใช้ predictable network interface names เช่น ens3, eth0, enp1s0 แทนชื่อเดิม:
# แสดงชื่อ interface ทั้งหมด
ip link show
# หรือใช้ ip address
ip addr
# แสดงสถานะแบบสั้น
ip -br link
ens3 หรือ eth0 ส่วน VPS ที่ใช้ VMware/Hyper-V อาจได้ ens160 หรือ eth0 ขึ้นอยู่กับ driverกำหนด Static IP ด้วย Netplan
กรณีที่ใช้บ่อยที่สุดบน VPS คือการกำหนด Static IP แทน DHCP ตัวอย่างด้านล่างสำหรับ interface ชื่อ ens3:
sudo nano /etc/netplan/00-installer-config.yaml
เนื้อหาไฟล์ (ระวัง indentation ต้องใช้ space ไม่ใช่ tab):
network:
version: 2
renderer: networkd
ethernets:
ens3:
dhcp4: false
dhcp6: false
addresses:
- 203.0.113.10/24
routes:
- to: default
via: 203.0.113.1
nameservers:
addresses:
- 1.1.1.1
- 8.8.8.8
search:
- example.com
บันทึกไฟล์แล้วใช้คำสั่งเหล่านี้:
# ทดสอบ syntax ก่อน (ไม่ apply จริง)
sudo netplan generate
# ทดสอบแบบ apply แต่ rollback ใน 120 วินาทีถ้าไม่ confirm
sudo netplan try
# apply ถาวร
sudo netplan apply
netplan try คือคำสั่งที่ปลอดภัยที่สุด — ถ้า config ผิดและ SSH หลุด ระบบจะ rollback กลับอัตโนมัติใน 120 วินาที ทำให้ไม่ต้องกลัว network ล่มเพิ่ม IP หลายตัวบน Interface เดียว
VPS บางตัวมีหลาย IP (เช่น dedicated IP เพิ่มเติม) สามารถใส่ได้ใน addresses:
network:
version: 2
renderer: networkd
ethernets:
ens3:
dhcp4: false
addresses:
- 203.0.113.10/24
- 203.0.113.11/24
- 203.0.113.12/24
routes:
- to: default
via: 203.0.113.1
nameservers:
addresses:
- 1.1.1.1
- 8.8.8.8
ตั้งค่า DHCP (สำหรับ VPS Cloud ที่ IP เปลี่ยนได้)
ถ้า VPS ใช้ DHCP ต้องการ config แบบนี้:
network:
version: 2
renderer: networkd
ethernets:
ens3:
dhcp4: true
dhcp6: false
dhcp4-overrides:
use-dns: true
use-routes: true
ตั้งค่า IPv6 ร่วมกับ IPv4
เพิ่ม IPv6 address และ gateway ใน config เดียวกัน:
network:
version: 2
renderer: networkd
ethernets:
ens3:
dhcp4: false
dhcp6: false
addresses:
- 203.0.113.10/24
- 2001:db8::10/64
routes:
- to: default
via: 203.0.113.1
- to: ::/0
via: 2001:db8::1
nameservers:
addresses:
- 1.1.1.1
- 2606:4700:4700::1111
ตั้งค่า Bridge สำหรับ Virtual Machine / LXC
ถ้าใช้ VPS เป็น hypervisor สำหรับ LXC หรือ KVM VM ต้องสร้าง bridge interface:
network:
version: 2
renderer: networkd
ethernets:
ens3:
dhcp4: false
bridges:
br0:
interfaces: [ens3]
dhcp4: false
addresses:
- 203.0.113.10/24
routes:
- to: default
via: 203.0.113.1
nameservers:
addresses:
- 1.1.1.1
- 8.8.8.8
parameters:
stp: false
forward-delay: 0
ตั้งค่า VLAN บน Netplan
สำหรับ network ที่ต้องการ VLAN tagging:
network:
version: 2
renderer: networkd
ethernets:
ens3:
dhcp4: false
vlans:
vlan100:
id: 100
link: ens3
addresses:
- 10.0.100.10/24
vlan200:
id: 200
link: ens3
addresses:
- 10.0.200.10/24
ตรวจสอบว่า Netplan ทำงานถูกต้อง
# ดูสถานะ network interfaces
ip addr show
# ดู routing table
ip route show
# ทดสอบ connectivity
ping -c 4 1.1.1.1
# ทดสอบ DNS resolution
dig google.com @1.1.1.1
# ดู systemd-networkd status
systemctl status systemd-networkd
# ดู network link status ทั้งหมด
networkctl status
networkctl status แสดงสถานะ interface ทุกตัวพร้อม online/offline state ใช้ debug ได้ดีมาก โดยเฉพาะเมื่อ interface ขึ้นแล้วแต่ network ยังใช้งานไม่ได้แก้ปัญหาที่พบบ่อย
Error: "Invalid YAML — found character '\t' that cannot start any token"
ปัญหา indentation ใช้ tab แทน space ซึ่ง YAML ไม่รองรับ:
# ตรวจสอบว่ามี tab ใน file หรือไม่
cat -A /etc/netplan/00-installer-config.yaml | grep '\^I'
# แก้ไขโดยแทนที่ tab ด้วย space (2 หรือ 4 spaces)
sed -i 's/\t/ /g' /etc/netplan/00-installer-config.yaml
# ตรวจสอบ syntax อีกครั้ง
sudo netplan generate
Error: "Permissions for /etc/netplan/... are too open"
ไฟล์ Netplan ต้องมี permission เฉพาะ root อ่านได้:
# กำหนด permission ที่ถูกต้อง
sudo chmod 600 /etc/netplan/*.yaml
sudo chown root:root /etc/netplan/*.yaml
# ลอง apply อีกครั้ง
sudo netplan apply
Error: "networkd-dispatcher: Failed to connect to dbus"
systemd-networkd ไม่ได้ start หรือ dbus ไม่พร้อม:
# เริ่ม systemd-networkd
sudo systemctl enable --now systemd-networkd
# ตรวจสอบ dbus
systemctl status dbus
# Restart networkd-dispatcher
sudo systemctl restart networkd-dispatcher
Error: network ล่มหลัง netplan apply — SSH หลุดและกลับไม่ได้
ถ้าไม่ได้ใช้ netplan try และ config ผิด ต้องเข้า VPS console โดยตรง:
# เข้า VPS console จาก control panel ของ provider
# แล้วดู error ใน journalctl
sudo journalctl -u systemd-networkd -n 50
# restore config เดิม
sudo cp /etc/netplan/00-installer-config.yaml.bak /etc/netplan/00-installer-config.yaml
sudo netplan apply
Error: "route already exists" หลัง netplan apply
มี default route ซ้ำกันอยู่แล้ว ต้องลบออกก่อน:
# ดู routing table ปัจจุบัน
ip route show
# ลบ route ที่ซ้ำ
sudo ip route del default
# Apply netplan ใหม่
sudo netplan apply
คำถามที่พบบ่อย
ต่างจาก /etc/network/interfaces อย่างไร?
Netplan เป็น abstraction layer ที่นั่งอยู่บน renderer (systemd-networkd หรือ NetworkManager) แทนที่จะ configure network โดยตรง ข้อดีคือ config เดียวใช้ได้ทั้ง server และ desktop และรองรับ feature สมัยใหม่อย่าง bonding, VLAN, WireGuard ได้ง่ายกว่า ส่วน /etc/network/interfaces ยังใช้ได้บน Debian และ Ubuntu แต่เป็น legacy ที่ Ubuntu ไม่แนะนำอีกต่อไปตั้งแต่ 18.04
cloud-init เขียนทับ Netplan config ที่ตั้งไว้ทุก reboot ทำอย่างไร?
VPS หลายเจ้าใช้ cloud-init generate ไฟล์ /etc/netplan/50-cloud-init.yaml ตอน boot ทำให้ config ที่แก้ถูกเขียนทับ แก้ได้ด้วยการ disable cloud-init network:
# สร้างไฟล์ disable cloud-init network
echo "network: {config: disabled}" | sudo tee /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
# หรือสร้าง config ใหม่แยกต่างหากให้มีชื่อก่อน (ตัวเลขน้อยกว่า 50)
sudo nano /etc/netplan/01-custom.yaml
Netplan รองรับ WireGuard ได้ไหม?
ได้ ตั้งแต่ Netplan 0.99+ รองรับ WireGuard tunnel โดยตรง:
network:
version: 2
tunnels:
wg0:
mode: wireguard
key: /etc/wireguard/privatekey
addresses:
- 10.0.0.1/24
peers:
- keys:
public: <peer-public-key>
allowed-ips: [0.0.0.0/0]
endpoint: vpn.example.com:51820