DHCP
解决局域网中地址分配的问题,只用于局域网
把一个主机接入TCP/IP网络,要为一台主机配置哪些参数:
IP/mask
Gateway
DNS Server
Wins Server,NTP Server
参数配置方式:
静态指定
动态分配
bootp protocol
dhcp:引入了“租约”的bootp;也可以实现为特定主机保留某固定地址;
DHCP:动态主机配置协议
arp:address resolving protocol
IP-->MAC
rarp:reverse arp
MAC-->IP
监听的端口:
Server :67/UDP
Client:68/UDP # 使用dhclient -d命令,客户端即监听在68端口
工作流程:发送四次报文
(1)Client:dhcp discover
(2)Server:dhcp offer(IP/mask,gw,...)
lease time:租约期限
(3)Client:dhcp request
(4)Server:dhcp ack
续租:
50%,75%,87.5%
单播给服务:
dhcp request
dhcp ack
dhcp request
dhcp nck
dhcp discover
CentOS:
dhcp(ISC,named):
dnsmasq:dhcp & dns
dhcp:
dhcpd:dhcp服务
dhcrelay:中继服务
yum install -y dhcp
rpm -ql dhcp
/etc/dhcp/dhcpd.conf
/etc/dhcp/dhcpd6.conf # ipv6
/usr/lib/systemd/system/dhcpd.service
/usr/lib/systemd/system/dhcpd6.service # ipv6
/usr/lib/systemd/system/dhcrelay.service
:.,$s/^[^#]/#/g
配置一个DHCP服务,让其它主机使用此服务动态获取IP
准备两台主机:其中一台,修改VMware的“虚拟网络编辑器”,使用vmnet1(专用虚拟通道),并将使用本地DHCP服务获取IP取消;两台主机的网络模式均设置为“仅主机”模式。 # 如图1
1. 192.168.0.11主机:
(1)安装dhcp:
yum install -y dhcp
(2)修改配置文件:
vim /etc/dhcp/dhcpd.conf
option domain-name "zhang.redhatvm.com";
option domain-name-servers 192.168.0.1; # 全局domain-name,在另外一台主机上查看/etc/resolv.conf
option routers 192.168.0.100; # 全局routers,给使用dhcp服务的那台主机配置网关,可在那台主机查看路由表信息,systemctl restart network && route -n 如图2
default-lease-time 43200; # 默认租约时间
max-lease-time 86400; # 最大续租时间
subnet 192.168.0.0 netmask 255.255.255.0 {
range 192.168.0.180 192.168.0.200;
# option routers 192.168.0.253; # 局部,
# option domain-name-servers 1.1.1.1; # 局部 domain-name
}
systemctl start dhcpd # 监听在67端口
(3)在另外一台主机上:
dhclient -d # 如图3 客户端,监听在68端口
当重新修改dhcpd.conf配置文件 range的范围时,重启。在另一台主机重新 dhclient -d 可以发现discover-->bound 的IP是在我们修改的range范围内。不修改range,使用dhclient -d 命令,可以看到信息显示的是:
DHCPREQUEST on ens33 to 255.255.255.255 port 67 (xid=0x59864e54)
DHCPACK from 192.168.0.11 (xid=0x6ea3200)
bound to 192.168.0.200 -- renewal in 17158 seconds.
(4) 在另外一台主机上:
systemctl restart network
route -n
局部配置有 routers,优先使用局部的;局部没有找全局的;
# 给某台主机配置静态IP和单独的网关
vim /etc/dhcp/dhcpd.conf # 如图4
host passacaglia {
hardware ethernet 00:0c:29:de:f8:f6;# 需要被分配静态IP那台主机的MAC地址
fixed-address 192.168.0.88;
# option routers 192.168.0.166;
}
systemctl restart dhcpd
另一台主机:
systemctl restart network
vim /etc/sysconfig/network-scripts/ifcfg-ens33 # 需要获取DHCP服务分配IP的主机
PEERDNS=no # 不让DHCP服务修改该主机的域名解析服务的IP,即是/etc/resolv.conf中的nameserver,可以手动配置。
less /var/lib/dhcpd/dhcpd.leases # 查看dhcp服务将IP租约情况
图1:
图2:
图3:
图4:
nmtui 网络配置命令
具体详解参考此链接:https://www.cnblogs.com/pipci/p/12571469.html
DHCP应用配置总结
dhcpd服务:
配置文件:/etc/dhcp/dhcpd.conf # 主要用于配置对于IPV4网络IP分配机制
# 全局
default-lease-time;
max-lease-time;
option routers GW;
option domain-name-servers IP,IP,IP;
subnet NET netmask MASK {
range
...
}
host HOST_ID{
hardware ethernet MAC;
fixed-address;
}
# 分配信息库:
/var/lib/dhcp/dhcpd.leases~
客户端命令:dhclient
-d:运行在前台;
其它配置选项:
filename:指明引导文件名称;
next-server:指明引导所在的服务主机的IP地址;
filename "pxelinux.0";
next-server 192.168.0.20;
tftp:trivial ftp,udp
dhcrelay服务:
pxe
PXE:preboot excution environment,Intel
CentOS:
dhcp(ip/netmask,gw,dns;filename,next-server;)
tftp server(bootloader,kernel,initrd)
yum repository(ftp,http,nfs)
kickstart
yum install -y tftp tftp-server
tftp server:
69/udp # 监听端口
CentOS 6:
chkconfig tftp on
service xinetd restart
CentOS 7:
systemctl start tftp.socket
tftp 默认的文件根目录:/var/lib/tftpboot
cp /etc/inittab /var/lib/tftpboot
cd /tmp
tftp 192.168.0.108
get inittab
quit
ls
centos7构建pxe环境
yum -y install syslinux
cp /usr/share/syslinux/pexlinux.0 /var/lib/tftpboot/
cp /media/cdrom/images/pxeboot/{vmlinuz,initrd.img} /var/lib/tftpboot
cp /usr/share/syselinux/{chain.c32,mboot.c32,menu.c32,memdisk} /bar/lib/tftpboot
mkdir /var/lib/tftpboot/pxelinux.cfg/
vim /var/lib/tftpboot/pxelinux.cfg/default
default menu.c32
prompt 5
timeout 30
MENU TITLE CentOS PXE Menu
LABEL Install CentOS 7 x86_64
KERNEL vmlinuz
APPEND initrd=initrd.img inst.repo=http://192.168.10.9/centos/7/x86_64
LABEL linux_autoinst
MENU LABEL Install CentOS 7 x86_64 auto
KERNEL vmlinuz
APPEND initrd=initrd.img inst.repo=http://192.168.10.9/centos/7/x86_64 ks=http://192.168.10.9/kickstarts/centos7.cfg
centos6构建pxe环境
yum -y install syslinux
cp /usr/share/syslinux/pexlinux.0 /var/lib/tftpboot/
cp /media/cdrom/images/pxeboot/{vmlinuz,initrd.img} /var/lib/tftpboot
cp /usr/share/isolinux/{boot.msg,vesamenu.c32,splash.png} /var/lib/tftpboot
mkdir /var/lib/tftpboot/pxelinux.cfg
cp /media/cdrom/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
cat /var/lib/tftpboot/pxelinux.cfg/default
display bootmsg
menu background splash.jpg
menu title Welcome to CentOS 6.7 !
menu color border 0 #ffffffff #00000000
menu color sel 7 #ffffffff #ff000000
menu color title 0 #ffffffff #00000000
menu color tabmsg 0 #ffffffff #00000000
menu color unsel 0 #ffffffff #00000000
menu color hotsel 0 #ff000000 #ffffffff
menu color hotkey 7 #ffffffff #ff000000
menu color scrollbar 0 #ffffffff #00000000
label autoinst
menu label ^Auto Install CentOS
menu default
kernel vmlinuz
append initrd=initrd.img ks=ftp://192.168.10.16/pub/centos6.cfg
label linux
menu label ^Install or upgrade an existing system
kernel vmlinuz
append initrd=initrd.img
label vesa
menu label Install system with ^basic video driver
kernel vmlinuz
append initrd=initrd.img nomodeset
label rescue
menu label ^Rescue installed system
kernel vmlinuz
append initrd=initrd.img rescue
label local
menu label Boot from ^local drive
localboot 0xffff
label memtest86
menu label ^Memory test
kernel memtest
append -
default menu.c32
prompt 5
timeout 30
MENU TITLE CentOS 7 PXE Menu
LABEL linux
MENU LABEL Install CentOS 7 x86_64
KERNEL vmlinuz
APPEND initrd=initrd.img inst.repo=http://172.16.100.67/centos7 ks=http://172.16.100.67/centos7.cfg
运维工具
BootStraping:OS installation,pxe,cobbler
Configuration:ansible,puppet,saltstack,chef,cfengine
Command&Control:ansible,fabric,func