DNS Bind in Ubuntu
posted on 27 Feb 2010 16:53 by zamorano99 in Ubuntuการติดตั้งและคอนฟิกเนมเซิร์ฟเวอร์ ในองค์กร
ในที่นี้จะกล่าวถึงการติดตั้งและใช้งาน Name server ในองค์กรเท่านั้นจะไม่กล่าวถึงระบบโดเมนของ
อินเทอร์เน็ต ถามว่าเนมเซิร์ฟเวอร์ในองค์กรไม่มีได้มั๊ย ตอบว่าได้ แต่ท่านจะใช้งานได้เฉพาะ IP เท่านั้น เช่น ท่าน
ติดตั้งเวบเซิร์ฟเวอร์ในองค์กร มี IP 192.168.2.1 เวลาใช้งานจากเบราเซอร์ก็ต้องเรียก http://192.168.2.1 ท่าน
เป็นผู้ดูแลระบบคงไม่มีปัญหาที่จะใช้งานแบบนี้ แต่ผู้ใช้งานทั่วไปคงลำาบาก หรือผู้ดูแลระบบได้ติดตั้ง mail
sever โดยที่ mail server มี IP address 192.168.2.2 ถ้าไม่มีเนมเซิร์ฟเวอร์ ท่านก็ไม่สามารถส่ง mail ไปยัง
somchai@192.168.2.2 ได้ จากที่กล่าวมาจะเห็นว่า nameserver มีความสำาคัญไม่น้อย ถ้าเราเริ่มต้นด้วย Name
server แล้ว อย่างอื่นก็จะสะดวกตามไปด้วย
ความต้องการในบทนี้
IP Address ชื่อ
192.168.2.1 www.myintranet.org
192.168.2.2 mail.myintranet.org
192.168.2.10 ns.myintranet.org
โปรแกรม BIND9
โปรแกรมที่เราติดตั้งเพื่อทำาเนมเซิร์ฟเวอร์คือ Bind เวอร์ชัน 9 ซึ่งจะเรียกว่า BIND9 BIND (Berkeley
Internet Name Domain) โปแกรม BIND ได้พัฒนาขึ้นที่ University of California at Berkeley เป็นโปรเจคของ
นักศึกษาปริญญาตรี ภายใต้การสนับสนุนโดย US Defense Advanced Research Projects Administration
(DARPA) โปแกรม BIND ตั้งแต่พัฒนาจนถึงเวอร์ชัน 4.8.3 ได้รับการดูแลจาก Computer Systems Research
Group (CSRG) ที่ UC Berkeley ทีมพัฒนาได้แก่ Douglas Terry, Mark Painter, David Riggle และ Songnian Zhou
หลังจากนั้นก็ได้มีการรับช่วงการพัฒนามาหลายคนหลายองค์กร จนปัจจุบัน BIND เวอร์ชัน 9 ซึ่งออกมา
เมื่อเดือนกันยายน ปี 2000
มีหลายวิธีที่จะคอนฟิก BIND9 ส่วนใหญ่เราจะทำาเป็น namserver แบบ Primary master และ secondary
master แบบ Primary Master จะอ่านข้อมูลจากไฟล์คอนฟิกบนเครื่องตัวเอง แบบ Secondary Master จะอ่าน
ข้อมูลจากเครื่องอื่นที่ได้รับอนุญาต
การติดตั้ง BIND9 บน Ubuntu Server
Bind9 อยู่ในแผ่นติดตั้ง Ubuntu Server อยู่แล้ว เราสามารถติดตั้งได้ด้วยคำาสั่ง tasksel
root@ubuntuserver:~#
tasksel
หรือ sudo tasksel ก็แล้วแต่ว่าใครล๊อกอินเข้ามาด้วย user ไหน เมื่อเรียกคำาสั่ง tasksel ก็จะได้ดังภาพ
หรือจะติดตั้งด้วย apt-get
root@ubuntuserver:~#
aptget
install bind9
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
bind9doc
resolvconf
The following NEW packages will be installed:
bind9
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 0B/267kB of archives.
After this operation, 762kB of additional disk space will be used.
Selecting previously deselected package bind9.
(Reading database ... 15320 files and directories currently installed.)
Unpacking bind9 (from .../bind9/bind9_9.4.210_
i386.deb) ...
Setting up bind9 (1:9.4.210)
...
Adding group `bind' (GID 113) ...
Done.
Adding system user `bind' (UID 105) ...
Adding new user `bind' (UID 105) with group `bind' ...
Not creating home directory `/var/cache/bind'.
wrote key file "/etc/bind/rndc.key"
Reloading AppArmor profiles : done.
* Starting domain name service... bind [ OK ]
หลังจากที่ติดตั้ง BIND9 ไปแล้วไดเรกทอรีที่เก็บไฟล์คอนฟิก ได้แก่
● /etc/bind ไฟล์คอนฟิกหลักคือ /etc/bind/named.conf
● include ไฟล์ หรือ option เพิ่มเติม จะเก็บไว้ที่ /etc/bind/named.conf.options
● /etc/bind/db.root เก็บ name server ทั่วโลก
Caching Nameserver
Caching Nameserver เป็น Server ตัวถัดไปที่จะให้ลูกข่ายค้นหาถ้าหาใน server ตัวแรกไม่เจอ ส่วนใหญ่
เราจะใส่ค่า Nameserver ของ ISP ที่เราใช้งานอยู่ เช่น KSC 203.113.24.199, TOT 203.113.24.199 ซึ่ง option นี้อยู่
ใน /etc/bind/named.conf.options
// forwarders {
// 0.0.0.0;
// };
เปลี่ยนเป็น
forwarders {
203.155.33.1;
203.113.24.199;
};
Primary Master Name Server
ในที่นี้จะกล่าวถึงการติดตั้งเนมเซิร์ฟเวอร์ที่เป็น Primary Master คือการอ่านค่าคอนฟิกจากเครื่องตัวเอง
ซึ่งค่าคอนฟิกนี้คือ การเปลี่ยนชื่อเป็น IP Address และเปลี่ยน IP Address เป็นชื่อ สามารถทำาได้โดยการ ก็อปปี้
ไฟล์คอนฟิกที่มีอยู่แล้วเอามาแก้ไข ไม่ต้องพิมพ์เองทั้งหมด
1. ก๊อปปี้ไฟล์ดังคำาสั่ง root@ubuntu-server:~# cp /etc/bind/db.local /etc/bind/db.myintranet.org ไฟล์นี้ทำา
หน้าที่เพื่อเปลี่ยนชื่อเป็น IP Address
2. แก้ไขไฟล์ /etc/bind/named.conf.local ด้วย Text Editor ที่ท่านถนัดโดยเพิ่มรายละเอียด ด้านล่างเข้าไป
zone "myintranet.org" {
type master;
file "/etc/bind/db.myintranet.org";
};
3. ทำาการแก้ไขไฟล์ดังนี้ /etc/bind/db.myintranet.org
;
; BIND data file for local loopback interface
;
$TTL 604800
@ IN SOA ns.myintranet.org. root.myintranet.org. (
1 ; Serial
604800 ; Refresh
86400 ; Retry
2419200 ; Expire
604800 ) ; Negative Cache TTL
@ IN MX 10 mail.myintranet.org.
;
@ IN NS ns.myintranet.org.
@ IN A 192.168.2.10
ns IN A 192.168.2.10
www IN A 192.168.2.1
mail IN A 192.168.2.2
4. ก๊อปปี้ไฟล์ดังคำาสั่ง root@ubuntu-server:~# sudo cp /etc/bind/db.127 /etc/bind/db.192 ไฟล์นี้ทำาหน้าที่
เปลี่ยนชื่อ IP Address เป็นชื่อ
5. หลังจากนั้นแก้ไขไฟล์ /etc/bind/named.conf.local
เพิ่ม
zone "2.168.192.inaddr.
arpa" {
type master;
notify no;
file "/etc/bind/db.192";
};
6. แก้ไขไฟล์ /etc/bind/db.192
;
; BIND reverse data file for local loopback interface
;
$TTL 604800
@ IN SOA ns.myintranet.org. root.myintranet.org. (
1 ; Serial
604800 ; Refresh
86400 ; Retry
2419200 ; Expire
604800 ) ; Negative Cache TTL
;
@ IN NS ns.
10 IN PTR ns.myintranet.org.
1 IN PTR www.myintanet.org.
2 IN PTR mail.myintanet.org.
7. สั่ง restart service ด้วยคำาสั่ง root@ubuntu-server:~# /etc/init.d/bind9 restart
root@ubuntuserver:~#
/etc/init.d/networking restart
* Reconfiguring network interfaces... [ OK ]
root@ubuntuserver:~#
8. แก้ไขไฟล์ /etc/resolv.conf
nameserver localhost
หรือ
nameserver 192.168.2.10
9. แล้วรีสตาร์ทเน็ตเวิร์คด้วยคำาสั่ง root@ubuntuserver:~#
/etc/init.d/networking restart
10. ทดสอบการทำางานของเซิร์ฟเวอร์ บนเซิร์ฟเวอร์ด้วยคำาสั่ง dig
root@ubuntuserver:/
etc/bind# dig myintranet.org
; <<>> DiG 9.4.2 <<>> myintranet.org
;; global options: printcmd
;; Got answer:
;; >>
HEADER<<opcode:
QUERY, status: NOERROR, id: 38927
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 1
;; QUESTION SECTION:
;myintranet.org. IN A
;; ANSWER SECTION:
myintranet.org. 604800 IN A 192.168.2.10
;; AUTHORITY SECTION:
myintranet.org. 604800 IN NS ns.myintranet.org.
;; ADDITIONAL SECTION:
ns.myintranet.org. 604800 IN A 192.168.2.10
;; Query time: 1 msec
;; SERVER: 192.168.2.10#53(192.168.2.10)
;; WHEN: Fri Sep 5 17:17:04 2008
;; MSG SIZE rcvd: 81
etc/bind# dig www.myintranet.org
; <<>> DiG 9.4.2 <<>> www.myintranet.org
;; global options: printcmd
;; Got answer:
;; >>
HEADER<<opcode:
QUERY, status: NOERROR, id: 1814
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 1
;; QUESTION SECTION:
;www.myintranet.org. IN A
;; ANSWER SECTION:
www.myintranet.org. 604800 IN A 192.168.2.1
;; AUTHORITY SECTION:
myintranet.org. 604800 IN NS ns.myintranet.org.
;; ADDITIONAL SECTION:
ns.myintranet.org. 604800 IN A 192.168.2.10
;; Query time: 1 msec
;; SERVER: 192.168.2.10#53(192.168.2.10)
;; WHEN: Fri Sep 5 17:17:34 2008
;; MSG SIZE rcvd: 85
etc/bind# dig mail.myintranet.org
; <<>> DiG 9.4.2 <<>> mail.myintranet.org
;; global options: printcmd
;; Got answer:
;; >>
HEADER<<opcode:
QUERY, status: NOERROR, id: 3851
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 1
;; QUESTION SECTION:
;mail.myintranet.org. IN A
;; ANSWER SECTION:
mail.myintranet.org. 604800 IN A 192.168.2.2
;; AUTHORITY SECTION:
myintranet.org. 604800 IN NS ns.myintranet.org.
;; ADDITIONAL SECTION:
ns.myintranet.org. 604800 IN A 192.168.2.10
;; Query time: 1 msec
;; SERVER: 192.168.2.10#53(192.168.2.10)
;; WHEN: Fri Sep 5 17:19:06 2008
;; MSG SIZE rcvd: 86
ถ้าใช้คำาสั่ง dig แล้วได้คำาตอบดังข้างต้นแสดงว่าเนมเซิร์ฟเวอร์ทำางานถูกต้องแล้ว
11. ทดสอบการทำางานของเนมเซิร์ฟเวอร์จากเครื่องลูกข่ายที่เป็นลีนุกซ์ทำาเช่นกัน กับข้อ 8-10
12. ทดสอบการทำางานของเนมเซิร์ฟเวอร์จากเครื่องลูกข่ายวินโดว์
เราชี้ DNS server ไปยัง IP Name Server ของเรา
แล้วทดลองใช้คำาสั่ง nslookup ดังภาพ
หรือทดลอง ping ดู
ก็สรุปได้ว่าเนมเซิร์ฟเวอร์ของเราทำางานได้แล้ว
แหล่งอ้างอิง
http://doc.ubuntu.com/ubuntu/serverguide/C/dns.html
