DNS คืออะไร? Domain Name System ทำงานอย่างไร
ทุกครั้งที่คุณเข้าเว็บไซต์ ส่งอีเมล หรือใช้งานแอป DNS ทำงานอยู่เบื้องหลังเพื่อแปลงชื่ออย่าง google.com ให้เป็น IP address ที่คอมพิวเตอร์ใช้สื่อสารกัน
สารบัญ
DNS คืออะไร?
DNS หรือ Domain Name System คือระบบตั้งชื่อแบบกระจายทั่วโลกของอินเทอร์เน็ต ทำหน้าที่แปลงชื่อโดเมนที่มนุษย์อ่านได้ให้เป็น IP address
ถ้าไม่มี DNS เราจะต้องจำ address อย่าง 142.250.80.46 แทนที่จะพิมพ์ชื่ออย่าง google.com
DNS ถูกสร้างโดย Paul Mockapetris ในทศวรรษ 1980 และกำหนดใน RFC 1034 และ RFC 1035 เพื่อแทนระบบ HOSTS.TXT เดิมที่ขยายตามอินเทอร์เน็ตไม่ไหว
ทำไมต้องมี DNS
DNS มีอยู่เพราะมนุษย์จำชื่อได้ดีกว่าตัวเลข แต่ network ใช้ตัวเลขในการส่งข้อมูล DNS จึงช่วยเรื่องเหล่านี้:
- ชื่อที่เป็นมิตรกับมนุษย์ - พิมพ์
github.comแทน IP address - Indirection - โดเมนสามารถชี้ไป server ใหม่ได้โดยไม่ต้องแก้ทุกลิงก์หรือ bookmark
- Load balancing - ชื่อเดียวสามารถ resolve เป็นหลาย IP address เพื่อกระจาย traffic
- Email routing - MX records บอก mail server ว่าต้องส่งอีเมลของโดเมนไปที่ไหน
- Domain verification - TXT records ใช้ยืนยัน ownership สำหรับบริการต่าง ๆ certificates, SPF, DKIM และ DMARC
DNS Resolution ทำงานอย่างไร
เมื่อเบราว์เซอร์ต้องการ IP address, recursive resolver จะเดินตามลำดับชั้น DNS ให้แทนคุณ
You type: www.example.com
1. Browser cache
If the answer is cached and not expired, use it.
2. Operating system cache
The OS checks its DNS cache and hosts file.
3. Recursive resolver
Your ISP resolver or a public resolver checks its cache.
4. Root name server
Resolver asks: where are the .com servers?
5. TLD name server
Resolver asks .com: where are example.com's nameservers?
6. Authoritative name server
Resolver asks example.com's nameserver: what is www.example.com?
7. Answer
Resolver caches the result and returns the IP address to your device.ลำดับชั้นของ DNS
DNS ถูกจัดเป็นต้นไม้กลับหัว และอ่านชื่อโดเมนจากขวาไปซ้ายตามลำดับชั้น
. (root)
|
---------------------
.com .org .net top-level domains
| | |
example wikipedia cloud second-level domains
| | |
www en api subdomains
Full domain: www.example.com.
The final dot represents the DNS root.| ระดับ | ตัวอย่าง | ดูแลโดย |
|---|---|---|
| Root | . | ICANN และ root server operators |
| TLD | .com, .org, .net, .th | registries ของ top-level domains |
| Second-level domain | example.com | เจ้าของโดเมนผ่าน registrar |
| Subdomain | www, api, mail | เจ้าของโดเมนผ่าน DNS records |
ประเภท DNS Records
DNS เก็บ record หลายชนิดตามงานที่ต่างกัน:
A example.com. 93.184.216.34
AAAA example.com. 2606:2800:220:1:248:1893:25c8:1946
CNAME www.example.com. example.com.
MX example.com. 10 mail.example.com.
TXT example.com. "v=spf1 include:_spf.example.com ~all"
NS example.com. ns1.example.com.| Record | หน้าที่ | ตัวอย่าง |
|---|---|---|
| A | แมปชื่อไปยัง IPv4 address | example.com -> 93.184.216.34 |
| AAAA | แมปชื่อไปยัง IPv6 address | example.com -> 2606:... |
| CNAME | สร้าง alias ไปยังชื่ออื่น | www -> example.com |
| MX | กำหนดเส้นทางอีเมลของโดเมน | 10 mail.example.com |
| TXT | เก็บข้อความสำหรับยืนยันตัวตนและ email security | SPF, DKIM, DMARC, ownership checks |
| NS | ระบุ authoritative nameservers | ns1.example.com |
| SOA | เก็บ authority และ timing data ของ zone | serial, refresh, retry, expire, TTL |
| CAA | ควบคุมว่า CA ใดออก certificate ให้โดเมนได้ | อนุญาต letsencrypt.org |
ข้อจำกัดของ CNAME
CNAME ไม่สามารถอยู่ร่วมกับ record ชนิดอื่นที่ชื่อเดียวกันได้ จึงมีผู้ให้บริการ DNS หลายรายเพิ่ม ALIAS หรือ ANAME สำหรับ root domain hosting
DNS Caching และ TTL
DNS answers ถูก cache เพื่อให้ lookup เร็วขึ้น ระยะเวลาที่ cache เก็บไว้ควบคุมด้วย TTL หรือ Time To Live หน่วยเป็นวินาที
- TTL สั้น เช่น 300 วินาที ช่วยให้การเปลี่ยนแปลงมีผลเร็ว
- TTL ยาว เช่น 3600 หรือ 86400 วินาที ลด DNS traffic และเพิ่มความเร็ว
- ก่อน migration ควรลด TTL ล่วงหน้า 24-48 ชั่วโมง แล้วเพิ่มกลับเมื่อระบบนิ่งแล้ว
DNS Propagation
DNS propagation คือเวลาที่ต้องรอให้คำตอบเก่าที่ถูก cache อยู่ตาม resolver ทั่วโลกหมดอายุ
| ชนิดการเปลี่ยนแปลง | เวลาทั่วไป | หมายเหตุ |
|---|---|---|
| อัปเดต A/AAAA | ไม่กี่นาทีถึง 48 ชั่วโมง | ขึ้นกับ TTL เดิมเป็นหลัก |
| เปลี่ยน nameserver | 24-72 ชั่วโมง | NS records ระดับ TLD มักมี TTL ยาว |
| เพิ่ม subdomain ใหม่ | ไม่กี่นาทีถึงไม่กี่ชั่วโมง | มักไม่มี cache เก่า |
| อัปเดต MX | 1-24 ชั่วโมง | อีเมลอาจล่าช้าระหว่าง transition |
ความปลอดภัยของ DNS
DNS แบบดั้งเดิมไม่ได้ออกแบบโดยคำนึงถึง privacy หรือ authentication เทคโนโลยีสมัยใหม่จึงช่วยแก้ปัญหาคนละส่วน:
Without DNSSEC:
Resolver asks: What is example.com?
Attacker replies: It is 6.6.6.6
Resolver has no built-in way to verify the answer.
With DNSSEC:
Resolver checks cryptographic signatures.
Fake answers fail validation and are rejected.| เทคโนโลยี | ทำอะไร | ไม่ทำอะไร |
|---|---|---|
| DNSSEC | เซ็น DNS records เพื่อให้ resolver ตรวจสอบความถูกต้องได้ | ไม่เข้ารหัส DNS queries |
| DoH | ส่ง DNS queries ผ่าน HTTPS บน port 443 | ไม่ได้พิสูจน์ว่า records ถูก signed หากไม่ใช้ DNSSEC |
| DoT | ส่ง DNS queries ผ่าน TLS บน port 853 | ยังถูกระบุได้ว่าเป็น DNS traffic |
การป้องกันที่ดีที่สุดคือใช้ DNSSEC เพื่อความถูกต้องของ records และใช้ DoH หรือ DoT เพื่อ privacy ระหว่างส่งข้อมูล
Public DNS Resolvers
คุณสามารถใช้ public resolver แทน resolver เริ่มต้นของ ISP ได้ ตัวเลือกที่พบบ่อยคือ:
| Provider | IPv4 | คุณสมบัติ |
|---|---|---|
| 8.8.8.8 / 8.8.4.4 | DNSSEC, DoH, DoT | |
| Cloudflare | 1.1.1.1 / 1.0.0.1 | เร็ว เน้น privacy, DNSSEC, DoH, DoT |
| Quad9 | 9.9.9.9 / 149.112.112.112 | บล็อกโดเมนอันตราย, DNSSEC, DoH, DoT |
| OpenDNS | 208.67.222.222 / 208.67.220.220 | ตัวเลือก filtering และ DNSSEC |
การแก้ปัญหา DNS
ปัญหา DNS อาจทำให้ server ที่ยังทำงานอยู่ดูเหมือนล่ม สาเหตุที่พบบ่อยได้แก่:
| ปัญหา | อาการ | วิธีแก้ |
|---|---|---|
| Cache เก่า | ยังโหลดเว็บเก่าหลังเปลี่ยน DNS | flush cache และรอ TTL หมดอายุ |
| Nameserver ผิด | โดเมนไม่ resolve เลย | ตรวจ NS records ที่ registrar |
| ไม่มี A/AAAA record | NXDOMAIN หรือ browser DNS error | เพิ่ม records ให้ domain/subdomain |
| อีเมลไม่ทำงาน | อีเมล bounce หรือหาย | ตรวจ MX, SPF, DKIM และ DMARC |
| CNAME target ผิด | เว็บชี้ไปบริการผิด | trace CNAME chain |
คำสั่ง DNS ที่มีประโยชน์
คำสั่งเหล่านี้มีประโยชน์สำหรับ debug DNS จาก terminal:
# nslookup
nslookup example.com
nslookup -type=MX example.com
nslookup example.com 8.8.8.8
# dig
dig example.com A
dig example.com MX
dig @8.8.8.8 example.com
dig +trace example.com
dig +short example.com
# Flush DNS cache on Windows
ipconfig /flushdnsค้นหา DNS Records ได้ทันที
ใช้เครื่องมือ DNS Checker ฟรีของเราเพื่อค้นหา A, AAAA, CNAME, MX, TXT, NS, SOA และ DNS records อื่น ๆ สำหรับโดเมนใดก็ได้
ลองใช้ DNS Checkerแหล่งอ้างอิง
- Mockapetris, P. (1987). RFC 1034 - Domain Names: Concepts and Facilities. https://datatracker.ietf.org/doc/html/rfc1034
- Mockapetris, P. (1987). RFC 1035 - Domain Names: Implementation and Specification. https://datatracker.ietf.org/doc/html/rfc1035
- Arends, R., et al. (2005). RFC 4033 - DNS Security Introduction and Requirements. https://datatracker.ietf.org/doc/html/rfc4033
- Hoffman, P. & McManus, P. (2018). RFC 8484 - DNS Queries over HTTPS (DoH). https://datatracker.ietf.org/doc/html/rfc8484
- ICANN. Root Server Technical Operations. https://root-servers.org/