Friday, September 30, 2011

How to Setup a DNS Server on CentOS 5

This is a pretty geeky thing to do but running your own DNS Server on your intranet does make some sense from a performance and conveience perspective.
Background
The sole purpose of a DNS Server is to provide a look-up service for mapping hostnames e.g. www.google.com to IP addresses 100.101.20.2. By far the most popular DNS Server software out there is a package called bind.
There are essentially 2 reasons to running your own DNS Server.
  1. Improving the speed of hostname lookups
  2. Managing your own intranet hostnames & IP addresses in a central location
The first reason is what is commonly called a Caching Name Server. In essence by having one machine provide all hostname look-ups in your intranet, you’re cutting down on every machine in your intranet having to independently look-up names. Additionally once a name is looked up your DNS Server will cache the results for a set period of time so that subsequent queries can come out of your DNS Server’s cache and not have to go and do the look-up on your ISPs DNS Server.
The second reason is actually the cooler one. Since you control the DNS Server you can create your own local domain (called zones in bind lingo) and name all the systems within your intranet. In so doing this it’s much easier to connect to systems using names rather than IP addresses. Plus it’s fun to name all the systems!
Taking this a step further you can create generic hostnames such as imap, mail, smtp, ntp, pop, etc. and manage these in one stop rather than having to manage them throughout your intranet.
Getting started
First things first, install the necessary bind software.
1
yum install bind bind-utils bind-libs

Generating a rndc key
NOTE: All the config files we’re going to work on are located in /var/named/chroot

Next you’ll need to create/modify a rndc.conf and/or rndc.key file. This file contains a key which is required in order to manage the bind service once it’s up and running. With this key you can theoretically manage bind either on the host where it’s running or you can manage it on any system that is allowed to do so and knows this key!
1
2
# command to generate a new 512 byte key
rndc-confgen -b 512
This command will return the following output which you’ll want to either redirect to a file or copy and paste into a file. The file should be rndc.conf but I like to use this name instead, /var/named/chroot/etc/rndc.key. It just makes more sense to me.
1
2
3
4
5
6
7
8
9
10
11
12
# Start of rndc.conf
key "rndckey" {
        algorithm hmac-md5;
        secret "nHFS3WOpdap75IvsYSXVNYWusnAQPT6z5XC8V5YPWXnZ8RN8tdfSFuClZ8nNouWyGhvHB8mETJgwsrvhiYhIhA==";
};
 
options {
        default-key "rndckey";
        default-server 127.0.0.1;
        default-port 953;
};
# End of rndc.conf
The rndc-confgen command also returns some additional output below. This goes into the file /var/named/chroot/etc/named.conf. Make sure to remove the comments at the beginning of each line to turn them on.
1
2
3
4
5
6
7
8
9
10
11
# Use with the following in named.conf, adjusting the allow list as needed:
key "rndckey" {
      algorithm hmac-md5;
      secret "nHFS3WOpdap75IvsYSXVNYWusnAQPT6z5XC8V5YPWXnZ8RN8tdfSFuClZ8nNouWyGhvHB8mETJgwsrvhiYhIhA==";
};
# 
controls {
      inet 127.0.0.1 port 953
      allow { 127.0.0.1; } keys { "rndckey"; };
};
# End of named.conf
named.conf file
Now let’s flesh out that named.conf file a little bit more.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
acl lan {   
        192.168.1.0/24;
        127.0.0.1;
};
 
controls {
        inet 127.0.0.1 port 953
        allow { lan; } keys { "rndckey"; };
};
 
logging {
        category default { default_syslog; };
        category lame-servers { null; };
};
 
options {
        pid-file "/var/run/named/named.pid";
        directory "/var/named";
        dump-file "named_dump.db";
        forward only;
        forwarders {
        # the following IP addresses are my ISPs DNS Servers. These will be used for looking up
        # hostnames that I don't locally manage, i.e. the REST OF THE INTERNET!
        24.92.226.40;     # my ISPs DNS Server #1
        24.92.226.41;     # my ISPs DNS Server #2
        };
        /*
         * If there is a firewall between you and nameservers you want
         * to talk to, you might need to uncomment the query-source
         * directive below.  Previous versions of BIND always asked
         * questions using port 53, but BIND 8.1 uses an unprivileged
         * port by default.
         */
        query-source address * port 53;
        allow-query { lan; };
        allow-recursion { lan; };
        allow-transfer { lan; };
};
 
zone "bubba.net"{
        type master;
        file "data/db.bubba.net";
};
zone "0.0.127.in-addr.arpa"{
        type master;
        file "data/db.127.0.0";
};
zone "1.168.192.in-addr.arpa"{
        type master;
        file "data/db.192.168.1";
};
zone "." {
        type hint;
        file "root.hints";
};
Here is a quick rundown of what’s going on here. The acl lan creates an access control list that includes 192.168.1.0/24 and 127.0.0.1 so that only hosts within these IP address ranges can remotely manage and use this bind instance. 192.168.1.0/24 means IP addresses in the range 192.168.1.1 – 192.168.1.255.
The zones, “bubba.net”, “0.0.127.in-addr.arpa”, “1.168.192.in-addr.arpa”, and “.” are all essentially files containing either hostname → IP address mappings or IP address → hostname mappings that this bind server will be responsible for.
Bulding zone files
The first zone: “bubba.net” includes all the hostnames and the IP addresses they point to. So these would be my systems:
  • scully.bubba.net
  • mulder.bubba.net
  • doggett.bubba.net
The second and third zones: “0.0.127.in-addr.arpa” and “1.168.192.in-addr.arpa” are special zones that provide “reverse” name look-ups. These are when you look up a IP address and want to know what name(s) are associated with it. For example:
  • 192.168.1.100
  • 192.168.1.101
The fourth zone: “.” is a special zone that makes this bind server act as a caching name server, for any hostnames and IP addresses that fall outside of the first 3 zones. These hostname and IP address look-ups will be forwarded to my ISPs DNS Server, and the results cached here for subsequent quereies.
Here is what the zone “bubba.net” looks like.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
$TTL 604800
;
; Zone file for bubba.net
;
; Mandatory minimum for a working domain
;
@   IN  SOA ns.bubba.net. hostmaster.bubba.net. (
      2000072802 ; serial
      28800 ; refresh
      7200 ; retry
      3600000 ; expire
      86400 ; default_ttl
      )
@   IN  NS      ns.bubba.net.
@   IN  MX  10  mail.bubba.net.
@   IN  MX  20  mail.bubba.net.
;
; Provide familiar names to services but
; acutally all are coming from XXXXX
; These need to be bound to the address directly, no CNAME's.
; -----------------------------------------------------------
bubba.net.              IN  A 192.168.1.1
;
hostmaster.bubba.net.   IN  A 192.168.1.101
ns.bubba.net.           IN  A 192.168.1.101
...
...
; Subnet 192.168.1 machines
; -------------------------
mulder.bubba.net.       IN  A         192.168.1.1
mulder.bubba.net.       IN  HINFO     "AMD Athlon(tm) Dual Core Processor 4850e 2.2GHz" "LINUX"
mulder.bubba.net.       IN  MX        10  mail.bubba.net.
mulder.bubba.net.       IN  TXT       ""
scully.bubba.net.       IN  A         192.168.1.2
scully.bubba.net.       IN  HINFO     "Celeron 450" "Windows 2000"
scully.bubba.net.       IN  MX        10  mail.bubba.net.
scully.bubba.net.       IN  TXT       ""
...
...
The first 16 lines are pretty standard. These setup a TTL or Time To Live which means how long any of this data should be cache. Line 14 denotes which machine is the SOA, Start of Authority, ns.bubba.net is the master of this domain is what it’s saying 8-). Lines 15 & 16 state which machine is the mail exchange for this domain. I only have one mail exchange so I just list it twice.
The most important section in this file starts with the declaration of hostnames. Each hostname gets 4 lines. A “IN A” line which tells you the IP address for this hostname. The “IN HINFO“ which is just a description of the host itself. The “IN MX” states who the mail exchange is for this host. And finally the “IN TXT“ line which I’m not going to get into today but it is important later on when you’re setting up you mail server.
Here is what the zone “db.192.168.1″ looks like.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$TTL 604800
@   IN  SOA ns.bubba.net. hostmaster.bubba.net. (
      2000072801 ; serial
      28800 ; refresh
      7200 ; retry
      604800 ; expire
      86400 ; default-ttl
      )
