服務流程:linux
dhcp服務器要想提供服務首先要有 一個靜態的ip地址,這樣才能給別的主機分配ip地址
本次實驗是centos7.4安裝shell
~]# yum info dhcp 查看是否有dhcp服務的包 ~]# yum install dhcp 安裝dhcp服務 ~]# rpm -qf dhcp 查看軟件包包括的程序列表 /etc/NetworkManager /etc/NetworkManager/dispatcher.d /etc/NetworkManager/dispatcher.d/12-dhcpd /etc/dhcp/dhcpd.conf 配置文件 /etc/dhcp/dhcpd6.conf /etc/dhcp/scripts /etc/dhcp/scripts/README.scripts /etc/openldap/schema/dhcp.schema /etc/sysconfig/dhcpd /usr/bin/omshell /usr/lib/systemd/system/dhcpd.service 提供dhcp服務 /usr/lib/systemd/system/dhcpd6.service /usr/lib/systemd/system/dhcrelay.service 提供中繼服務,和dhcp不可同時開啓 /usr/sbin/dhcpd 主程序(啓動包括了ipv4和ipv6) /usr/sbin/dhcrelay 中繼程序 /usr/share/doc/dhcp-4.2.5 /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example 配置文件參考的範例文件 /usr/share/doc/dhcp-4.2.5/dhcpd6.conf.example /usr/share/doc/dhcp-4.2.5/ldap /usr/share/doc/dhcp-4.2.5/ldap/README.ldap /usr/share/doc/dhcp-4.2.5/ldap/dhcp.schema /usr/share/doc/dhcp-4.2.5/ldap/dhcpd-conf-to-ldap /usr/share/man/man1/omshell.1.gz /usr/share/man/man5/dhcpd.conf.5.gz /usr/share/man/man5/dhcpd.leases.5.gz /usr/share/man/man8/dhcpd.8.gz /usr/share/man/man8/dhcrelay.8.gz /usr/share/systemtap/tapset/dhcpd.stp /var/lib/dhcpd /var/lib/dhcpd/dhcpd.leases 分配地址的地址數據庫,存放地址分配狀況 /var/lib/dhcpd/dhcpd6.leases
~]# vim /etc/dhcp/dhcpd.conf 編輯配置文件 # DHCP Server Configuration file. # see /usr/share/doc/dhcp*/dhcpd.conf.example 配置文件爲空,須要參考範例文件 # see dhcpd.conf(5) man page ~]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf 複製範例文件,並覆蓋配置文件 ~]# vim /etc/dhcp/dhcpd.conf 編輯配置文件 option domain-name "magedu.com"; 域名後綴,訪問某主機的時候會自動補上這個後綴 option domain-name-servers 114.114.114.114,8.8.8.8; DNS服務器地址 default-lease-time 86400; 默認租期,秒爲單位,能夠設長點。 max-lease-time 864000; 特殊的租期 subnet 192.168.27.0 netmask 255.255.255.0 { ip地址的分配 range 192.168.27.50 192.168.27.200; ip地址分配範圍 option routers 192.168.27.1; 網關地址 } host passacaglia { 綁定固定的ip地址 hardware ethernet 0:0:c0:5d:bd:95; 客戶端mac地址 fixed-address 192.168.27.100; 綁定固定的ip server-name "toccata.fugue.com"; } ~]# systemctl start dhcpd 啓動dhcp服務 ~]# cat /var/lib/dhcpd/dhcpd.leases 開啓另外一臺機器,看是否能夠獲取到地址 lease 192.168.27.129 { 看到獲取到的dhcp分配的地址 starts 4 2018/03/15 09:12:38; ends 5 2018/03/16 09:12:38; cltt 4 2018/03/15 09:12:38; binding state active; next binding state free; rewind binding state free; hardware ethernet 00:0c:29:f2:8d:8f; client-hostname "centos6"; } ~]# cat /var/lib/dhclient/dhclient-eth0.leases 在客戶端查看ip地址獲取狀況 lease { interface "eth0"; fixed-address 192.168.27.129; option subnet-mask 255.255.255.0; option routers 192.168.27.1; option dhcp-lease-time 86400; option dhcp-message-type 5; option domain-name-servers 114.114.114.114,8.8.8.8; dns服務地址 option dhcp-server-identifier 192.168.27.128; dhcp服務器地址 option domain-name "magedu.com"; renew 4 2018/03/15 19:14:26; rebind 5 2018/03/16 06:12:27; expire 5 2018/03/16 09:12:27; }
成功!
讓客戶端從新獲取ip,須要在客戶端作一下操做數據庫
~]# rm -rf /var/lib/dhclient/* 刪除客戶端數據庫下的內容 ~]# killall dhclient 殺掉客戶端運行程序 ~]# dhclient -d 前臺顯示從新獲取ip
這個實驗的成功須要兩步:
1.關掉虛擬機的dhcp服務
2.關掉橋接模式vim