DHCP Server

By Sompan Chsilp
  30 October 2010


DHCP Server ชื่อเต็มคือ Dynamic Host Configuration Protocol Server
เป็นโปรแกรมที่รันที่เครื่องเซิร์ฟเวอร์เพื่อทำหน้าที่แจกไอพีให้แก่เครื่องไคลเอ็นท์ที่ร้องขอ
ที่แจกไปนอกจากจะเป็นหมายเลขไอพีแล้วยังประกอบด้วย ค่า subnetmask หมายเลขไอพีของ gateway และหมายเลขไอพีของ DNS server อีกด้วย
ในวงแลนหนึ่งๆ มักจะมีการติดตั้ง DHCP Server หนึ่งตัวเพื่อรองรับการแจกไอพี ซึ่งจะทำให้การบริหารจัดการหมายเลขไอพีของเครื่องสะดวกยิ่งขึ้น

เครื่องเซิร์ฟเวอร์ที่ติดตั้งด้วย SUTinsServer นี้ ได้ทำการติดตั้งและคอนฟิก DHCP Server ไว้ให้แล้ว เพียงแต่เปิดไฟล์วอลล์และสั่งรันโปรแกรมทุกอย่างก็จะพร้อมใช้
 (Note: This server has been pre configured and ready to use. No installation is needed.)


1. Install DHCP server
If the server has not been install, install it by using command:
sut@main:~$ sudo apt-get install dhcp3-server




2. แก้ไขคอนฟิกไฟล์(Edit configure file) /etc/dhcp3/dhcpd.conf
สำหรับเซิร์ฟเวอร์เครื่องนี้ได้ทำการคอนฟิกไว้ให้แล้ว ดังนั้นไฟล์คอนฟิกจึงมีข้อมูลดังนี้
(On this server it has already been configured and contains:)



ddns-update-style none;
authoritative;
log-facility local7;

## by sompan chansilp
## using netmask 255.255.252.0 will allow to have client on the same LAN
## from address 172.16.0.0 to 172.16.3.255
## address 172.16.0.0 and 172.16.3.255 will be used for broadcast
## address 172.16.3.254 will be chosen to be a gateway address
## so clients on this LAN should have address start from 172.16.1.1 to 172.16.3.253
subnet 172.16.0.0 netmask 255.255.252.0 {
  range 172.16.1.1 172.16.3.253;
  option domain-name-servers 172.16.0.1;
  option domain-name "myschool.net";
  option routers 172.16.3.254;
  option broadcast-address 172.16.0.0;
  # set this to adjust time for Thailand
  option time-offset     07000;
  default-lease-time 14400;
  # 14400 sec = 4 hrs. and 21600 sec = 6 hrs.
  max-lease-time 21600;
  host ns {
        next-server myschool.net;
        #hardware ethernet 12:34:56:78:AB:CD;
        fixed-address 172.16.0.1;
  }
}

### This setting will assign fix IP 172.16.3.3 to client having NIC address 00:24:81:46:2d:07;
### This computer might be a server.
#host server2.myschool.net {
#  hardware ethernet 00:24:81:46:2d:07;
#  fixed-address 172.16.3.3;
#}


คำอธิบาย(Explanation)

ด้วยการคอนฟิกตามข้อมูลในไฟล์คอนฟิกดังแสดง จะเป็นผลให้ DHCP server แจกไอพีให้เครื่องไคลเอ็นท์ที่ร้องขอจากหมายเลข 172.16.0.50 to 172.16.3.253 แจก subnetmask 255.255.252.0 แจก DNS เป็น 172.16.0.1 แจก gateway เป็น 172.16.3.254

3. Disable firewall temporary
Use command:
sut@main:~$ sudo ufw disable
Firewall stopped and disabled on system startup
sut@main:~$ sudo ufw status
Status: inactive


4. Start DHCP server
Use command:
sut@main:~$ sudo service dhcp3-server start
 * Starting DHCP server dhcpd3                                           [ OK ]

To see status:
sut@main:~$ sudo service dhcp3-server status
Status of DHCP server: dhcpd3 is running.

After you change any configure, restart it with command:

sut@main:~$ sudo service dhcp3-server restart
 * Stopping DHCP server dhcpd3                                           [ OK ]
 * Starting DHCP server dhcpd3                                           [ OK ]
 