@   IN  NS  ns.bubba.net.
;
101   IN  PTR mulder.bubba.net.
102   IN  PTR scully.bubba.net.
103   IN  PTR doggett.bubba.net.
...
...
The first 9 lines are essentially stating how long any Caching DNS Name Server should cache this data before getting a fresh copy. The SOA ns.bubba.net is saying which machine is the authority for this domain. The hostmaster.bubba.net is actually an email address that is responsible for this domain, hostmaster@bubba.net.
Finally the zone “db.127.0.0″ which really serves no purpose other than as a backup in case a host didn’t configure itself correctly with the loopback address. Here is what my zone file looks like:
1
2
3
4
5
6
7
8
9
10
11
$TTL 604800
@               IN      SOA     ns.bubba.net.   hostmaster.bubba.net. (
                        2000031801 ; serial
                        28800 ; refresh
                        7200 ; retry
                        604800 ; expire
                        86400 ; default_ttl
                        )
@               IN      NS      ns.bubba.net.
;
1               IN      PTR     localhost.bubba.net.
The zone “.” uses a file called root.hints. Here is an example of what this file looks like.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
; <<>> DiG 9.3.4-P1 <<>> @e.root-servers.net . ns
; (1 server found)
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 21247
;; flags: qr aa rd; QUERY: 1, ANSWER: 13, AUTHORITY: 0, ADDITIONAL: 14
 
;; QUESTION SECTION:
;.                              IN      NS
 
;; ANSWER SECTION:
.                       518400  IN      NS      D.ROOT-SERVERS.NET.
.                       518400  IN      NS      C.ROOT-SERVERS.NET.
.                       518400  IN      NS      G.ROOT-SERVERS.NET.
.                       518400  IN      NS      L.ROOT-SERVERS.NET.
.                       518400  IN      NS      F.ROOT-SERVERS.NET.
.                       518400  IN      NS      I.ROOT-SERVERS.NET.
.                       518400  IN      NS      B.ROOT-SERVERS.NET.
.                       518400  IN      NS      J.ROOT-SERVERS.NET.
.                       518400  IN      NS      M.ROOT-SERVERS.NET.
.                       518400  IN      NS      K.ROOT-SERVERS.NET.
.                       518400  IN      NS      H.ROOT-SERVERS.NET.
.                       518400  IN      NS      A.ROOT-SERVERS.NET.
.                       518400  IN      NS      E.ROOT-SERVERS.NET.
 
;; ADDITIONAL SECTION:
A.ROOT-SERVERS.NET.     3600000 IN      A       198.41.0.4
A.ROOT-SERVERS.NET.     3600000 IN      AAAA    2001:503:ba3e::2:30
B.ROOT-SERVERS.NET.     3600000 IN      A       192.228.79.201
C.ROOT-SERVERS.NET.     3600000 IN      A       192.33.4.12
D.ROOT-SERVERS.NET.     3600000 IN      A       128.8.10.90
E.ROOT-SERVERS.NET.     3600000 IN      A       192.203.230.10
F.ROOT-SERVERS.NET.     3600000 IN      A       192.5.5.241
F.ROOT-SERVERS.NET.     3600000 IN      AAAA    2001:500:2f::f
G.ROOT-SERVERS.NET.     3600000 IN      A       192.112.36.4
H.ROOT-SERVERS.NET.     3600000 IN      A       128.63.2.53
H.ROOT-SERVERS.NET.     3600000 IN      AAAA    2001:500:1::803f:235
I.ROOT-SERVERS.NET.     3600000 IN      A       192.36.148.17
J.ROOT-SERVERS.NET.     3600000 IN      A       192.58.128.30
J.ROOT-SERVERS.NET.     3600000 IN      AAAA    2001:503:c27::2:30
 
;; Query time: 109 msec
;; SERVER: 192.203.230.10#53(192.203.230.10)
;; WHEN: Fri Feb 20 03:49:01 2009
;; MSG SIZE  rcvd: 500
Starting bind up
Once you have all this in place you can start up bind with this command.
1
/etc/init.d/named start
Configuring a host to use your DNS server
One final step is to configure your host to use your newly setup DNS Server as it’s name server. You can accomplish this by changing 2 files. /etc/resolv.conf and /etc/nsswitch.conf.
The /etc/resolv.conf should look like this:
1
2
search bubba.net
nameserver 192.168.1.101
The /etc/nsswitch.conf should have an entry like this in it:
1
2
3
4
5
...
...
hosts:      files dns
...
...
Taking it out for a test drive
You can see if it’s working by poking the name server using host, dig, or nslookup. Here are a couple of example quereies.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# host example
% host -l bubba.net
bubba.net name server ns.bubba.net.
bubba.net has address 192.168.1.1
...
...
 
# dig example
% dig ns.bubba.net
 
; <<>> DiG 9.3.4-P1 <<>> ns.bubba.net
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 54856
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 0
 
;; QUESTION SECTION:
;ns.bubba.net.                  IN      A
 
;; ANSWER SECTION:
ns.bubba.net.           604800  IN      A       192.168.1.101
 
;; AUTHORITY SECTION:
bubba.net.              604800  IN      NS      ns.bubba.net.
 
;; Query time: 1 msec
;; SERVER: 192.168.1.101#53(192.168.1.101)
;; WHEN: Sat Feb 28 20:54:46 2009
;; MSG SIZE  rcvd: 60
 
# nslookup example
% nslookup ns.bubba.net
Server:         192.168.1.101
Address:        192.168.1.101#53
 
Name:   ns.bubba.net
Address: 192.168.1.101

Readers who viewed this page, also viewed:

#DNS(Domain Name System)

#คำศัพท์ เกี่ยวกับ DNS

ศัพท์
ความหมาย/หน้าที่
Forward Master Zone
แปลงชื่อ Domain เป็น IP Adress
Reverse Master Zone
แปลง IP Adress เป็นชื่อ Domain
BIND
(Berkeley
Internet
Name Domain)
โปรแกรมให้บริการดีเอ็นเอส ที่ใครๆก็ใช้ เหมือนพิมพ์งานก็ต้อง ms word
bind-utils-9.2.4-2
bind-9.2.4-2
bind-libs-9.2.4-2
ypbind-1.17.2-3
bind-chroot-9.2.4-2
Master Name Sever


Slave Name Server
Name Server มีสองแบบคือ Master Name Sever และ
Slave Name Server ซึ่งจะต้องอยู่ต่างเครื่องกัน

Master Name Server จะอ่านข้อมูลประจำโซนจากแฟ้มใน Disk และมีสิทธิหน้าที่ในโซนนั้นๆ

Slave Name Server สำเนาข้อมูลจาก Master Name Server
named
โปรแกรมบริการชื่อ Domain บน Linux

/usr/sbin/named -u named -t /var/named/chroot
Host
เครื่องคอมพิวเตอร์ศูนย์กลางที่ต่อเข้ากับ Network,Internet เพื่อบริการใดๆ
Domain
ชื่อ web address/computer
ทำให้เข้าใจ/จำง่าย/มีความหมาย กว่า IP Address

ถ้าเรียก โดเมน เหมือนเรียก IP Address

http://www.icann.org/
Sub Domain
ส่วนย่อยของ Domain เช่น mail.dns.linuxstep.com
mail คือ Sub Domain
Localhost
เครื่องเราเอง จะตั้งค่า ip address อะไร แต่เมื่อเรียก
Localhost ก็จะเรียกเครื่องเราเอง

IP Address 127.0.0.1 = Localhost
IP(Internet Protocal)
IP หรือ Internet Protocal
IP Address คือรหัสตัวเลขที่กำหนดขึ้น เพื่อใช้อ้างถึงที่อยู่ของ Host และ Network
ตัวเลขเหล่านี้จะไม่ซ้ำกัน (unique numeric) ซึ่งมีมาตรฐานในการกำหนด และเป็นที่รู้จักทั่วโลก IPV4,IPV6
Port ช่องทาง ที่ใช้ติดต่อกับ computer/อุปกรณ์ ใน network
Loopback

(Address
Localhost)
สำหรับ window กำหนด loopback IP=127.0.0.1 เมื่อส่งสัญญาณออกจะไม่ส่งออกนอกเครือข่าย แต่จะกลับมา
ที่ต้นทาง ด้วย loop back driver

