VPN network security tunnel

ติดตั้ง WireGuard VPN บน VPS ใน 10 นาที

มิถุนายน 2026 · อ่าน 6 นาที

WireGuard คือโปรโตคอล VPN รุ่นใหม่ที่ถูกออกแบบมาให้เร็ว เบา และปลอดภัยกว่า OpenVPN หรือ IPSec อย่างเห็นได้ชัด โค้ดทั้งหมดมีเพียงราว 4,000 บรรทัด เทียบกับ OpenVPN ที่มีหลักแสนบรรทัด ทำให้ตรวจสอบความปลอดภัยง่ายกว่า และ overhead น้อยกว่ามาก บทความนี้จะพาคุณติดตั้ง WireGuard VPN Server บน VPS Linux ตั้งแต่ต้นจนใช้งานได้จริงภายใน 10 นาที

ทำไมต้องเลือก WireGuard?

ก่อนลงมือติดตั้ง ขอสรุปเหตุผลที่ WireGuard ได้รับความนิยมพุ่งสูงในช่วงหลายปีที่ผ่านมา

WireGuard ถูกรวมเข้าใน Linux Kernel ตั้งแต่เวอร์ชัน 5.6 (ปี 2020) แล้ว ดังนั้นบน Ubuntu 20.04+ หรือ Debian 11+ สามารถติดตั้งได้ทันทีโดยไม่ต้องเพิ่ม repository พิเศษ

สิ่งที่ต้องมีก่อนเริ่ม

ขั้นตอนที่ 1 — อัพเดตระบบและติดตั้ง WireGuard

เชื่อมต่อ SSH เข้า VPS แล้วรันคำสั่งต่อไปนี้

sudo apt update && sudo apt upgrade -y
sudo apt install wireguard -y

ตรวจสอบว่าติดตั้งสำเร็จ

wg --version

ขั้นตอนที่ 2 — สร้าง Key Pair สำหรับ Server

WireGuard ใช้ระบบ Public/Private Key แบบเดียวกับ SSH โดย Private Key เก็บไว้บน server และ Public Key แชร์ให้ client

cd /etc/wireguard
umask 077
wg genkey | tee server_private.key | wg pubkey > server_public.key

ดู key ที่สร้างขึ้น (จำเป็นต้องใช้ในขั้นตอนถัดไป)

cat server_private.key
cat server_public.key
อย่าเปิดเผย Private Key ให้ใคร Private Key คือสิ่งที่พิสูจน์ตัวตนของ server คุณ ถ้าหลุดออกไปให้สร้างใหม่ทันที

ขั้นตอนที่ 3 — สร้างไฟล์ Config ของ Server

สร้างไฟล์ /etc/wireguard/wg0.conf โดยแทนที่ SERVER_PRIVATE_KEY ด้วย private key ที่ได้จากขั้นตอนก่อนหน้า และ YOUR_SERVER_IP ด้วย IP สาธารณะของ VPS

[Interface]
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = SERVER_PRIVATE_KEY

# เปิด IP forwarding เพื่อให้ traffic ผ่าน VPN ออกอินเตอร์เน็ตได้
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
ชื่อ network interface อาจแตกต่างกันตาม VPS provider บางเครื่องอาจเป็น ens3 หรือ enp1s0 แทน eth0 ตรวจสอบด้วย ip route | grep default

ขั้นตอนที่ 4 — เปิด IP Forwarding

เปิด IP forwarding เพื่อให้ Linux ส่ง packet ระหว่าง network ได้

echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p

ขั้นตอนที่ 5 — เปิด Firewall Port

อนุญาตพอร์ต UDP 51820 ผ่าน firewall

# ถ้าใช้ UFW
sudo ufw allow 51820/udp
sudo ufw reload

# ถ้าใช้ iptables โดยตรง
iptables -A INPUT -p udp --dport 51820 -j ACCEPT

ขั้นตอนที่ 6 — สร้าง Key Pair และ Config สำหรับ Client

ทำขั้นตอนนี้บน server เพื่อสร้าง key สำหรับ client (ทำซ้ำได้สำหรับ client หลายเครื่อง)

wg genkey | tee client1_private.key | wg pubkey > client1_public.key
cat client1_private.key
cat client1_public.key

เพิ่ม Peer (client) เข้าไปในไฟล์ /etc/wireguard/wg0.conf ของ server โดยแทนที่ CLIENT1_PUBLIC_KEY ด้วย public key ของ client

[Peer]
PublicKey = CLIENT1_PUBLIC_KEY
AllowedIPs = 10.0.0.2/32

