Dịch Vụ DHCP

DỊCH VỤ DHCP TRONG LINUX
[Môi trường làm việc: Server: Red Hat Linux AS 4 – Client: XP sp2]

Mục lục:
1. Giới thiệu:
2. Cài đặt và cấu hình.
3. Theo dõi và sử dụng DHCP.
4. Tài liệu tham khảo.

#####################
#####################

1. Giới thiệu

Dịch vụ cung cấp địa chỉ IP động DHCP (Dynamic Host Configuration Protocol ).
Không những cung cấp được IP mà dịch vụ trên còn đưa ra cho chúng ta nhiều tính năng để cung cấp những yếu tố khác cho các máy client, ví dụ như cung cấp địa chỉ của máy tính dùng để phân giải tên miền DNS, địa chỉ của một Gateway router, địa chỉ máy WINS…

Một DHCP server bao gồm bốn mục chính sau:
  • Options: Dùng để cung cấp các yếu tố cho phía client như địa chỉ IP, địa chỉ subnet mask, địa chỉ Gateway, địa chỉ DNS .v.v…
  • Scope: Một đoạn địa chỉ được quy định trước trên DHCP server mà chúng ta sẽ dùng để gán cho các máy client.
  • Reservation: Là những đoạn địa chỉ dùng để “để dành” trong một scope mà chúng ta đã quy định ở trên.
  • Lease: Thời gian “cho thuê” địa chỉ IP đối với mỗi client.

2. Cài đặt và cấu hình DHCP server

Thường thì dịch vụ này đã được setup khi ta cài đặt OS linux theo chuẩn server.
Nếu máy tính của bạn chưa cài đặt dịch vụ DHCP thì bạn download gói cài đặt rpm về sử dụng. Thường ta sử dụng gói có version dhcp-3.0.1rc14-1.i386.rpm.
  • Ở dạng đuôi mở rộng là .rpm, ta chạy lệnh: # rpm –ivh dhcp-3.0.1rc14-1.i386.rpm
  • Ở dạng source code, ta biên dịch như sau: # tar –xzvf dhcp-3.0.1rc14-1.i386.tar.gz


Sau khi hoàn tất xong quá trình cài đặt, bật dịch vụ DHCP lên, nó tự động đọc tập tin /etc/dhcpd.conf, nhưng theo mặc định gói cài đặt này không tự tạo ra file /etc/dhcpd.conf. Mà chúng ta sẽ tạo và cấu hình để dịch vụ này có thể hoạt động theo ý muốn của chúng ta.
Thường thì tập tin này ta copy từ tập tin /usr/share/doc/dhcp-x.x.x/dhcpd.conf.sample. sử dụng lệnh sau:
[root@star root]# cp /usr/share/doc/dhclient-3.0.1/dhclient.conf.sample /etc/dhcpd.conf

Soạn file cấu hình /etc/dhcpd.conf cấu hình cho riêng mình.


#Không cho phép DHCP Server cập nhật động DNS.
ddns-update-style interim;
ignore client-updates;

