Wednesday, June 1, 2011

Cấu hình một Linux DHCP server đơn giản

1.Mô tả về file cấu hình của dhcpd


Sau khi cài đặt dhcpd thì file cấu hình cho daemon nằm tại /etc/dhcpd.conf. File lưu trữ các ip được leased nằm tại /var/lib/dhcpd/dhcpd.leases

FIle cấu hình chứa các parameters và các sections.

  • Các sections là nơi để khai báo thông tin về mạng, các địa chỉ đc gán cho clients, các đặc tính của clients. Section thường đc đặt trong cặp {}
  • Parameters xác định các tính chất đc gửi đến clients. Thường thì các parameters đc bắt đầu bằng từ khóa "option". Các parameters đc khai báo trước một section đc gọi là global parameters, và các parameters này sẽ đc áp dụng cho toàn bộ các sections sau section đó. 

Sau khi cài đặt dhcp, file cấu hình /etc/dhcpd.conf sẽ không có gì. THông tin cấu hình mẫu dùng để tham khảo đc lưu trữ trong đuờng dẫn /usr/share/doc/dhcp*/dhcpd.conf.sample. Ta có thể chép file mẫu này vào /etc/ và đổi tên thành dhcpd.conf, hoặc tự tạo một file text mới để ghi các thông tin cấu hình lên file này.

Ví dụ có một file cấu hình mẫu như sau:



ddns-update-style interim;
subnet 192.168.1.0 netmask 255.255.255.0 {
option subnet-mask 255.255.255.0;
option routers 192.168.1.1;
default-lease-time 600;
max-lease-time 604800;
range 192.168.1.100 192.168.1.200;
}


  • -Dòng ddns... cho biết kiểu của Dynamic DNS. Mặc định là interim.
  • -dòng subnet... bắt đầu khai báo một section. Ở đây khai báo các thông số cho subnet 192.168.1.0/24. Bên trong là các parameters dành cho subnet này.
  • -dòng option subnet-mask: subnet mask dành cho dhcp client.
  • -dòng option routers: cung cấp default gateway cho clients.
  • -dòng default-lease-time: thời gian tối đa để lease một IP nếu client ko xác định thời gian, tính theo second
  • -dòng max-lease-time: thời gian tối đa để lease một IP
  • -dòng range...: dải IP sẽ đc cấp bởi dhcp server, xác định 2 giá trị ip đầu và ip cuối.


Như vậy, với khai báo như trên, ta có thể hiểu đơn giản rằng: DHCP server cung cấp dải ip từ 192.168.1.100 đến 192.168.1.200 với subnet mask là 255.255.255.0 cho subnet 192.168.1.0/24 có địa chỉ của gateway là 192.168.1.1

2. Cấu hình một DHCP server

Một mô hình đơn giản như sau:
-Setup một linux server có địa chỉ 10.0.0.2/24 để cài đặt dhcpd.
-Một client chạy windows xp dùng để test chức năng lease ip.
Yêu cầu:
-Xây dựng một DHCP server cung cấp ip cho subnet 10.0.0.0/24, địa chỉ gateway là 10.0.0.1/24, domain là mydomain.local. Domain nameserver có ip là 10.0.0.2/24

Tiến hành thực hiện:
-Cài đặt dhcpd từ đĩa đc mount

[root@dns CentOS]# rpm -ivh dhcp-3.0.5-23.el5.i386.rpm 
warning: dhcp-3.0.5-23.el5.i386.rpm: Header V3 DSA signature: NOKEY, key ID e8562897
Preparing...                ########################################### [100%]
package dhcp-3.0.5-23.el5.i386 is already installed
[root@dns CentOS]# 


-CHép file cấu hình mẫu sang /etc/ đổi tên thành dhcpd.conf hoặc tạo một file dhcpd.conf trong /etc/
[root@dns CentOS]# cp /usr/share/doc/dhcp-3.0.5/dhcpd.conf.sample /etc/dhcpd.conf


-Nội dung của file dhcpd.conf như sau:



ddns-update-style interim;
ignore client-updates;


subnet 10.0.0.0 netmask 255.255.255.0 {


# --- default gateway
        option routers                  10.0.0.1;
        option subnet-mask              255.255.255.0;


#       option nis-domain               "domain.org";
        option domain-name              "mydomain.local";
        option domain-name-servers      10.0.0.2;


        option time-offset              -18000; # Eastern Standard Time
#       option ntp-servers              192.168.1.1;
#       option netbios-name-servers     192.168.1.1;
# --- Selects point-to-point node (default is hybrid). Don't change this unless
# -- you understand Netbios very well
#       option netbios-node-type 2;


        range dynamic-bootp 10.0.0.100 10.0.0.200;
        default-lease-time 21600;
        max-lease-time 43200;


        # we want the nameserver to appear at a fixed address
#       host ns {
#               next-server marvin.redhat.com;
#               hardware ethernet 12:34:56:78:AB:CD;
#               fixed-address 207.175.42.254;
#       }
}


-Lưu ý là dấu # là dấu comment.
-Sử dụng client xp để kiểm tra việc lease ip:

-Kiểm tra file lưu trữ ip đc leased:

[root@dns CentOS]# more /var/lib/dhcpd/dhcpd.leases
# All times in this file are in UTC (GMT), not your local timezone.   This is
# not a bug, so please don't ask about it.   There is no portable way to
# store leases in the local timezone, so please don't request this as a
# feature.   If this is inconvenient or confusing to you, we sincerely
# apologize.   Seriously, though - don't ask.
# The format of this file is documented in the dhcpd.leases(5) manual page.
# This lease file was written by isc-dhcp-V3.0.5-RedHat

lease 10.0.0.200 {
  starts 4 2011/05/26 03:02:24;
  ends 4 2011/05/26 09:02:24;
  binding state active;
  next binding state free;
  hardware ethernet 00:0c:29:9b:96:cd;
  uid "\001\000\014)\233\226\315";
  client-hostname "PC2";
}
[root@dns CentOS]# 


=====Hết====

No comments:

Post a Comment