การกำหนด Static IP บน Cloud VPS เป็นขั้นตอนสำคัญหลังจากสร้าง Server ใหม่ เพื่อให้ IP Address คงที่ไม่เปลี่ยนแปลงหลัง Reboot และสามารถชี้ Domain, ตั้งค่า Firewall, หรือกำหนด DNS Record ได้อย่างถูกต้อง Cloud VPS ส่วนใหญ่จะกำหนด IP ผ่าน DHCP ตั้งแต่แรก และ IP นั้นอาจเปลี่ยนได้ถ้า Lease หมดหรือมีการ Reboot
บทความนี้อธิบายการตั้งค่า Static IP บน Linux ทั้ง Ubuntu (ผ่าน Netplan) และ RHEL/Rocky Linux (ผ่าน NetworkManager) ตั้งแต่การหาข้อมูล IP เดิม, การแก้ไขไฟล์ Config, การตรวจสอบผล รวมถึงการตั้งค่า DNS และ Default Route ให้ถูกต้อง
ก่อนเริ่ม — รวบรวมข้อมูล Network
ก่อนตั้งค่า Static IP ต้องรู้ข้อมูลเครือข่ายจาก Provider หรือจากระบบปัจจุบัน เพราะถ้าตั้งค่าผิดจะ SSH เข้า Server ไม่ได้
# ดู IP Address ปัจจุบัน
ip addr show
ip -4 addr show # เฉพาะ IPv4
# ดู Default Gateway
ip route show default
# ตัวอย่าง Output:
# default via 192.168.1.1 dev eth0 proto dhcp src 192.168.1.100 metric 100
# → Gateway = 192.168.1.1, Interface = eth0
# ดู DNS Server ปัจจุบัน
cat /etc/resolv.conf
# หรือ
resolvectl status # systemd-resolved
# ดู Interface ที่ใช้งาน
ip link show
# หาชื่อ Interface เช่น eth0, ens3, enp0s3, ens5
# สรุปข้อมูลที่ต้องการ:
# IP Address: 192.168.1.100 (IP ที่จะตั้งแบบ Static)
# Subnet Mask: /24 (CIDR) หรือ 255.255.255.0
# Gateway: 192.168.1.1
# DNS: 8.8.8.8, 8.8.4.4
# Interface: eth0
ตั้งค่า Static IP บน Ubuntu (Netplan)
Ubuntu 18.04 ขึ้นไปใช้ Netplan เป็นระบบจัดการ Network Configuration ไฟล์ Config อยู่ใน /etc/netplan/
# ดูไฟล์ Netplan ที่มีอยู่
ls /etc/netplan/
# ปกติมีไฟล์เช่น: 00-installer-config.yaml หรือ 50-cloud-init.yaml
# ดู Config ปัจจุบัน (ตัวอย่าง DHCP)
cat /etc/netplan/00-installer-config.yaml
# network:
# version: 2
# ethernets:
# eth0:
# dhcp4: true
แก้ไขไฟล์ Netplan เพื่อเปลี่ยนจาก DHCP เป็น Static:
# แก้ไขไฟล์ (ใช้ nano หรือ vi)
sudo nano /etc/netplan/00-installer-config.yaml
# เนื้อหาที่ต้องการ (แทนที่ค่าด้วยข้อมูลจริงของ Server)
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
# ตรวจสอบ Syntax ของ YAML ก่อน Apply
sudo netplan generate
# ทดสอบ 120 วินาที (Rollback อัตโนมัติถ้า Network หลุด)
sudo netplan try
# ถ้าทุกอย่างถูกต้อง → กด Enter เพื่อ Confirm
# ถ้า Network หลุด → รอ 120 วินาที ระบบ Rollback อัตโนมัติ
# Apply Config ถาวร
sudo netplan apply
# ตรวจสอบผลลัพธ์
ip addr show eth0
ip route show
กรณี Cloud-init Override
Cloud VPS บางรายใช้ Cloud-init ซึ่งอาจ Reset Netplan Config กลับเป็น DHCP ทุกครั้งที่ Reboot ต้องปิดการทำงานของ Cloud-init ในส่วนของ Network
# ตรวจสอบว่ามี Cloud-init config หรือไม่
ls /etc/cloud/cloud.cfg.d/
# ปิด Cloud-init ไม่ให้ Override Network Config
sudo bash -c 'echo "network: {config: disabled}" > /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg'
# ตรวจสอบ
cat /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
# จากนั้น Apply Netplan ใหม่
sudo netplan apply
ตั้งค่า Static IP บน RHEL / Rocky Linux / AlmaLinux (NetworkManager)
RHEL-based Distribution ใช้ NetworkManager เป็นหลัก สามารถตั้งค่าผ่าน nmcli หรือแก้ไขไฟล์ .nmconnection โดยตรง
วิธีที่ 1 — ใช้ nmcli (แนะนำ)
# ดู Connection ที่มีอยู่
nmcli connection show
# ดูชื่อ Connection (เช่น "eth0", "ens3", "System eth0")
nmcli connection show --active
# ตั้ง Static IP (แทนที่ "eth0" ด้วยชื่อ Connection จริง)
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" \
ipv4.dns-search ""
# Restart Connection เพื่อ Apply
nmcli connection down "eth0" && nmcli connection up "eth0"
# ตรวจสอบผลลัพธ์
ip addr show
nmcli connection show "eth0" | grep -E "ipv4|IP4"
วิธีที่ 2 — แก้ไขไฟล์ .nmconnection
# ไฟล์ Connection อยู่ที่
ls /etc/NetworkManager/system-connections/
# ชื่อไฟล์เช่น eth0.nmconnection หรือ ens3.nmconnection
# ดูเนื้อหาไฟล์เดิม
cat /etc/NetworkManager/system-connections/eth0.nmconnection
# ตัวอย่างเนื้อหาที่ต้องการสำหรับ Static IP
[connection]
id=eth0
type=ethernet
interface-name=eth0
[ethernet]
[ipv4]
method=manual
address1=192.168.1.100/24,192.168.1.1
dns=8.8.8.8;8.8.4.4;
[ipv6]
method=disabled
# หลังแก้ไขไฟล์ ต้อง Reload
sudo nmcli connection reload
sudo nmcli connection up eth0
ตั้งค่า Static IP บน Debian / Debian-based Server รุ่นเก่า
Debian และ Ubuntu รุ่นเก่าบางระบบยังใช้ไฟล์ /etc/network/interfaces แทน Netplan
# ดูไฟล์ /etc/network/interfaces
cat /etc/network/interfaces
# ตัวอย่าง Config แบบ DHCP เดิม
# auto eth0
# iface eth0 inet dhcp
# เปลี่ยนเป็น Static IP
# แก้ไขไฟล์:
sudo nano /etc/network/interfaces
# เนื้อหาใหม่:
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 8.8.4.4
# Restart Networking
sudo systemctl restart networking
# หรือ
sudo ifdown eth0 && sudo ifup eth0
# ตรวจสอบ
ip addr show eth0
ตั้งค่า DNS อย่างถาวร
บน Server ที่ใช้ systemd-resolved (Ubuntu รุ่นใหม่, RHEL 9+) การแก้ไข /etc/resolv.conf โดยตรงจะถูก Overwrite อัตโนมัติ ต้องตั้งค่าผ่านช่องทางที่ถูกต้อง
# ตรวจสอบว่าใช้ systemd-resolved หรือไม่
ls -la /etc/resolv.conf
# ถ้าเป็น symlink ไปที่ /run/systemd/resolve/stub-resolv.conf → ใช้ systemd-resolved
# ดู DNS ที่ใช้งานอยู่ (systemd-resolved)
resolvectl status
resolvectl dns
# ตั้ง DNS ผ่าน Netplan (Ubuntu) — ใส่ใน nameservers section
# ตั้ง DNS ผ่าน nmcli (RHEL)
nmcli connection modify "eth0" ipv4.dns "1.1.1.1,8.8.8.8"
nmcli connection up "eth0"
# ตั้ง DNS แบบ Global สำหรับ systemd-resolved
sudo nano /etc/systemd/resolved.conf
# [Resolve]
# DNS=8.8.8.8 8.8.4.4
# FallbackDNS=1.1.1.1
sudo systemctl restart systemd-resolved
# ตรวจสอบ DNS Resolution
nslookup google.com
dig google.com @8.8.8.8
ตรวจสอบหลังตั้งค่า Static IP
# 1. ตรวจสอบ IP Address
ip addr show
# 2. ตรวจสอบ Default Route
ip route show
# ต้องมี: default via [gateway] dev [interface]
# 3. Ping Gateway
ping -c 3 192.168.1.1
# 4. Ping DNS
ping -c 3 8.8.8.8
# 5. ทดสอบ DNS Resolution
ping -c 3 google.com
nslookup google.com
# 6. ตรวจสอบหลัง Reboot
sudo reboot
# หลัง Reboot ให้รัน ip addr show อีกครั้งเพื่อยืนยัน
# 7. ตรวจสอบว่า Config ถาวรจริง
# Ubuntu: ดูไฟล์ Netplan
cat /etc/netplan/00-installer-config.yaml
# RHEL: ดูค่าใน nmcli
nmcli connection show "eth0" | grep ipv4
Troubleshooting — กรณีที่พบบ่อย
# ปัญหา: Network หลุดหลังตั้งค่า
# สาเหตุที่พบบ่อย:
# 1. IP Address ชนกับ Device อื่นใน Network
# 2. Gateway ผิด
# 3. Subnet Mask ผิด (เช่น /32 แทน /24)
# 4. ชื่อ Interface ผิด (ใช้ eth0 แต่จริงๆ เป็น ens3)
# วิธีแก้: ใช้ Console ของ VPS Provider
# แล้วรันคำสั่งต่อไปนี้เพื่อตรวจสอบ
ip addr show
ip route show
cat /etc/netplan/*.yaml # Ubuntu
nmcli connection show # RHEL
# ปัญหา: Netplan apply ไม่ได้เพราะ YAML ผิด
sudo netplan generate # ดู error message
# YAML ต้องใช้ spaces ไม่ใช่ tabs สำหรับ indent
# ปัญหา: IP เดิมยังค้างอยู่หลัง Apply
# บางครั้งต้อง Flush IP เก่าออกก่อน
sudo ip addr flush dev eth0
sudo netplan apply
# ปัญหา: DNS ไม่ทำงาน
# ตรวจสอบว่า /etc/resolv.conf มี nameserver
cat /etc/resolv.conf
# ถ้าไม่มี หรือมีแต่ผิด — ตั้งค่า DNS ใหม่ผ่าน Netplan/nmcli
Script ตั้งค่า Static IP อัตโนมัติ (Ubuntu)
#!/bin/bash
# set-static-ip.sh
# ใช้บน Ubuntu 20.04/22.04
# Usage: sudo ./set-static-ip.sh eth0 192.168.1.100 24 192.168.1.1
IFACE=$1
IP=$2
PREFIX=$3
GW=$4
DNS1="8.8.8.8"
DNS2="8.8.4.4"
if [ -z "$IFACE" ] || [ -z "$IP" ] || [ -z "$PREFIX" ] || [ -z "$GW" ]; then
echo "Usage: $0 <interface> <ip> <prefix> <gateway>"
echo "Example: $0 eth0 192.168.1.100 24 192.168.1.1"
exit 1
fi
NETPLAN_FILE="/etc/netplan/99-static-$IFACE.yaml"
cat > "$NETPLAN_FILE" <<EOF
network:
version: 2
renderer: networkd
ethernets:
$IFACE:
dhcp4: false
addresses:
- $IP/$PREFIX
routes:
- to: default
via: $GW
nameservers:
addresses:
- $DNS1
- $DNS2
EOF
chmod 600 "$NETPLAN_FILE"
netplan apply
echo "Static IP $IP/$PREFIX configured on $IFACE"
echo "Gateway: $GW, DNS: $DNS1, $DNS2"
สรุป
การตั้งค่า Static IP บน Cloud VPS ขึ้นอยู่กับ Distribution — Ubuntu ใช้ Netplan โดยแก้ไขไฟล์ YAML ใน /etc/netplan/ และรัน netplan apply ส่วน RHEL/Rocky ใช้ nmcli connection modify ที่ง่ายและปลอดภัยกว่าการแก้ไขไฟล์ตรง ก่อนตั้งค่าควรรวบรวมข้อมูล IP, Gateway, และ Subnet จากระบบปัจจุบันหรือจาก Control Panel ของ Provider และควรทดสอบผ่าน netplan try บน Ubuntu เพื่อให้ Rollback อัตโนมัติถ้าเกิดปัญหา หลังตั้งค่าแล้ว Reboot แล้วตรวจสอบอีกครั้งเพื่อยืนยันว่า Config ถาวรจริง
แนะนำบริการ DE
การตั้งค่า Static IP บน Cloud VPS ต้องการสิทธิ์ Root และ Console Access สำรอง Cloud VPS ของ DE ให้ Root Access เต็มรูปแบบพร้อม VNC Console ผ่าน Web Panel เพื่อใช้กู้คืนในกรณีที่ Network Config ผิดพลาดและ SSH ใช้งานไม่ได้ รองรับทั้ง Ubuntu และ Rocky Linux พร้อมใช้งานทันที
หากต้องการโฮสต์เว็บโดยไม่ต้องจัดการ IP Configuration เอง Cloud Hosting ของ DE มีระบบจัดการ IP และ DNS ให้อัตโนมัติผ่าน Control Panel