subnet 192.168.1.0 netmask 255.255.255.0 {

# The range of IP addresses the server will issue to DHCP enabled PC clients booting up on the network

range 192.168.1.220 192.168.1.240;

# Set the amount of time in seconds that a client may keep the IP address

default-lease-time 86400;
max-lease-time 86400;

# Set the default gateway to be used by the PC clients
option routers 192.168.1.1;

# Don't forward DHCP requests from this NIC interface to any other NIC interfaces
option ip-forwarding off;

# Set the broadcast address and subnet mask to be used by the DHCP clients
option broadcast-address 192.168.1.255;
option subnet-mask 255.255.255.0;

# Set the DNS server to be used by the DHCP clients
option domain-name-servers 192.168.1.77, 210.245.31.100;

# Set the NTP server to be used by the DHCP clients
# option nntp-server 192.168.1.100;

# If you specify a WINS server for your Windows clients, you need to include the following option in the dhcpd.conf file:
# option netbios-name-servers 192.168.1.100;

# những máy luôn nhận IP cố định
host ketoan {
hardware ethernet 08:00:20:03:93:02;
fixed-address 172.16.1.120;
}

  • Sau khi chỉnh sữa trong file cấu hình ta phải khởi động lại dịch vụ:
[root@star root]# /etc/rc.d/init.d/dhcpd restart
Hay: [root@star root]# service dhcpd restart
  • Cho phép DHCP tự động khởi động khi boot :
[root@star root]# chkconfig dhcpd on

3. Theo dõi và sử dụng DHCP.

Xem những địa chỉ đã được cấp bởi DHCP ta xem trong files:
[root@star root]#vi /var/lib/dhcp/dhcpd.leases
DHCP server không thể làm làm việc nếu thiếu file dhcpd.leases. vì thế nếu chưa có file này, chúng ta tạo bằng lệnh:
# touch /var/lib/dhcp/dhcpd.leases

Sử dụng lệnh sau để xem log của DHCP
[root@star root]# tail -f /var/log/messages

Lúc cấu hình chưa thành công
Dec 19 22:53:08 localhost dhcpd: If you intend to request help from the dhcp-server@isc.org
Dec 19 22:53:08 localhost dhcpd: mailing list, please read the section on the README about
Dec 19 22:53:08 localhost dhcpd: submitting bug reports and requests for help.
Dec 19 22:53:08 localhost dhcpd:
Dec 19 22:53:08 localhost dhcpd: Please do not under any circumstances send requests for
Dec 19 22:53:08 localhost dhcpd: help directly to the authors of this software - please
Dec 19 22:53:08 localhost dhcpd: send them to the appropriate mailing list as described in
Dec 19 22:53:08 localhost dhcpd: the README file.
Dec 19 22:53:08 localhost dhcpd:
Dec 19 22:53:08 localhost dhcpd: exiting.

[4]+ Stopped tail -f /var/log/messages

Lúc cấu hình thành công
Dec 19 23:05:45 localhost dhcpd: Wrote 0 leases to leases file.
Dec 19 23:05:45 localhost dhcpd: Listening on LPF/eth0/00:0c:29:87:2f:d1/192.168.1.0/24
Dec 19 23:05:45 localhost dhcpd: Listening on LPF/eth0/00:0c:29:87:2f:d1/192.168.1.0/24
Dec 19 23:05:45 localhost dhcpd: Sending on LPF/eth0/00:0c:29:87:2f:d1/192.168.1.0/24
Dec 19 23:05:45 localhost dhcpd:
Dec 19 23:05:45 localhost dhcpd: Sending on LPF/eth0/00:0c:29:87:2f:d1/192.168.1.0/24
Dec 19 23:05:45 localhost dhcpd: Sending on Socket/fallback/fallback-net
Dec 19 23:05:45 localhost dhcpd: Sending on Socket/fallback/fallback-net
Dec 19 23:05:45 localhost dhcpd:
Dec 19 23:05:45 localhost dhcpd: dhcpd startup succeeded

[6]+ Stopped tail -f /var/log/messages

Lúc DHCP server linux tranh chấp cấp ip với DHCP server của window

Dec 19 23:27:33 localhost dhcpd: DHCPOFFER on 192.168.1.240 to 00:0b:db:08:4a:3b (NewName) via eth0
Dec 19 23:31:09 localhost dhcpd: DHCPDISCOVER from 00:1a:4d:6c:d1:3e via eth0
Dec 19 23:31:09 localhost dhcpd: DHCPREQUEST for 192.168.1.147 (192.168.1.77) from 00:1a:4d:6c:d1:3e via eth0: unknown lease 192.168.1.147.
Dec 19 23:31:09 localhost dhcpd: DHCPDECLINE of 192.168.1.147 from 00:1a:4d:6c:d1:3e via eth0: not found
Dec 19 23:31:10 localhost dhcpd: DHCPOFFER on 192.168.1.239 to 00:1a:4d:6c:d1:3e (XNK03) via eth0Dec 19 23:31:16 localhost dhcpd: DHCPDISCOVER from 00:1a:4d:6c:d1:3e (XNK03) via eth0
Dec 19 23:31:16 localhost dhcpd: DHCPOFFER on 192.168.1.239 to 00:1a:4d:6c:d1:3e (XNK03) via eth0Dec 19 23:31:16 localhost dhcpd: DHCPREQUEST for 192.168.1.148 (192.168.1.77) from 00:1a:4d:6c:d1:3e via eth0: unknown lease 192.168.1.148.
Dec 19 23:33:45 localhost smbd[3210]: [2007/12/19 23:33:45, 0] lib/util_sock.c:read_socket_data(384)
Dec 19 23:33:45 localhost smbd[3210]: read_socket_data: recv failure for 4. Error = Connection reset by peer

[7]+ Stopped tail -f /var/log/messages
Log file ghi nhận DHCP server linux cấp thành công ip cho client
Dec 19 23:47:47 localhost dhcpd: For info, please visit http://www.isc.org/sw/dhcp/
Dec 19 23:47:47 localhost dhcpd: Wrote 2 leases to leases file.
Dec 19 23:47:47 localhost dhcpd: Listening on LPF/eth0/00:0c:29:87:2f:d1/192.168.1.0/24
Dec 19 23:47:47 localhost dhcpd: Listening on LPF/eth0/00:0c:29:87:2f:d1/192.168.1.0/24
Dec 19 23:47:47 localhost dhcpd: Sending on LPF/eth0/00:0c:29:87:2f:d1/192.168.1.0/24
Dec 19 23:47:47 localhost dhcpd:
Dec 19 23:47:47 localhost dhcpd: Sending on LPF/eth0/00:0c:29:87:2f:d1/192.168.1.0/24
Dec 19 23:47:47 localhost dhcpd: Sending on Socket/fallback/fallback-net
Dec 19 23:47:47 localhost dhcpd: Sending on Socket/fallback/fallback-net
Dec 19 23:47:47 localhost dhcpd: dhcpd startup succeeded

[9]+ Stopped tail -f /var/log/messages
Kiểm tra hoạt động của DHCP.
  • Trong window, để client nhận ip động thì chúng ta đã biết cấu hình như thế nào rồi. Dùng lệnh ipconfig /all để xem tất cả các ip trong window
  • Trong linux để client nhận ip động thì ta sữa file cấu hình card enternet 1 tí. Dùng lệnh ifconfig –a để xem tất cả các ip trong linux
Nội dung file dhcpd.leases sau khi cấp ip thành công cho 2 máy.

# 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.1rc14

lease 192.168.1.240 {
starts 3 2007/12/19 16:42:10;
ends 4 2007/12/20 16:42:10;
tstp 4 2007/12/20 16:42:10;
binding state active;
next binding state free;
hardware ethernet 00:0b:db:08:4a:3b;
uid "\001\000\013\333\010J;";
client-hostname "NewName";
}
lease 192.168.1.239 {
starts 3 2007/12/19 16:45:45;
ends 4 2007/12/20 16:45:45;
tstp 4 2007/12/20 16:45:45;
binding state active;
next binding state free;
hardware ethernet 00:1a:4d:6c:d1:3e;
uid "\001\000\032Ml\321>";
client-hostname "XNK03";
}

4. Tài liệu tham khảo

  • /usr/share/doc/dhcp-x.x.x/dhcpd.conf.sample
  • man dhcp-options
  • Nguồn: http://www.linuxhomenetworking.com

Share this

Related Posts

Previous
Next Post »