จากนั้นสร้างไฟล์ config สำหรับ client (นำไปใช้บนอุปกรณ์ผู้ใช้) โดยแทนค่าที่จำเป็นทั้งหมด

[Interface]
Address = 10.0.0.2/24
PrivateKey = CLIENT1_PRIVATE_KEY
DNS = 1.1.1.1

[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = YOUR_SERVER_IP:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
AllowedIPs = 0.0.0.0/0 หมายความว่า traffic ทั้งหมด จะผ่าน VPN ถ้าต้องการ route เฉพาะบาง subnet ให้แก้ค่านี้ตามต้องการ เช่น 10.0.0.0/24

ขั้นตอนที่ 7 — เริ่มต้น WireGuard Server

# เริ่ม service
sudo systemctl start wg-quick@wg0

# ตั้งให้ start อัตโนมัติเมื่อ reboot
sudo systemctl enable wg-quick@wg0

# ตรวจสอบสถานะ
sudo systemctl status wg-quick@wg0

# ดู connection จาก client
sudo wg show

ขั้นตอนที่ 8 — ติดตั้ง WireGuard บน Client

Windows / macOS

ดาวน์โหลด WireGuard app จาก wireguard.com/install แล้ว Import ไฟล์ .conf ที่สร้างไว้ในขั้นตอนที่ 6

Ubuntu / Debian

sudo apt install wireguard -y
# วางไฟล์ config ที่ /etc/wireguard/wg0.conf แล้วรัน
sudo wg-quick up wg0

Android / iOS

ติดตั้ง WireGuard app จาก Play Store หรือ App Store แล้ว Scan QR Code จากไฟล์ config (สร้าง QR ด้วยคำสั่ง qrencode -t ansiutf8 < client1.conf บน server)

ทดสอบการเชื่อมต่อ

หลังจาก connect แล้ว ตรวจสอบว่า VPN ทำงานถูกต้อง

  1. ตรวจ IP ที่ whatismyip.com — ควรแสดง IP ของ VPS ไม่ใช่ IP ของคุณ
  2. Ping ไปยัง server IP ภายใน VPN: ping 10.0.0.1
  3. ดู traffic บน server: sudo wg show จะแสดง handshake time และ bytes ที่รับ/ส่ง
sudo wg show

# ตัวอย่าง output:
# interface: wg0
#   public key: xxxxxxxxxxxxxxxxxxxx
#   private key: (hidden)
#   listening port: 51820
#
# peer: CLIENT_PUBLIC_KEY_HERE
#   endpoint: 203.0.113.45:54321
#   allowed ips: 10.0.0.2/32
#   latest handshake: 23 seconds ago
#   transfer: 1.24 MiB received, 820 KiB sent

การจัดการ Client เพิ่มเติม

เพิ่ม Client ใหม่แบบ Hot-reload (ไม่ต้อง restart service)

# เพิ่ม peer โดยตรงโดยไม่ต้อง restart
sudo wg set wg0 peer NEW_CLIENT_PUBLIC_KEY allowed-ips 10.0.0.3/32

# บันทึกการเปลี่ยนแปลงลงไฟล์ config
sudo wg-quick save wg0

ลบ Client ออก

sudo wg set wg0 peer CLIENT_PUBLIC_KEY_TO_REMOVE remove
sudo wg-quick save wg0

เพิ่มความปลอดภัย

แนะนำทำขั้นตอนเหล่านี้เพิ่มเติมเพื่อเพิ่มความปลอดภัยของ server

สรุป

WireGuard เป็นตัวเลือกที่ดีที่สุดสำหรับการสร้าง VPN ส่วนตัวบน VPS ในปี 2026 ด้วยความที่มันเบา เร็ว และติดตั้งง่ายกว่าทางเลือกอื่นอย่างเห็นได้ชัด สิ่งที่คุณทำได้หลังจากนี้มีดังนี้

VPS ที่มี RAM 512 MB และ bandwidth 1 TB/เดือน เพียงพอสำหรับการใช้งาน WireGuard VPN สำหรับครอบครัวหรือทีมงานขนาดเล็ก 5–10 คนได้สบายๆ ลองดูแพ็กเกจ VPS ของ 29VPS ที่เหมาะกับการใช้งานนี้ได้เลย

ตรวจสอบว่า WireGuard ทำงานถูกต้อง

# ตรวจสอบ service status
systemctl status wg-quick@wg0

# ดู interface และ peer ที่เชื่อมต่ออยู่
wg show

# ทดสอบ routing ผ่าน VPN (รันจาก client)
ping 10.0.0.1          # ping ไปที่ VPS ผ่าน VPN tunnel
curl ifconfig.me       # ต้องแสดง IP ของ VPS ไม่ใช่ IP client

# ดู IP forwarding
sysctl net.ipv4.ip_forward
# ต้องเป็น: net.ipv4.ip_forward = 1
Tip: ใช้ wg show เพื่อดู "latest handshake" ของแต่ละ peer — ถ้าไม่มี handshake ใน 3-5 นาที แสดงว่า client ไม่ได้เชื่อมต่ออยู่ หรือ key ผิด

แก้ปัญหาที่พบบ่อย

Error: "RTNETLINK answers: Operation not permitted" ตอน start wg-quick

Kernel module ยังไม่ถูก load หรือ kernel ไม่รองรับ WireGuard ตรวจสอบ:

# ตรวจสอบ kernel module
lsmod | grep wireguard
modprobe wireguard   # load module manually

# ถ้า kernel เก่าเกินไป (< 5.6) ต้องใช้ wireguard-dkms
apt install wireguard-dkms -y

Error: Client เชื่อมต่อได้แต่ internet ไม่ทำงาน (ไม่มี internet ผ่าน VPN)

มักเกิดจาก IP forwarding ไม่ได้เปิด หรือ iptables MASQUERADE ไม่ทำงาน:

# ตรวจสอบ IP forwarding
cat /proc/sys/net/ipv4/ip_forward
# ต้องเป็น 1

# ตรวจสอบ NAT rule
iptables -t nat -L POSTROUTING -n -v | grep MASQUERADE

# ตรวจสอบว่า interface name ถูกต้อง (ไม่ใช่ eth0 ทุกเครื่อง)
ip route | grep default   # หา interface จริง (eth0/ens3/enp1s0)

Error: Client เชื่อมต่อไม่ได้ — "handshake did not complete"

Public key ผิด หรือ UDP port 51820 ถูก firewall block:

# ตรวจสอบว่า port เปิดอยู่
ufw status | grep 51820
# ควรเห็น: 51820/udp ALLOW Anywhere

# ทดสอบจาก client ว่า port ถึงได้
nc -vzu YOUR_VPS_IP 51820

# ตรวจสอบ public key ใน server config ตรงกับ client ไหม
cat /etc/wireguard/wg0.conf | grep PublicKey

Error: WireGuard ไม่ start หลัง reboot

ต้อง enable service ไม่ใช่แค่ start:

# ตรวจสอบว่า enabled
systemctl is-enabled wg-quick@wg0

# ถ้าไม่ enabled
systemctl enable wg-quick@wg0

Error: ต้องการเพิ่ม client ใหม่แต่ service ไม่ reload config

ใช้ wg set เพื่อเพิ่ม peer แบบ hot-reload โดยไม่ต้อง restart และ drop connection ที่มีอยู่:

# เพิ่ม peer ใหม่โดยไม่ restart
wg set wg0 peer NEW_CLIENT_PUBLIC_KEY allowed-ips 10.0.0.5/32

# บันทึกลงไฟล์ config
wg-quick save wg0

# ตรวจสอบว่าเพิ่มแล้ว
wg show wg0 peers

คำถามที่พบบ่อย

WireGuard ปลอดภัยพอสำหรับ production ไหม?

ใช่ WireGuard ถูกรวมเข้า Linux kernel mainline ในปี 2020 และได้รับการ audit จากผู้เชี่ยวชาญด้านความปลอดภัย ใช้ cryptography สมัยใหม่ (Curve25519, ChaCha20, Poly1305) และโค้ดเพียง ~4,000 บรรทัด ทำให้ attack surface น้อยกว่า OpenVPN มาก เหมาะทั้งสำหรับ personal use และ enterprise

VPS provider เห็น traffic ในสาย VPN ได้ไหม?

Traffic ระหว่าง client กับ VPS ถูกเข้ารหัสด้วย WireGuard provider ที่ VPS เห็นแค่ UDP packets ขนาดหนึ่งวิ่งออก แต่ไม่รู้ว่า content คืออะไร อย่างไรก็ตาม traffic จาก VPS ออก internet ไม่ถูกเข้ารหัสแล้ว provider บน path นั้นมองเห็นได้ตามปกติ

รองรับ client กี่เครื่องพร้อมกัน?

WireGuard เองไม่มี limit ที่ตายตัว ขึ้นอยู่กับ bandwidth และ CPU ของ VPS VPS RAM 512MB + bandwidth 1TB/เดือน รองรับ client 10-20 คนที่ใช้งาน active พร้อมกันได้สบายๆ ถ้าทุกคนใช้แค่ browsing และ SSH ไม่ใช่ download/streaming ตลอดเวลา