5. Setup client computer to obtain automatic IP
To testing, make sure that no DHCP server on the same LAN. If you not sure, just connect a computer to the server directly. Old computer may need a cross cable. When you set network on a client to obtain IP automatically, the client will get IP in the range  172.16.0.50 to 172.16.3.253.  Check also for DNS server IP, netmask and gateway address that the client has obtained.


On the server side.

After the client got the IP, you can see data in
file /var/lib/dhcp3/dhcpd.leases
that contains data as:

lease 172.16.0.50 {
  starts 6 2009/10/17 00:05:00;
  ends 1 2009/10/19 12:05:00;
  cltt 6 2009/10/17 00:05:00;
  binding state active;
  next binding state free;
  hardware ethernet 00:24:81:46:2d:48;
  client-hostname "server.onlinetest.net";
}

It means that IP 172.16.0.50 has been assigned to a client computer whose NIC having MAC address 00:24:81:46:2d:48

If the server have trouble assigning IP to the client, just clear data in
file /var/lib/dhcp3/dhcpd.leases



6. More advanced configure style
Read on the terminal:

man dhcpd
man dhcpd.conf


7. ถ้าต้องการรัน DHCP Server ทุกครั้งพร้อมการบูตคอมพิวเตอร์ให้ใช้คำสั่ง
    To enable DHCP server with computer boot use command
sut@server:~$ sudo update-rc.d  dhcp3-server defaults
แล้วที่จอจะแสดง
update-rc.d: warning: dhcp3-server stop runlevel arguments (0 1 6) do not match LSB Default-Stop values (1)
 Adding system startup for /etc/init.d/dhcp3-server ...
   /etc/rc0.d/K20dhcp3-server -> ../init.d/dhcp3-server
   /etc/rc1.d/K20dhcp3-server -> ../init.d/dhcp3-server
   /etc/rc6.d/K20dhcp3-server -> ../init.d/dhcp3-server
   /etc/rc2.d/S20dhcp3-server -> ../init.d/dhcp3-server
   /etc/rc3.d/S20dhcp3-server -> ../init.d/dhcp3-server
   /etc/rc4.d/S20dhcp3-server -> ../init.d/dhcp3-server
   /etc/rc5.d/S20dhcp3-server -> ../init.d/dhcp3-server

ถ้าต้องการหยุดการรันทุกครั้งพร้อมการบูตคอมพิวเตอร์ให้ใช้คำสั่ง
sut@server:~$ sudo update-rc.d  -f dhcp3-server remove
แล้วที่จอจะแสดง

 Removing any system startup links for /etc/init.d/dhcp3-server ...
   /etc/rc0.d/K20dhcp3-server
   /etc/rc1.d/K20dhcp3-server
   /etc/rc2.d/S20dhcp3-server
   /etc/rc3.d/S20dhcp3-server
   /etc/rc4.d/S20dhcp3-server
   /etc/rc5.d/S20dhcp3-server
   /etc/rc6.d/K20dhcp3-server




8. Fine tune firewall setting for DHCP server

To allow firewall to be active now and enabled on system boot, use command:
sut@main:~$ sudo ufw enable
Firewall is active and enabled on system startup
sut@main:~$

To see rules:
sut@main:~$ sudo ufw status
Status: active

To                         Action  From
--                         ------  ----
80/tcp                     ALLOW   Anywhere
443                        ALLOW   Anywhere

Display known list of application:
sut@main:~$ sudo ufw app list
Available applications:
  Apache
  Apache Full
  Apache Secure
  Bind9
  Dovecot IMAP
  Dovecot POP3
  Dovecot Secure IMAP
  Dovecot Secure POP3
  Lighttpd Full
  Lighttpd HTTP
  Lighttpd HTTPS
  OpenSSH
  Postfix
  Postfix Submission

Unfortunately, no DHCP server.
We have to add rules by specify ports

sut@main:~$ sudo ufw allow 67
Rule added
sut@main:~$ sudo ufw allow 68
Rule added

See rules again:
sut@main:~$ sudo ufw status
Status: active

To                         Action  From
--                         ------  ----
80/tcp                     ALLOW   Anywhere
443                        ALLOW   Anywhere
67                         ALLOW   Anywhere
68                         ALLOW   Anywhere

Note:
67 for Bootps. It is the server port.
68 for Bootpc. It is the client port.

More advanced firewall setup, read firewall manual.