http://www.certiguide.com/netplus/
cg_np_IVLoopbackAddressingandConnections.htm
Root Domain
ลำดับบนสุดของระบบ DNS โดยมีการกำหนดดังนี้
ลำดับที่ 1 root
ลำดับที่ 2 .com, .net, .edu, .gov, .org, .th, ...
ลำดับที่ 3 subdomain ตัวอย่าง mail3.linuxstep.com
mail3 =subdomain

ตัวอย่างลำดับ DNS
www2.dns.linuxstep.com
Name Resolution
การที่ DNS Server เก็บหมายเลข IP Address คู่กับชื่อเครื่องแบบ Domain Name เพื่อทำการค้นหาชื่อตามการร้องขอของเครื่อง         DNS client
DNS Server
เครื่องที่คอยจัดการ/ดูแล/แก้ไข domain subdomain
Client
เครื่องลูกข่าย
Resolver
กลไกการแปลง domain เป็น ip address
Reverse File
/etc/resolve.conf

การ resolve หมายถึง
DNS Zone
การจัด domain เป็นพื้นที่ย่อย
TCP,UDP,ARP

เป็น protocoll ที่ DNS ใช้ในการติดต่อผ่าน port 53

TCP - ปรับปรุงข้อมูล
UDP - ส่งคำขอข้อมูล
ARP - แปลงค่า IP address เป็น Hardware

#Files ที่ใช้ในการ Config
Files Config
ความหมาย / หน้าที่
/etc/named.conf
กำหนดการทำงานของ Name Server สำคัญมาก
/var/named/named.ca เก็บข้อมูลชื่อ Server ซื่งเป็น Root Name Server
ไม่ควรแก้ไขข้อมูลใน file นี้เป็นอันขาด
/var/named/named.local เก็บข้อมูลการ map ชื่อ กับ IP Address แบบ Reverse Lookup
/var/named/dm.com.zone ระบุชื่อ Server,Zone, ทำ Forward Lookup
/var/named/ 203.203.203.203.zone
Reward Lookup
/etc/hosts
เก็บชื่อ host
/etc/resolve.conf
เก็บชื่อ domain และ ip ที่ทำงานแบบ Forward Lookup
/etc/sysconfig/network เก็บชื่อ host ของเครื่อง
/etc/sysconfig/named กำหนดตำแหน่งของ named ที่เป็น file config

#ความหมายของ SOA Resource Record
SOA (Start to authority)
เริ่มให้สิทธิ/อำนาจ
ความหมาย / หน้าที่
@
Domain Name ที่ได้จาก /etc/named.comf
IN Internet
serial
เป็นหมายเลขที่ใช้แสดงการอัพเดทข้อมูลระหว่าง Primary master และ Secondary master
refresh
ระยะเวลา refresh ข้อมูล
retry
ระยะเวลา ตรวจสอบข้อมูลกับ Primary DNS Server
expire
ระยะเวลา หมดอายุ กรณีติดต่อกับ Primary DNS Server ไม่ได้

#ความหมายข้อมูลใน Fields Type DNS Record
Type
ความหมาย / หน้าที่
A
แปลงชื่อ Domain เป็น IP Address แบบ ipv4
NS
Name Server record กำหนดว่า server ใดรับผิดชอบ domain
PTR
Pointer record ใช้ Map IP Address เป็น Host
MX
Mail Exchanger
ใช้ระบุชื่อเครื่อง Mail Server ของ Domain
เมื่อเราต้องกำหนด MX เราจะพบว่าต้องกำหนด
MX 10 mail1.example.com
MX 20 mail2.newdomain.com
ค่าเลข 10 และ20 นั้นหมายถึง Priority ของ Mail โดยค่าน้อยสุดจะสำคัญสุด

ชื่อหลัง MX ต้องเป็น Domain เท่านั้นและไม่ใช่ CName
CNAME
Cononical name ใช้เป็น Alias name ให้เป็น host จริง
เช่น ns,mail,www,www2
;
Remark

#คำสั่ง Linux เกี่ยวกับ DNS

Command
ความหมาย / หน้าที่ / ตัวอย่าง
vi
editor แก้ไข file
named-checkconf
ตรวจ error named.conf
e.g.
#named-checkconf /etc/named.conf
named-checkzone
ตรวจ error zone
e.g.
#named-checkzone lp.com /var/named/lp.com.zone
service network restart
reboot network service
service named restart
reboot named service
nslookup
ค้นหา domain,ip address รวมทั้งข้อมูลต่างๆ

[root@l203 root]# nslookup
> www.li203.com
Server: 203.156.167.203
Address: 203.156.167.203#53
www.li203.com canonical name = l203.li203.com.
Name: l203.li203.com
Address: 203.156.167.203
> 203.156.167.203
Server: 203.156.167.203
Address: 203.156.167.203#53

203.167.156.203.in-addr.arpa name = l203.li203.com.
> mail.li203.com
Server: 203.156.167.203
Address: 203.156.167.203#53

mail.li203.com canonical name = l203.li203.com.
Name: l203.li203.com
Address: 203.156.167.203
dig update ข้อมูลให้ทันสมัย
e.g.
dig > /var/named/named.ca
host
แสดง domain และ ip address
e.g.
[root@l203 root]# host www.li203.com
www.li203.com is an alias for l203.li203.com.
l203.li203.com has address 203.156.167.203

#Forward DNS
ขั้นตอนที่
ความหมาย / หน้าที่
1
แก้ named.conf

zone "domain.com" IN {
type forward;
forwarders { 203.156.138.45; };
};

#Redirect domain
ขั้นตอนที่
ความหมาย / หน้าที่
1
ให้แก้ index.html หรือ index.php

<html>
<head>
<META HTTP-EQUIV="Refresh" CONTENT="5;URL=http://www.domain.com">
</head>
<body>
<center>
ืำnew website
<a href="http://www.domain.com">www.domain.com</a>
</center>
</body>
</html>


จาก www.itwizard.info

การทำเครื่องเป็น Primary DNS หรือ Master DNS
   ทำไมต้องทำ เพราะต้องการให้เครื่องเป็นเครื่องหลักของ dns และอีกเหตุผลก็คือconfig ให้ support กับ Secondary
จากการconfig ธรรมดาแล้วให้กำหนดค่าจาก named.conf

