DNS (Domain Name System) คือระบบแปลงชื่อโดเมนเป็น IP Address ที่ทุก Linux Server ต้องพึ่งพาทุกวัน ตั้งแต่การ apt install ที่ต้องค้นหา IP ของ Repository, การเชื่อมต่อฐานข้อมูลที่ใช้ชื่อ Host แทน IP ไปจนถึงการส่ง Email ที่ต้องค้นหา MX Record ถ้า DNS ตั้งค่าผิดหรือ Resolver ช้า ทุก Network Operation จะได้รับผลกระทบทันที
บทความนี้อธิบายการตั้งค่า DNS บน Linux ตั้งแต่ระบบ Resolver (/etc/resolv.conf, systemd-resolved, NetworkManager), การกำหนด DNS Server ที่จะใช้, การตั้งค่า /etc/hosts สำหรับ Local Resolution, การใช้ dig และ nslookup ตรวจสอบการทำงาน รวมถึงการตั้งค่า DNS Caching และ DNSSEC
ระบบ DNS Resolver บน Linux
Linux มีสามระบบหลักในการจัดการ DNS Resolution ขึ้นอยู่กับ Distribution และ Configuration:
# ตรวจสอบว่าระบบใช้ระบบ DNS อะไร
# วิธีที่ 1: ดู /etc/resolv.conf
cat /etc/resolv.conf
# ถ้าเห็น: "nameserver 127.0.0.53" → ใช้ systemd-resolved
# ถ้าเห็น: "nameserver 127.0.0.1" → อาจใช้ dnsmasq (NetworkManager)
# ถ้าเห็น: "nameserver 8.8.8.8" → กำหนดตรงหรือผ่าน DHCP
# วิธีที่ 2: ตรวจสอบ Symlink ของ /etc/resolv.conf
ls -la /etc/resolv.conf
# → /etc/resolv.conf -> /run/systemd/resolve/stub-resolv.conf (systemd-resolved)
# → /etc/resolv.conf -> /run/NetworkManager/resolv.conf (NetworkManager)
# → /etc/resolv.conf (regular file) (manual)
# วิธีที่ 3: ตรวจสอบ Service ที่รันอยู่
systemctl status systemd-resolved
systemctl status NetworkManager
/etc/resolv.conf — ไฟล์ตั้งค่า DNS หลัก
ไฟล์ /etc/resolv.conf กำหนด DNS Nameserver, Search Domain, และพฤติกรรม Resolver สำหรับระบบที่ไม่ได้ใช้ systemd-resolved สามารถแก้ไขได้ตรง แต่บนระบบที่มี systemd-resolved หรือ NetworkManager ไฟล์นี้ถูก Generate อัตโนมัติ
# ตัวอย่าง /etc/resolv.conf แบบ Manual
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 1.1.1.1
search example.com internal.example.com
options ndots:5 timeout:2 attempts:3
# คำอธิบาย Options:
# nameserver — IP ของ DNS Server (ได้สูงสุด 3 ตัว)
# search — Domain ที่ต่อท้ายเมื่อ Query ชื่อที่ไม่มีจุด (เช่น "db" → "db.example.com")
# domain — Default Search Domain (ถ้าไม่มี search)
# options ndots:5 — Query ต้องมีจุดอย่างน้อย 5 ตัวจึงถือเป็น FQDN ก่อน Search
# options timeout:2 — รอ Response ไม่เกิน 2 วินาที
# options attempts:3 — ลอง Nameserver ได้ 3 ครั้ง
แก้ไข /etc/resolv.conf บนระบบ Manual
# บนระบบที่ /etc/resolv.conf เป็นไฟล์ปกติ (ไม่ใช่ Symlink)
sudo nano /etc/resolv.conf
# เพิ่ม/เปลี่ยน nameserver
nameserver 1.1.1.1
nameserver 1.0.0.1
# ป้องกัน DHCP เขียนทับ (ล็อคไฟล์)
sudo chattr +i /etc/resolv.conf # Lock
sudo chattr -i /etc/resolv.conf # Unlock (เมื่อต้องการแก้ไขอีกครั้ง)
# ตรวจสอบ attribute
lsattr /etc/resolv.conf
systemd-resolved — DNS Resolver สมัยใหม่
Ubuntu 18.04+ และ Debian ใหม่ใช้ systemd-resolved เป็น Local DNS Stub Resolver ที่ทำงานที่ 127.0.0.53 มีความสามารถด้าน DNS-over-TLS, DNSSEC, และ Caching
# ดูสถานะ DNS ปัจจุบันของทุก Interface
resolvectl status
# ดูเฉพาะ DNS Server ที่ใช้งาน
resolvectl dns
# ดู Search Domains
resolvectl domain
# ทดสอบ Query ผ่าน systemd-resolved
resolvectl query google.com
resolvectl query --type=MX gmail.com
# ดู Cache Statistics
resolvectl statistics
# ล้าง DNS Cache
sudo resolvectl flush-caches
กำหนด DNS Server ใน systemd-resolved
# แก้ไขไฟล์ /etc/systemd/resolved.conf
sudo nano /etc/systemd/resolved.conf
# ตัวอย่างการตั้งค่า
[Resolve]
DNS=1.1.1.1 8.8.8.8
FallbackDNS=1.0.0.1 8.8.4.4
Domains=example.com
DNSSEC=yes
DNSOverTLS=opportunistic
Cache=yes
CacheFromLocalhost=no
# Restart service หลังแก้ไข
sudo systemctl restart systemd-resolved
# ตรวจสอบผล
resolvectl status
กำหนด DNS Server ต่อ Interface ผ่าน Netplan
# /etc/netplan/00-config.yaml
network:
version: 2
ethernets:
eth0:
dhcp4: false
addresses:
- 192.168.1.10/24
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses:
- 1.1.1.1
- 8.8.8.8
search:
- example.com
- internal.example.com
# Apply
sudo netplan apply
# ตรวจสอบ
resolvectl status eth0
/etc/hosts — Local Name Resolution
ไฟล์ /etc/hosts ใช้กำหนดการ Resolve ชื่อโดยตรง โดยไม่ผ่าน DNS Server เหมาะสำหรับ Server ภายใน, Development Environment, หรือ Override DNS ชั่วคราว ระบบจะตรวจสอบ /etc/hosts ก่อน DNS เสมอ (ตามลำดับใน /etc/nsswitch.conf)
# /etc/hosts — โครงสร้างพื้นฐาน
127.0.0.1 localhost
127.0.1.1 myhostname.example.com myhostname
::1 localhost ip6-localhost ip6-loopback
# เพิ่ม Internal Servers
192.168.1.10 db.internal db
192.168.1.20 cache.internal redis
192.168.1.30 app1.internal app1
# Override DNS ชั่วคราว (เช่น ทดสอบ Deploy)
203.0.113.10 www.example.com example.com
# รูปแบบ: IP FQDN Alias1 Alias2
# สามารถใส่ได้หลาย Alias ในบรรทัดเดียว
# ตรวจสอบลำดับการ Resolve (NSS — Name Service Switch)
cat /etc/nsswitch.conf | grep hosts
# ตัวอย่าง Output:
# hosts: files dns myhostname
# ความหมาย: ตรวจสอบตามลำดับ files → dns → myhostname
# files = /etc/hosts
# dns = DNS Server
# myhostname = hostname ของ Machine เอง
# ทดสอบ Resolve ชื่อ (ใช้ getent ที่ใช้ NSS เหมือน Application จริง)
getent hosts db.internal
getent hosts google.com
dig — เครื่องมือ Query DNS แบบละเอียด
dig (Domain Information Groper) เป็นเครื่องมือ Query DNS ที่ให้รายละเอียดครบถ้วนที่สุด เหมาะสำหรับ Debug DNS Problems และตรวจสอบ Record ต่าง ๆ
# Query A Record (IPv4)
dig example.com
# Query เฉพาะ Record Type
dig example.com A
dig example.com AAAA # IPv6
dig example.com MX # Mail Server
dig example.com TXT # Text/SPF/DKIM
dig example.com NS # Nameserver
dig example.com CNAME # Alias
dig example.com SOA # Start of Authority
dig example.com PTR # Reverse DNS (ใช้กับ IP)
# Reverse DNS Lookup
dig -x 8.8.8.8
# Query ผ่าน DNS Server เฉพาะ (ไม่ใช้ System Resolver)
dig @8.8.8.8 example.com
dig @1.1.1.1 example.com MX
# แสดงเฉพาะ Answer (ไม่มี Header)
dig +short example.com
dig +short MX gmail.com
# ดู TTL ด้วย
dig +ttlid example.com
# ตรวจสอบ DNSSEC
dig +dnssec example.com
อ่านผลลัพธ์ของ dig
# ตัวอย่าง Output ของ dig example.com A
; <<>> DiG 9.18.1 <<>> example.com A
;; QUESTION SECTION:
;example.com. IN A
;; ANSWER SECTION:
example.com. 3600 IN A 93.184.216.34
;; AUTHORITY SECTION:
example.com. 3600 IN NS a.iana-servers.net.
;; Query time: 23 msec
;; SERVER: 1.1.1.1#53(1.1.1.1) (UDP)
;; WHEN: Thu Apr 16 10:00:00 UTC 2026
;; MSG SIZE rcvd: 56
# คำอธิบาย:
# ANSWER SECTION — ผลลัพธ์ DNS (IP, Record Type, TTL)
# AUTHORITY SECTION — Nameserver ที่รับผิดชอบ Domain
# Query time — เวลาในการ Query (ถ้า 0 msec = มาจาก Cache)
# SERVER — DNS Server ที่ตอบ
# TTL — จำนวนวินาทีที่ Cache ยังใช้ได้
nslookup — Query DNS แบบ Interactive
# Query ปกติ
nslookup example.com
nslookup example.com 8.8.8.8 # ระบุ DNS Server
# Interactive Mode
nslookup
> set type=MX
> gmail.com
> set type=NS
> example.com
> exit
# Reverse Lookup
nslookup 8.8.8.8
DNS Caching บน Linux
การ Cache DNS Response ช่วยลด Latency และลด Load บน DNS Server DNS Cache บน Linux ทำงานผ่าน systemd-resolved, nscd, หรือ dnsmasq
# systemd-resolved (Ubuntu/Debian ใหม่)
# ดู Cache Statistics
resolvectl statistics
# ล้าง Cache
sudo resolvectl flush-caches
# nscd (Name Service Cache Daemon) — ติดตั้งเพิ่มเติม
sudo apt install nscd
# ดู Cache Statistics
nscd -g
# ล้าง Cache เฉพาะ DNS
nscd -i hosts
# dnsmasq — DNS/DHCP Caching Server
sudo apt install dnsmasq
# /etc/dnsmasq.conf — ตั้งค่าพื้นฐาน
# server=8.8.8.8
# server=1.1.1.1
# cache-size=1000
# neg-ttl=3600
sudo systemctl enable --now dnsmasq
# เมื่อใช้ dnsmasq แก้ /etc/resolv.conf ให้ชี้ที่ 127.0.0.1
# nameserver 127.0.0.1
DNSSEC — ตรวจสอบความถูกต้องของ DNS Response
# เปิด DNSSEC ใน systemd-resolved
# /etc/systemd/resolved.conf
# DNSSEC=yes # บังคับใช้ DNSSEC
# DNSSEC=opportunistic # ใช้เมื่อทำได้ (ค่าที่แนะนำ)
# DNSSEC=no # ปิด
# ตรวจสอบว่า Domain รองรับ DNSSEC
dig +dnssec example.com
# ดู DNSSEC Status
resolvectl query --type=DS example.com
resolvectl status | grep DNSSEC
# ทดสอบ DNSSEC Validation
dig +dnssec sigok.verteiltesysteme.net # ควร Validate ได้
dig +dnssec sigfail.verteiltesysteme.net # ควร Fail (ทดสอบ DNSSEC Failure)
Split DNS — ใช้ DNS ต่างกันตาม Domain
Split DNS ใช้ DNS Server ต่างกันสำหรับ Domain ต่างกัน เช่น ใช้ Internal DNS สำหรับ .internal แต่ใช้ Public DNS สำหรับ Domain ภายนอก เหมาะสำหรับ Server ที่ต้องเชื่อมต่อทั้ง Internal Network และ Internet
# กำหนด Split DNS ผ่าน systemd-resolved
# /etc/systemd/resolved.conf.d/internal.conf
[Resolve]
DNS=192.168.1.53
Domains=internal.example.com ~example.com
# หรือกำหนดต่อ Interface ผ่าน resolvectl
sudo resolvectl dns eth0 192.168.1.53
sudo resolvectl domain eth0 internal.example.com ~example.com
# ~ หน้า Domain = Route DNS สำหรับ Domain นี้ผ่าน Interface นี้
# กำหนดผ่าน Netplan
# network:
# ethernets:
# eth0:
# nameservers:
# addresses: [192.168.1.53]
# search: [internal.example.com]
# eth1:
# nameservers:
# addresses: [8.8.8.8]
# search: []
Troubleshoot DNS Problems
# Step 1: ตรวจสอบ DNS Server ที่ระบบใช้
resolvectl status
# หรือ
cat /etc/resolv.conf
# Step 2: ทดสอบ Query ตรงไปยัง DNS Server
dig @8.8.8.8 google.com
# ถ้า Query ได้ → ปัญหาอยู่ที่ Resolver ของระบบ ไม่ใช่ Network
# Step 3: ทดสอบผ่าน System Resolver
dig google.com
getent hosts google.com
# Step 4: ตรวจสอบ /etc/hosts ว่า Override DNS หรือไม่
grep google.com /etc/hosts
# Step 5: ตรวจสอบ NSS Order
cat /etc/nsswitch.conf | grep hosts
# Step 6: ดู Log ของ systemd-resolved
journalctl -u systemd-resolved -f
# Step 7: ทดสอบ Connectivity ไปยัง DNS Port 53
nc -zv 8.8.8.8 53
nc -zuv 8.8.8.8 53 # UDP
# ปัญหาที่พบบ่อย:
# "NXDOMAIN" → ชื่อ Domain ไม่มีอยู่จริง
# "SERVFAIL" → DNS Server มีปัญหา หรือ DNSSEC Validation ล้มเหลว
# "connection refused" → Firewall บล็อก Port 53
# "Timeout" → DNS Server เข้าถึงไม่ได้หรือช้า
Script ตั้งค่า DNS อัตโนมัติ
#!/bin/bash
# /usr/local/bin/set-dns.sh
# ตั้งค่า DNS Server ผ่าน systemd-resolved
PRIMARY_DNS="${1:-1.1.1.1}"
SECONDARY_DNS="${2:-8.8.8.8}"
RESOLVED_CONF="/etc/systemd/resolved.conf.d/custom-dns.conf"
# สร้าง Drop-in Config
sudo mkdir -p /etc/systemd/resolved.conf.d
cat <<EOF | sudo tee "$RESOLVED_CONF"
[Resolve]
DNS=${PRIMARY_DNS} ${SECONDARY_DNS}
FallbackDNS=1.0.0.1 8.8.4.4
DNSSEC=opportunistic
Cache=yes
EOF
# Restart systemd-resolved
sudo systemctl restart systemd-resolved
# ตรวจสอบ
echo "DNS ปัจจุบัน:"
resolvectl status | grep "DNS Servers"
# ทดสอบ Query
echo "ทดสอบ Query google.com:"
resolvectl query google.com 2>&1 | head -3
echo "✅ ตั้งค่า DNS เสร็จ: Primary=${PRIMARY_DNS}, Secondary=${SECONDARY_DNS}"
สรุป
การตั้งค่า DNS บน Linux ขึ้นอยู่กับ System ที่ใช้งาน Ubuntu/Debian รุ่นใหม่ใช้ systemd-resolved ซึ่งตั้งค่าผ่าน /etc/systemd/resolved.conf หรือ Netplan และสามารถตรวจสอบสถานะด้วย resolvectl status ส่วน /etc/hosts ใช้สำหรับ Override การ Resolve ชื่อโดยตรงโดยไม่ผ่าน DNS Server การ Debug ให้เริ่มจาก dig @8.8.8.8 domain เพื่อแยกว่าปัญหาอยู่ที่ Network หรือ System Resolver สำหรับ Performance ให้เปิด Cache ใน systemd-resolved และพิจารณา DNSSEC เพื่อป้องกัน DNS Spoofing
แนะนำบริการ DE
การตั้งค่า DNS บน Server จริงต้องมีสิทธิ์ Root เพื่อแก้ไข systemd-resolved และ Netplan Cloud VPS ของ DE ให้ Root Access เต็มรูปแบบพร้อม IP Address คงที่ สามารถกำหนด Custom DNS Server, ตั้งค่า Split DNS สำหรับ Hybrid Network และเปิด DNSSEC ได้อย่างอิสระ
สำหรับเว็บไซต์ที่ต้องการจัดการ DNS Record แบบครบวงจร Cloud Hosting ของ DE มีระบบจัดการ DNS Zone ผ่าน Control Panel พร้อมรองรับ A, CNAME, MX, TXT และ SPF Record สำหรับ Email และ Domain Verification