zone "abcd.com" { -->ถ้ากรอก www.abcd.com มาจะเข้า zone นี้
type master; -->เป็นเครื่อง Primary Server หรือ master
file "abcd.com.zone"; -->ไฟล์ที่ทำการ Forward ชื่อเครื่อง ไปเป็น ipของ server
allow-transfer{192.168.101.3;}; -->คำสั่งเพื่อให้ transfer ไปยัง ip address เครื่อง Secondary Server Server เป็นการ ระบุ Domain ที่ต้องการทำ Backup
allow-query{any;}; -->query ทุกๆอย่างที่มีอยู่ใน Primary Server

การทำให้เครื่องเป็น Secondary DNS
   ทำไมต้องทำเมื่อต้องการสำรอง DNS เราต้องรับค่ามาจาก Master DNS ให้กำหนดค่าใน named.conf

zone "abcd.com" {--> ถ้ากรอก www.abcd.com มาจะเข้า zone
type slave; -->เป็นเครื่อง Secondary Server หรือ slave
file "slaves/abcd.com.zone"; -->ไฟล์ที่ทำการ Forward ชื่อเครื่อง ไปเป็น ipของ server อ้างอิงไปยังไฟล์ของเครื่อง Primary Server
masters { 192.168.101.213;}; -->กำหนด ip เครื่อง Primary Server เป็นการ ระบุ Domain ที่ต้องการทำ Backup
allow-query{ any;}; -->query ทุกๆอย่างที่มีอยู่ใน Primary Server
};


เมื่อเราดูจากตำแหน่ง file แล้วเราจะเห็น /slaves
[root@l2023 ~]# cd /var/named/chroot/var/named/
[root@l2023 named]# ll
-rw-r----- 1 root named 448 Mar 11 08:03 202.29.75.zone
-rw-r----- 1 root named 442 Sep 30 2005 203.156.167.zone
-rw-r----- 1 root named 575 Mar 12 06:01 afaps2023.com.zone
drwxrwx--- 2 named named 4096 Aug 26 2004 data
-rw-r----- 1 root named 587 Mar 12 06:28 li2023.com.zone
-rw-r----- 1 root named 195 Sep 30 2005 localhost.zone
-rw-r----- 1 root named 2499 Sep 30 2005 named.ca
-rw-r----- 1 root named 461 Mar 12 06:36 named.local
drwxrwx--- 2 named named 4096 Jul 27 2004 slaves
[root@l2023 named]# cd slaves/
[root@l2023 slaves]# ll
total 0
[root@l2023 slaves]# cd ..
[root@l2023 named]# ls
202.29.75.zone 203.156.167.zone afaps2023.com.zone data li2023.com.zone localhost.zone named.ca named.local slaves
[root@l2023 named]# cd data
[root@l2023 data]# ll
total 0


การทำสอบ Master DNS และ Secondary DNS
#service named restart
ดูภาพ http://www.linuxstep.com/tips/20070410004/dnsserver/image020.jpg

#หลักการทำงานของ DNS

ขั้นตอนที่
ความหมาย / หน้าที่
1
client(ผู้เล่น) ต้องการเข้า www.linuxstep.com เครื่อง client จะขอ IP Address ไปที่ DNS Server (ถ้าไม่พบ IP Address) จะขอ IP Address กับ DNS Server ในลำดับที่สูงกว่า เช่น ISP
2
DNS Server ของ ISP ค้นหา IP Address ต่อไป ถ้าไม่พบ ก็ต้องส่งต่อไปที่
Root Domain Server ที่ Root Domain Server ต้องเจอแน่นอนเพราะเป็นสถานที่สุดท้ายแล้ว
3
เมื่อเจอก็จะได้ IP Address ของ www.linuxstep.com และแจ้งไปที่ Domain Server ของ ISP ทราบ และจัดเก็บไว้ด้วย
4
Domain Server ของ ISP ก็ส่งต่อไปที่ DNS Server ของ Client และแจ้ง IP Address ไปที่ client (ผู้เล่น)
5
เมื่อ client ได้ IP Address ของ www.linuxstep.com แล้ว ก็ติดต่อกับ
IP Address ได้โดยตรง โดยการรับส่งข้อมูลจะใช้ Protocol TCP

#ชื่อเฉพาะที่ได้กำหนดเกี่ยวกับ DNS
.mil
ทหาร
.com
การค้า
.gov
รัฐบาล
.net
internet
.info
ข้อมูล ข่าวสาร
.org องค์กร
.edu การศึกษา
.co บริษัท
.arts ศิลปะ
.rec องค์กร
.nom บุคคลทั่วไป
.web websote
.ac สถานศึกษา
.store บริษัทที่ทำการค้า

FAQ

คำถามที่พบบ่อยเกี่ยวกับ DNS
คำถาม
ตอบ
1. port ใดที่ใช้กับ DNS
53
2.หน่วยงานใดรับผิดชอบ DNS
ระดับโลก คือ Internic
ประเทศไทย คือ thnic
3. TT L คืออะไร
Tile To Live เวลาที่กำหนดให้ปรับปรุงข้อมูลเป็น วินาที
4. DNS ย่อมาจาก
Domain Name System

บางคนก็เขียน Domain Name Service ซึ่งผมเห็นไม่มากแต่ก็
เห็นในหนังสือบางเล่ม ใครตอบได้ช่วยบอกทีครับ
5 ip address 1 ตัว กำหนดให้ได้
หลาย domain ได้หรือไม่
ตามหลักการ DNS ของ Linux ทำได้ครับ เพราะใช้ Virtual แทน
กรณีมีหลายชื่อ domain
6.โปรแกรมอะไรจัดการ DNS ถ้า Redhat คือ bind

ถ้าเป็น centOS5
bind-chroot-9.3.3-10.el5
system-config-bind-4.0.3-2.el5.centos
ypbind-1.19-8.el5
bind-9.3.3-10.el5
bind-utils-9.3.3-10.el5
kdebindings-3.5.4-1.fc6
bind-libs-9.3.3-10.el5
7.การศึกษาการทำ DNS ยากหรือไม่เพราะบางคนไม่สามารถ แก้ได้ ยากหรือไม่แล้วแต่คน ผมแนะนำครับ
ควรจะมีความตั้งใจและหาคนแนะนำ
8.domain redirect คือ การแปลงชื่อให้สั้นลงหรือให้เป็นชื่ออื่น
10.จำ config อย่างไร ให้ dns
ค้นได้ด้วย google
อยู่ที่การเขียน code html
11.master name server คือ เป็นฐานข้อมูลหลักของ Domain แก้ไขได้ที่เดียว
12.slave name server คือ สำเนาของ master name server
13.root name server คือ ลำดับบนสุดของ domain
14.ใน windows server มีการ
กำหนด dns หรือไม่
มีครับลองไปศึกษาดูได้จาก windows2003 R
15.webmin จัดการ dns มีหรือไม่ มีครับ www.webmin.com , www.rpmfind.net
โปรแกรม caching-nameserver*.rpm

 
 
 
หากคนไทยหันมาฆ่ากันเอง จะร้องเพลงชาติไทยให้ใครฟัง 


วิธี Configure IP Address บน Linux และ คำสั่งเกี่ยวกับ Network ที่น่าสนใจ

เมื่ออาทิตย์ที่แล้วผมต้องติดตั้ง Ubuntu 8.04 Server Edition ลงบนเครื่องเซิร์ฟเวอร์ Dell PowerEdge 200 ที่เพิ่งซื้อมาใหม่ทั้ง 3 เครื่อง สิ่งหนึ่งที่ผมต้องทำระหว่างติดตั้งก็คือการ Configure IP Address และการใช้คำสั่งเกี่ยวกับ Network บน command line...
ผมเห็นว่าวิธีการ Configure IP Address และการใช้ network command บน Linux จริง ๆ แล้วมันก็ไม่ได้น่ากลัวอะไร แต่ผู้ใช้มือใหม่ไม่กล้าใช้กัน ประกอบกับผมไปเจอบทความดี ๆ เข้า ก็เลยผมอยากแปลเพื่อเผยแพร่และรวมรวมคำสั่งเกี่ยวกับ network ต่าง ๆ ที่น่าสนใจ เพื่อที่จะได้เป็นประโยชน์กับผู้อื่นดีกว่า
หมายเหตุ: ระบบที่ผมทดลองคือ Ubuntu ดังนั้นคำสั่งบางคำสั่งอาจจะใช้ได้เฉพาะ Linux ตระกูล Debian เท่านนั้น

วิธีการกำหนด IP Addressให้ network adapter

Linux มีไฟล์หนึ่งที่ชื่อว่า /etc/network/interfaces ซึ่งใช้เก็บ configuration ของ network interfaces แต่ละอันในเครื่อง ดังนั้นหากเราจะ Configure IP Address ก็จะต้องแก้ไขไฟล์ดังกล่าว
วิธีการกำหนด IP Address โดยทั่วไปก็มีอยู่ 2 วิธี ได้แก่:
  1. Static IP Configuration - IP Address จะถูกกำหนดโดยผู้ดูแลระบบ ซึ่งมีวิธีการ configure ดังนี้:
    • เปิด network configuration file (/etc/network/interfaces) ด้วย Text Edition ที่คุณถนัด
      $ sudo vim /etc/network/interfaces
    • เพิ่ม Static IP Configuration สำหรับ network adapter สมมติว่าค่าต่าง ๆ ที่ผมต้องการคือ:
      • network adapter คือ eth0
      • IP Address คือ 192.168.0.70
      • Netmask คือ 255.255.255.0
      • Network คือ 192.168.0.0
      • Broadcast IP Address คือ 192.168.0.255
      • Gateway คือ 192.168.0.1
      ผมก็เติมบรรทัดเหล่านี้เข้าไปในไฟล์ /etc/network/interfaces
      iface eth0 inet static address 192.168.1.70 netmask 255.255.255.0 network 192.168.0.0 broadcast 192.168.0.255 gateway 192.168.0.1
    • บันทึกไฟล์และปิด Text Editor แล้วจึง restart networking services
      $ sudo /etc/init.d/networking restart
  2. DHCP network configuration - IP Address จะถูกกำหนดโดย DHCP Server ที่อยู่ใน network ซึ่งมีวิธีการ configure ดังนี้:
    • เปิด network configuration file (/etc/network/interfaces) ด้วย Text Edition ที่คุณถนัด
      $ sudo vim /etc/network/interfaces
    • เพิ่ม DHCP network Configuration สำหรับ network adapter เข้าไปในไฟล์ /etc/network/interfaces
      iface eth0 inet dhcp
    • บันทึกไฟล์และปิด Text Editor แล้วจึง restart networking services
      $ sudo /etc/init.d/networking restart

วิธีการกำหนด DNS Server

เราสามารถเพิ่มลด DNS Server ที่เราใช้ในการ look-up IP Address ได้โดยการแก้ไขไฟล์ /etc/resolv.conf
สมมติว่าคุณต้องการเพิ่ม 203.144.207.49 เป็น Primary DNS Server
  • เปิดไฟล์ /etc/resolv.conf ด้วย Text Edition ที่คุณถนัด
    $ sudo vi /etc/resolv.conf
  • เพิ่มบรรทัดต่อไปนี้เข้าไปก่อนหน้าบรรทัดอื่น ๆ ที่ขึ้นต้นด้วยคำว่า nameserver
    nameserver 203.144.207.49
หมายเหตุ: ในการ resolve IP Address นั้นจะเริ่มจาก name server ตัวแรกที่ปรากฎในไฟล์ก่อน แล้วค่อยไปค้นหาใน name server ตัวต่อ ๆ ไป

วิธีการ Resolve IP address

$ host www.yannarak.net

วิธีการจับคู่ IP Address กับ Hostname ด้วย Hosts file

ในบางกรณี คุณไม่สามารถใช้ DNS Server ในการ resolve IP ได้ ยกตัวอย่างเช่น localhost เป็นต้น ดังนั้นในระบบปฎิบัติการจึงต้องมีวิธีการที่ใช้ในการจับคู่ระหว่าง IP Address กับ Host name แบบ static โดยไม่ใช้ DNS Server ด้วย สำหรับ Linux มีไฟล์ที่ชื่อว่า /etc/hosts ซึ่งใช้ในการทำงานดังกล่าว
ตัวอย่างไฟล์/etc/hosts
127.0.0.1 localhost pegasus 192.168.1.35 www.mywebsite.com
รูปแบบของไฟล์ /etc/hosts คือ IP address เป็นฟิลด์แรก และ ฟิลด์ต่อ ๆ ไป คือ hostname ที่จะจับคู่กับ IP ดังกล่าว แต่ละฟิลด์จะคั่นด้วย white-space character ดังนั้น จากไฟล์ตัวอย่าง เราสามารถแทน IP 127.0.0.1 ได้ด้วย 2 hostname คือ localhost และ pegasus ส่วน IP 192.168.1.35 เราสามารถแทนได้ด้วย hostname เพืยงอันเดียวคือ www.mywebsite.com เป็นต้น

วิธีการแสดงสถานะของ network adapter

หลังจากที่เราได้ configure IP address ให้กับ network adapter แล้ว หากเราต้องการตรวจสอบสถานะของ network adapter นั้น ๆ ตรงตามที่เราต้องการหรือไม่เราก็สามารถตรวจสอบได้โดยใช้ คำสั่ง ifconfig ดังตัวอย่างต่อไปนี้:
$ ifconfig eth0
ต่อไปนี้เป็นตัวอย่าง ผลลัพธ์ที่ได้จากคำสั่งดังกล่าว
eth0 Link encap:Ethernet HWaddr 00:1e:37:ab:cd:ef inet addr:192.168.0.70 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::21e:37ff:fe8e:7ae1/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:353 errors:0 dropped:0 overruns:0 frame:0 TX packets:442 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:100 RX bytes:191447 (186.9 KB) TX bytes:128793 (125.7 KB) Base address:0x1840 Memory:fe000000-fe020000
ถ้าหากต้องการให้แสดงสถานะของ network interface ทุกอันในเครื่อง ก็ทำได้ง่ายเพียงแค่สั่งคำสั่ง ifconfig เฉย ๆ โดยไม่ต้องส่ง parameter ใด ๆ

วิธีการเปลี่ยน IP Address และ Netmask

คำสั่ง ifconfig นอกจากจะใช้ในการแสดงสถานะของ network adapter แล้ว ยังสามารถใช้ในการเปลี่ยน IP Address และ Netmask ของ network adapter ได้อีกด้วย
สมมติว่าเราต้องการเปลี่ยน IP Address ของ eth0 ให้เป็น 10.10.13.64 และ Netmask เป็น 255.255.0.0 ก็ทำได้ด้วยคำสั่งต่อไปนี้:
$ sudo ifconfig eth0 10.10.13.64 netmask 255.255.0.0 up

วิธีการเปิด/ปิด การทำงานของ network adapter

นอกจากนี้ คำสั่ง ifconfig ก็ยังเป็นคำสั่งที่ใช้ในการเปิด/ปิด network adapter อีกด้วย
คุณสามารถเปิดการใช้งาน network adapter ด้วยคำสั่ง:
$ sudo ifconfig eth0 up
หรือ
$ sudo ifup eth0
คุณสามารถปิดการใช้งาน network adapter ด้วยคำสั่ง:
$ sudo ifconfig eth0 down
หรือ
$ sudo ifdown eth0

วิธีการแสดง routing table

คุณสามารถใช้คำสั่งต่อไปนี้ในการแสดง routing table ได้
$ /sbin/route
หรือ
$ /sbin/route -n
ตัวอย่างผลลัพธ์ของคำสั่ง route
Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 eth0 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0

วิธีการกำหนด Default Gateway

$ sudo route add default gw 172.16.236.0
ถ้าหากเครื่องที่คุณต้องการกำหนด Default Gateway ได้ถูกกำหนด Default Gateway ไว้แล้วคุณต้องลบ route สำหรับ Default Gateway อันเดิมก่อนด้วยคำสั่งต่อไปนี้ก่อนที่จะเรียกคำสั่งด้านบน
$ sudo route del default

วิธีการแสดงรายการ Active Internet Connections (ทั้ง server และ connection ที่เชื่อมต่อเรียบร้อยแล้ว)

$ netstat -nat

วิธีการแสดงว่า Port อะไรกำลังเปิดอยู่บ้าง

$ sudo netstat -tulp
หรือ
$sudo netstat -tulpn

วิธีการแสดงข้อมูลสถิติของแต่ละ network adapter

$sudo netstat -i
ที่มา: Howto: Ubuntu Linux convert DHCP network configuration to static IP configuration

Wednesday, September 28, 2011

Setting virtual ip in centos

Virtual ip is an ip that bind to a physical ethernet card that already have physical ip address. To set virtual ip in centos, follow the below steps:

1. We will add virtual ip to eth0. Below is original configuration of /etc/sysconfig/network-scripts/ifcfg-eth0. ifcfg-eth0 is the original configuration file for eth0.
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
BOOTPROTO=static
BROADCAST=192.168.0.255
HWADDR=00:0C:29:C8:AA:7C
IPADDR=192.168.0.10
NETMASK=255.255.255.0
NETWORK=192.168.0.0
ONBOOT=yes
TYPE=Ethernet

2. Create new virtual configuration file by copying and editing(change IPADDR and remove HWADDR) the ifcfg-eth0 configuration file

# cd /etc/sysconfig/network-scripts
# cp ifcfg-eth0 ifcfg-eth0:0
# vi ifcfg-eth0:0

BOOTPROTO=static
BROADCAST=192.168.0.255
IPADDR=192.168.0.11
NETMASK=255.255.255.0
NETWORK=192.168.0.0
ONBOOT=yes
TYPE=Ethernet


3. Restart network
# /etc/init.d/network restart

4. Check your new virtual ip using ifconfig
# ifconfig

Saturday, September 24, 2011

ติดตั้ง web server บน CentOS ด้วยคำสั่ง YUM

ง่ายนิดเดียวครับสำหรับ การติดตั้ง web server บน CentOS ส่วนประกอบที่สำคัญในการติดตั้งก็มี apache(httpd) mysql php แค่นี้ก็เพียงพอต่อการทำ web server แบบไม่เน้น config อะไรให้ยุ่งยาก

   YUM ย่อมาจาก (Yellow dog Updater, Modified) เป็นตัวช่วยในการติดตั้งและอัปเกรดแพ็กเกจแบบ RPM แบบอัตโนมัติ ในปัจจุบันการติดตั้งแพ็กเกจแบบ yum นับวันได้รับความนิยมมากขึ้นเรื่อยๆ เนื่องจากมีความง่ายในการใช้งาน

เครื่องให้เตรียมพร้อม
1 ติดตั้ง CentOS ไว้ก่อน
2 เซ็ต network IP DNS Gateway ให้พร้อม
3 เช็ค ว่าเราสามารถออกเน็ทได้ไหม
4 ใช้คำสั่ง YUM ติดตั้ง web server ได้เลยครับ

1 ติดตั้ง CentOS
   การติดตั้ง CentOS อันนี้ผมจะไม่ขออธิบายการติดตั้งให้ถามที่ อากู๋ เอานะครับ แล้วเดียวจะเขียนบทความให้ทีหลัง ตอนนี้ CentOS ก็ออกมาถึง version 6 แล้ว แต่ผมยังใช้ 5.4 5.6 อยู่เลย เอาไว้ version 6 นิ่งแล้วจะมา อธิบายการติดตั้งละกันครับ

2 เซ็ต network IP DNS Gateway
   2.1 เซ็ต IP Address
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
   กด i (i คือ insert) แล้วพิมพ์ตามข้างล่างนี้เลยครับ
DEVICE=eth0
BOOTPROTO=static
BROADCAST=192.168.1.255
HWADDR=00:60:DF:31:13:3D
IPADDR=192.168.1.201
NETMASK=255.255.255.0
NETWORK=192.168.1.0
ONBOOT=yes

   กด Esc แล้วตามด้วย :qw Enter
   2.2 เซ็ต network DNS
# vi /etc/resolv.conf
   กด i (i คือ insert) แล้วพิมพ์ตามข้างล่าง
nameserver 8.8.8.8
nameserver 8.8.4.4

   DNS ที่ผมใช้ เป็นของ google หรือจะใส่ DNS ของคุณที่ใช้อยู่ก็ได้

   2.3 เซ็ต network Gateway (ของผมใช้ 192.168.1.1 ให้คุณเปลี่ยนเป็นของคุณเอง)
# vi /etc/sysconfig/network
   กด i (i คือ insert) แล้วพิมพ์เพิ่มต่อท้ายหรือถ้ามีแล้วให้แก้ไข
GATEWAY=192.168.1.1
   หรือใช้
# route add default 192.168.1.1 eth0
   ตรวจสอบ gateway ใช้คำสั่ง
# route
   Output
[root@CentOSLab ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0

3 ตรวจสอบว่าสามารถออกสู่ภายนอกได้หรือยัง
# ping www.google.com
   Output
[root@CentOSLab ~]# ping www.google.com
PING www.l.google.com (74.125.71.105) 56(84) bytes of data.
64 bytes from hx-in-f105.1e100.net (74.125.71.105): icmp_seq=1 ttl=47 time=115 ms
64 bytes from hx-in-f105.1e100.net (74.125.71.105): icmp_seq=2 ttl=47 time=104 ms
64 bytes from hx-in-f105.1e100.net (74.125.71.105): icmp_seq=3 ttl=47 time=115 ms

4 ติดตั้ง web server ได้เลยครับ
# yum -y install httpd php mysql mysql-server mysql-devel php-mysql
ลองมาสร้างไฟล์ php เพื่อตรวจสอบการทำงาน PHP โดยใช้คำสั่งตามนี้ครับ
# vi /var/www/html/phpinfo.php
   กด i (i คือ insert) แลว้พิมพ์ตามข้างล่างนี้เลยครับ
<?php
phpinfo();
?>

   กด Esc แล้วตามด้วย :qw Enter

ไปที่ browser พิมพ์ URL http://ipserrver/phpinfo.php
สำเร็จเรียบร้อยแล้วครับสำหรับการติดตั้ง web server บน CentOS

Thursday, August 25, 2011

วิธีติดตั้งเซิร์ฟเวอร์ Ubuntu 9.10 สำหรับคนไม่เคยใช้ลินุกซ์


[ เวอร์ชัน 10.04 คลิกที่นี่ ]
Ubuntu เป็นระบบปฎิบัติการสายพันธ์ลีนุกซ์ที่ไม่ใหญ่โตมากมาย สังเกตได้ว่า ใช้ซีดีแค่แผ่นเดียวเท่านั้น การติดตั้งและใช้งานค่อนข้างง่ายกว่าลีนุกซ์สายพันธ์อื่น จึงเหมาะสำหรับองค์กรขนาดเล็กถึงกลางที่ไม่ต้องการอะไรที่ยุ่งยากซับซ้อน

เปรียบเทียบ Windows กับ Ubuntu

OSDatabaseWeb Applicationรวมค่าลิขสิทธิ์
Windows ชุด ServerSQL ServerIIS + ASP + .NETหลักหมื่นถึงแสนต่อเครื่อง
Ubuntu ชุด ServerMySQL, PostgreSQLApache + PHP + Monoฟรีทุกเครื่อง
ดาวน์โหลด แผ่นซีดี (ไฟล์ .iso) ฟรีได้ที่ http://www.ubuntu.com
ว่าแต่...ทำไมผมยังชอบใช้วินโดวส์ ? " วินโดวส์ทำเรื่องยากให้ง่าย ลีนุกซ์ทำเรื่องง่ายให้ (ยุ่ง) ยาก "



ขั้นตอนการติดตั้งและปรับแต่งลีนุกซ์ Ubuntu 9.10 แบบรวบรัด


หากท่านใดต้องการติดตั้งลินุกซ์บนวินโดวส์ในเครื่องเดียวกัน แนะนำให้ติดตั้งโปรแกรมประเภท Virtual Machine อาทิ Vmware ชุด Workstation (ลิขสิทธิ์) ชุด Player (ฟรี), Microsoft Virtual PC (ฟรี), VirtualBox (ฟรี) ฯลฯ

I. ติดตั้ง Ubuntu ชุด Server

  • เลือก Language: English (ไม่แนะนำให้เลือก Thai เพราะจะยุ่งยากในการคอนฟิกภาษาไทยในภายหลัง)
  • เลือก Install Ubuntu Server
  • เลือก Language: English (ขั้นตอนนี้กำหนดดีฟอลต์ locale เป็น en_US.UTF-8)
  • เลือก Country: Other
  • เลือก Region: Asia/Thailand
  • เลือก Keyboard: Thailand
  • ตั้งชื่อเครื่อง (Hostname) อะไรก็ได้ แต่อย่าให้ชื่อซ้ำกับเครื่องอื่นในวงแลน ตัวอย่าง ubuntu
  • แบ่ง Partition เลือก use entire disk
  • กำหนดชื่อผู้ใช้งานและรหัสผ่าน (Username/Password) ตัวอย่าง somchai/1234
    โปรดอ่าน: ***สำคัญมาก จดไว้อย่าลืม เพราะต้องใช้ในการล็อกอินเข้าใช้งานเในครั้งแรก

  • เลือก No automatic updates
  • ติดตั้ง LAMP, OpenSSH, SAMBA
    สำหรับ PostgreSQL นั้น หากต้องการให้ภาษาไทยถูกต้อง อย่าคลิกเลือกในขั้นตอนนี้ (ดูวิธีติดตั้งในส่วนถัดไป)
    Ubuntu 9.10 (Karmic Koala) : LAMP = Linux 2.6.31-14 + Apache 2.2.12 + Mysql 5.1.37 + PHP 5.2.10-2
  • กำหนดรหัสผ่านของ MySQL root ตัวอย่าง 1234
    โปรดอ่าน: ***สำคัญมาก จดไว้อย่าลืม เพราะต้องใช้ในการล็อกอินเข้าใช้งาน MySQL
  • รอ...จนติดตั้งเสร็จ (ประมาณห้านาที)
  • เข้าสู่หน้าจอล็อกอิน
    ใส่ชื่อผู้ใช้งานและรหัสผ่านที่กำหนดไว้ตอนต้น ตัวอย่าง somchai/1234
  • ตั้งรหัสผ่านของ root คำสั่ง sudo passwd root
    หากต้องการเปลี่ยนเป็น root (เวลารันคำสั่งไม่ต้องมี sudo นำหน้า)
    คำสั่ง su root
  • กำหนด IP Address แบบ Static ตัวอย่าง 192.168.1.10/255.255.255.0
    sudo nano /etc/network/interfaces
    auto eth0
    iface eth0 inet static
    address 192.168.1.10
    netmask 255.255.255.0
    network 192.168.1.0
    broadcast 192.168.1.255
    gateway 192.168.1.254
    
    sudo /etc/init.d/networking restart
    ทดสอบคำสั่ง ping -c 4 google.co.th

  • กรณีที่ทำการข้ามขั้นตอน Configurating apt ของการติดตั้ง Ubuntu ในตอนต้น หรือหากไม่แน่ใจว่าได้ติดตั้ง package ต่าง ๆ ถูกต้องหรือไม่ ก็ให้สั่งรันอัปเดท package ใหม่อีกครั้งก็ได้ตามสะดวก คำสั่ง sudo apt-get update
  • คำสั่งที่จำเป็นต้องรู้ (ปกติผมก็ใช้อยู่แค่นี้ล่ะ) shutdown now
    reboot

II. ปรับแต่ง Ubuntu

ขั้นตอนต่อไปจะทำการคอนฟิกเซิร์ฟเวอร์ Ubuntu บนวินโดวส์ผ่าน Telnel

ติดตั้งโปรแกรม PuTTY

ดาวน์โหลดที่ http://www.putty.org (ฟรี)
การติดตั้งโปรแกรม Telnet Client เช่น PuTTY บนวินโดวส์ก็เพื่อความสะดวกในการรันคำสั่งบนเซิร์ฟเวอร์ที่อยู่คนละเครื่องกัน ซึ่งเวลาค้นหาคำสั่งของ Linux ต่าง ๆ จากเว็บไซด์เช่น Google ก็ copy คำสั่งที่ต้องการ แล้วคลิกขวามา paste ใส่ใน PuTTY เลยสะดวกกว่า
  • ระบุ IP Address ของเครื่อง Ubuntu ตัวอย่าง 192.168.1.10 SSH พอร์ต 22
  • ใส่ชื่อผู้ใช้งานและรหัสผ่านที่กำหนดไว้ตอนต้น ตัวอย่าง somchai/1234 (หรือ root ก็ได้ตามสะดวก)

    หมายเหตุ: หากตอนติดตั้ง Ubuntu ไม่ได้คลิกเลือก OpenSSH ก็ให้รันคำสั่ง
    sudo apt-get install ssh openssh-server


ทดสอบ Apache

  • เปิดเว็บบราวเซอร์ เช่น IE, Firefox, Opera เป็นต้น ระบุ URL ไปที่ Ubuntu ตัวอย่าง http://192.168.1.10


ทดสอบ PHP

  • รันคำสั่งใน PuTTY เพื่อสร้างไฟล์เอกสารสำหรับทดสอบ PHP ดังนี้ sudo nano /var/www/phpinfo.php
    ใส่ข้อความดังนี้
    <?php phpinfo(); ?>
    
  • เปิดเว็บบราวเซอร์ เช่น IE, Firefox, Opera เป็นต้น ระบุ URL ไปที่ Ubuntu ตัวอย่าง http://192.168.1.10/phpinfo.php

  • ตรวจสอบดูว่ามีส่วน MySQL หรือไม่ ?

ติดตั้ง phpMyAdmin

การติดตั้ง phpMyAdmin ก็เพื่อให้สามารถใช้งาน MySQL ผ่านเว็บบราวเซอร์
  • รันคำสั่งใน PuTTY ดังนี้ sudo apt-get install phpmyadmin
  • เปิดเว็บบราวเซอร์ เช่น IE, Firefox, Opera เป็นต้น ระบุ URL ไปที่ Ubuntu ตัวอย่าง http://192.168.1.10/phpmyadmin

    ใส่ชื่อผู้ใช้งานและรหัสผ่านของ MySQL ที่กำหนดไว้ในขั้นตอนการติดตั้ง Ubuntu ตัวอย่าง root/1234

ปรับแต่ง MySQL ให้ใช้งานกับ Windows Client (VB, VFP) ในวงแลน

  • รันคำสั่งใน PuTTY แก้ไขไฟล์คอนฟิกของ MySQL ดังนี้ sudo nano /etc/mysql/my.cnf
    # bind-address = 127.0.0.1 
    skip-name-resolve 
    
    sudo /etc/init.d/mysql restart
  • วิธีทดสอบแบบง่ายๆ เปิด ODBC Manager ใน Control Panel ของวินโดวส์
    หมายเหตุ กำหนด Privileges ของผู้ใช้งานแบบ Any Host (%) ใน MySQL

ติดตั้ง FTP Server

การติดตั้ง FTP Server ก็เพื่อให้สามารถ Upload/Download ไฟล์สำหรับทำเว็บแอปพลิเคชันต่าง ๆ บนเว็บเซิร์ฟเวอร์ Ubuntu ในตัวอย่างนี้เลือกใช้ Vsftpd เป็น FTP Server
  • รันคำสั่งใน PuTTY ดังนี้ sudo apt-get install vsftpd
  • แก้ไขไฟล์คอนฟิกของ Vsftpd ดังนี้ sudo nano /etc/vsftpd.conf
    anonymous_enable=NO
    local_enable=YES
    write_enable=YES
    local_umask=022
    xferlog_enable=NO
    ascii_upload_enable=YES 
    ascii_download_enable=YES
    
    sudo /etc/init.d/vsftpd restart
  • กำหนดกลุ่ม www-data สำหรับ ftp-client ไว้ที่เว็บโฟลเดอร์หลัก sudo chgrp www-data /var/www/
    sudo chmod 775 /var/www/
  • เพิ่มชื่อของเราในกลุ่ม www-data สำหรับ ftp-client ตัวอย่าง somchai sudo adduser somchai www-data
  • (ทางเลือก) สร้าง user ใหม่ชื่อ webmin สำหรับ ftp-client sudo useradd webmin -g www-data -d /var/www
    sudo passwd webmin
ทดสอบรันโปรแกรม FTP Client เช่น CuteFTP, FileZilla บนวินโดวส์
  • ระบุ Host Address ไปที่ Ubuntu ตัวอย่าง 192.168.1.10
  • กำหนด FTP พอร์ต 21
  • ระบุชื่อล็อกอินที่กำหนดไว้ข้างต้น ตัวอย่าง somchai หรือ webmin
  • กำหนด Remote Folder: /var/www

ติดตั้ง SAMBA

การติดตั้ง Samba ก็เพื่อให้สามารถแชร์ไฟล์ (File Sharing) บน Ubuntu กับเครื่องอื่นในวงแลน
sudo apt-get install samba (ถ้าไม่ได้เลือก Samba ไว้ในตอนติดตั้ง)
ตัวอย่าง แชร์โฟลเดอร์ชื่อ myapp และ temp
  • รันคำสั่งใน PuTTY สร้างโฟล์เดอร์ใน Ubuntu ดังนี้
    sudo mkdir /myshare
    sudo mkdir /myshare/myapp
    sudo mkdir /myshare/temp
    sudo chmod 0777 /myshare/myapp
    sudo chmod 0777 /myshare/temp
    
  • คอนฟิก Samba อนุญาติให้แชร์ไฟล์กับวินโดวส์แบบไม่ต้องมีหน้าจอล็อกอิน sudo nano /etc/samba/smb.conf
    workgroup = MYGROUP
    security = share
    
    [myapp]
    path = /myshare/myapp
    public = yes
    browseable = no
    writeable = yes
    
    [temp]
    path = /myshare/temp
    public = yes
    browseable = yes
    writeable = yes
    
    หมายเหตุ ตั้งชื่อ Workgroup ให้เหมือนกันกับเครื่องอื่น ๆ ในวงแลน
    sudo /etc/init.d/samba restart
ทดสอบไฟล์แชร์บน Ubuntu กับวินโดวส์ ดังนี้
  • เลือกเมนู Start | Run
  • ระบุชื่อเครื่องหรือ IP Address ของ Ubuntu ตัวอย่าง \\192.168.1.10

ติดตั้ง Webmin

การติดตั้ง Webmin ก็เพื่อให้สามารถบริหารจัดการเซิร์ฟเวอร์ Ubuntu ผ่านเว็บบราวเซอร์ เหมาะสำหรับคนไม่ชอบคำสั่งลีนุกซ์เช่นผมนั่นล่ะ
หมายเหตุ ตรวจสอบเวอร์ชันใหม่ที่ http://sourceforge.net/projects/webadmin
  • รันคำสั่งใน PuTTY ติดตั้ง webmin ดังนี้ sudo apt-get install perl libnet-ssleay-perl openssl libauthen-pam-perl libpam-runtime libio-pty-perl libmd5-perl
    wget http://prdownloads.sourceforge.net/webadmin/webmin_1.490_all.deb
    sudo dpkg -i webmin_1.490_all.deb
  • หากคำสั่ง wget ข้างต้นดาวน์โหลดไฟล์ไม่ได้ ก็ให้เพิ่ม webmin ใน Repository (อ่านเพิ่มเติม ที่นี่) nano /etc/apt/sources.list เพิ่มบรรทัดนี้
    deb http://download.webmin.com/download/repository sarge contrib 
    
    ติดตั้ง webmin เวอร์ชันล่าสุดจาก Repository ดังนี้
    cd /root
    wget http://www.webmin.com/jcameron-key.asc
    apt-key add jcameron-key.asc
    apt-get update
    apt-get install webmin
    
  • เปิดเว็บบราวเซอร์ เช่น IE, Firefox, Opera เป็นต้น ระบุ https:// ไปที่ Ubuntu ต่อท้ายด้วย :10000 ตัวอย่าง https://192.168.1.10:10000

    ระบุล็อกอินที่กำหนดไว้ในตอนติดตั้ง Ubuntu ข้างต้น ตัวอย่าง somchai/1234 (หรือ root ก็ได้ตามสะดวก)


ติดตั้ง PostgreSQL (ภาษาไทย)

การติดตั้งระบบฐานข้อมูล PostgreSQL บน Ubuntu จะสร้าง Database Cluster ให้อัตโนมัติด้วย ดังนั้นเพื่อให้ใช้งานภาษาไทยถูกต้อง ให้กำหนด Locale เป็น th_TH ก่อน มิฉะนั้นต้อง initdb -E สร้าง Cluster ใหม่ ซึ่งค่อนข้างยุ่งยากกว่า
  • รันคำสั่งใน PuTTY กำหนดภาษาไทยของ Ubuntu ดังนี้ sudo nano /var/lib/locales/supported.d/local
    en_US.UTF-8 UTF-8
    th_TH.UTF-8 UTF-8
    
    sudo dpkg-reconfigure locales
  • ตรวจสอบว่ามี th_TH.UTF-8 อยู่ใน Locale หรือไม่ locale -a

  • เปลี่ยนดีฟอลต์ Locale เป็นไทย ก่อนติดตั้ง PostgreSQL
  • export LANG=th_TH.UTF-8
  • ตรวจสอบให้แน่ใจว่า ค่าดีฟอลต์ทั้งหมดต้องเป็น th_TH locale

  • รันคำสั่งติดตั้ง PostgreSQL ดังนี้ sudo apt-get install postgresql-8.4
    หมายเหตุ ข้อความในหน้าจอขณะติดตั้งจะเป็นขยะอ่านไม่รู้เรื่อง ก็ให้ตอบ Y อย่างเดียว
  • (ทางเลือก) เปลี่ยนดีฟอลต์ Locale กลับเป็นอังกฤษ export LANG=en_US.UTF-8
  • ตั้งรหัสผ่านของ postgres ตัวอย่าง 1234 sudo -u postgres psql
    postgres=# \password postgres
    postgres=# \q
    
  • ตรวจสอบว่า ภาษาไทยใน PostgreSQL ถูกต้องหรือไม่ ? sudo -u postgres psql
    postgres=# \encoding
    UTF-8
    
    postgres=# show lc_collate ;
    th_TH.UTF-8
    
    postgres=# \q
หมายเหตุ
  • การสร้าง Database ใน PostgreSQL ให้กำหนด Encoding เป็น UTF-8
  • PHP กำหนด META ในส่วนหัวเอกสาร ระบุ Charset=UTF-8
  • การสร้างวินโดวส์แอปพลิเคชันเลือก ODBC ระบุ Driver={PostgreSQL UNICODE};

ติดตั้ง phpPgAdmin


การติดตั้ง phpPgAdmin ก็เพื่อให้สามารถใช้งาน PostgreSQL ผ่านเว็บบราวเซอร์
  • รันคำสั่งใน PuTTY ดังนี้ sudo apt-get install phppgadmin
  • แก้ไขไฟล์คอนฟิกของ Apache ดังนี้ sudo nano /etc/phppgadmin/apache.conf
    <DirectoryMatch /usr/share/phppgadmin/>
    allow from all
    
    sudo /etc/init.d/apache2 reload
  • กำหนดให้ล็อกอินของ postgres เข้าใช้งานได้ sudo nano /usr/share/phppgadmin/conf/config.inc.php
    $conf['extra_login_security'] = false;
    
    sudo /etc/init.d/apache2 reload
  • เปิดเว็บบราวเซอร์ เช่น IE, Firefox, Opera เป็นต้น ระบุ URL ไปที่ Ubuntu ตัวอย่าง http://192.168.1.10/phppgadmin

    ใส่ชื่อผู้ใช้งานและรหัสผ่านของ PostgreSQL ตัวอย่าง postgres/1234


ปรับแต่ง PHP ให้ใช้งานกับ PostgreSQL


การคอนฟิกในส่วนนี้ ก็เพื่อให้สามารถใช้งานคำสั่งของ PHP กับ PostgreSQL เช่น pg_connect(), pg_query() เป็นต้น
  • เปิดเว็บบราวเซอร์ เช่น IE, Firefox, Opera เป็นต้น รันสคริปท์คำสั่ง phpinfo() ที่ได้สร้างไว้แล้วในตอนต้น ตัวอย่าง http://192.168.1.10/phpinfo.php
    ตรวจดูว่ามีส่วน pgsql หรือไม่ ?

  • หากไม่พบส่วนดังกล่าวให้รันคำสั่งใน PuTTY ดังนี้ sudo apt-get install php5-pgsql

ปรับแต่ง PostgreSQL ให้ใช้งานกับ Windows Client (VB, VFP) ในวงแลน

  • รันคำสั่งใน PuTTY แก้ไขไฟล์คอนฟิกของ PostgreSQL ดังนี้ sudo nano /etc/postgresql/8.4/main/postgresql.conf
    listen_addresses = '*'
    password_encryption = on
    
    sudo nano /etc/postgresql/8.4/main/pg_hba.conf
    กำหนดช่วง IP ในวงแลน ตัวอย่าง 192.168.1.0
    host all all 192.168.1.0/24 md5
    host all all 127.0.0.1/32 md5
    
    sudo /etc/init.d/postgresql-8.4 restart
  • วิธีทดสอบแบบง่ายๆ เปิด ODBC Manager ใน Control Panel ของวินโดวส์
    หมายเหตุ สร้างผู้ใช้งาน (Role) ใน PostgreSQL ด้วย phpPgAdmin หรือ pgAdmin กำหนด Can Login ? = Yes

ติดตั้ง SQLite

การติดตั้งระบบฐานข้อมูล SQLite และตัวอย่างการใช้งานต่าง ๆ สามารถอ่านเพิ่มเติมได้ ที่นี่
  • ติดตั้ง SQLite2 Command Line ทำดังนี้
    $ sudo apt-get install sqlite
    $ sqlite
    

  • ติดตั้ง SQLite3 Command Line ทำดังนี้
    $ sudo apt-get install sqlite3
    $ sqlite3
    


ปรับแต่ง PHP ให้ใช้งานกับ SQLite


การคอนฟิกในส่วนนี้ ก็เพื่อให้สามารถใช้งานคำสั่งของ PHP กับ SQLite เช่น sqlite_open(), sqlite_query() เป็นต้น
  • รันคำสั่งใน PuTTY ดังนี้
    $ sudo apt-get install php5-sqlite
    $ sudo /etc/init.d/apache2 restart
    

  • เปิดเว็บบราวเซอร์ เช่น IE, Firefox, Opera เป็นต้น รันสคริปท์คำสั่ง phpinfo() ที่ได้สร้างไว้แล้วในตอนต้น ตัวอย่าง http://192.168.1.10/phpinfo.php
    ตรวจดูว่ามีส่วน SQLite ลักษณะดังรูปหรือไม่ ?

 
Design by GURU