dnsmasq中文参考资料(必看):
目标:dnsmasq部署pxe服务器脚本网络环境:kvm+vsftpd服务器:192.168.10.7dnsmasq简介
提供 DNS 缓存和 DHCP 服务功能。作为域名解析服务器(DNS),dnsmasq可以通过缓存 DNS 请求来提高对访问过的网址的连接速度。作为DHCP 服务器, 可以用于为局域网电脑分配内网ip地址和提供路由。
DNS和DHCP两个功能可以同时或分别单独实现。dnsmasq轻量且易配置,适用于个人用户或少于50台主机的网络。由于dnsmasq可以管理tftp-server服务。因此可以用来搭建PXE服务器。 #临时测试:systemctl stop firewalldsystemctl stop libvirtdsetenforce 0#准备本地yum源mkdir -pv /dvdmount /dev/cdrom /dvdchmod +x /etc/rc.d/rc.localgrep dvd /etc/rc.d/rc.local || echo 'mount /dev/cdrom /dvd' > /etc/rc.d/rc.localsource /etc/rc.d/rc.localcd /etc/yum.repos.dmkdir -pv bak/mv -v *.repo bak/cat > dvd.repo <<EOF[centos73]name=centos linux 7.3 localbaseurl=file:///dvdenabled=1gpgcheck=0EOFyum clean allyum repolistyum install -y vsftpdmkdir -pv /var/ftp/ks/yum install -y dnsmasq curl tftp-server xinetd syslinux mlocatemkdir -v /var/lib/tftpboot/pxelinux.cfg /var/lib/tftpboot/centos7/ /var/ftp/dvd/systemctl restart vsftpdmount /dev/cdrom /var/ftp/dvdupdatedblocate pxelinux.0cp -v /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/cp -v /usr/share/syslinux/menu.c32 /var/lib/tftpboot/cp -v /dvd/isolinux/* /var/lib/tftpboot/centos7/touch /var/lib/tftpboot/pxelinux.cfg/default #创建pxe启动菜单default文件cat > /var/lib/tftpboot/pxelinux.cfg/default <<EOFdefault menu.c32prompt 0timeout 300ONTIMEOUT localmenu title ########## PXE Boot Menu ##########label 1menu label ^1)InstallCentOS7 x64 withLocalRepokernel centos7/vmlinuzappend initrd=centos7/initrd.img method= devfs=nomount#ks=ftp://192.168.10.7/ks/7text.kslabel 2menu label ^2)InstallCentOS7 x64 with Repokernel centos7/vmlinuzappend initrd=centos7/initrd.img method= devfs=nomount ip=dhcplabel 3menu label ^3)InstallCentOS7 x64 withLocalRepousing VNCkernel centos7/vmlinuzappend initrd=centos7/initrd.img method= devfs=nomount inst.vnc inst.vncpassword=passwordlabel 4menu label ^4)Bootfromlocal driveEOFcat /var/lib/tftpboot/pxelinux.cfg/default 测试dnsmasq手动启动(已测OK):netstat -atunlp|grep dnsmasqsystemctl stop libvirtdsystemctl stop dnsmasqpkill -9 dnsmasqdnsmasq --interface=ens33 --dhcp-range=192.168.10.100,192.168.10.150 --dhcp-boot=pxelinux.0 --enable-tftp --tftp-root=/var/lib/tftpboot/dnsmasq -i ens33 -F 192.168.10.100,192.168.10.150 -M pxelinux.0 --enable-tftp --tftp-root=/var/lib/tftpboot/#以上的临时测试完毕(已测OK)-----------------------------------------------------------------------#正式编写dnsmasq自动部署pxe服务器的脚本vi dnsmasq.sh 脚本代码如下#!/bin/bashsetenforce 0getenforcesed -i '/^SELINUX=/c\SELINUX=disable' /etc/selinux/configgrep ^SELINUX /etc/selinux/configsystemctl stop firewalldsystemctl disable firewalldsystemctl stop libvirtdsystemctl disable libvirtdread -p 'selinux和firewalld已关闭,按回车键继续配置本地yum源...'#准备本地yum源mkdir -pv /dvdmount /dev/cdrom /dvdchmod +x /etc/rc.d/rc.localgrep dvd /etc/rc.d/rc.local || echo 'mount /dev/cdrom /dvd' > /etc/rc.d/rc.localsource /etc/rc.d/rc.localcd /etc/yum.repos.dmkdir -pv bak/mv -v *.repo bak/cat > dvd.repo <<EOF[centos73]name=centos linux 7.3 localbaseurl=file:///dvdenabled=1gpgcheck=0EOFyum clean allyum repolistmkdir -pv /var/ftp/ks/yum install -y dnsmasq vsftpd curl tftp-server xinetdcp -v /etc/dnsmasq.conf /etc/dnsmasq.conf.bakgrep -Ev '^#|^$' /etc/dnsmasq.confcat >/etc/dnsmasq.d/dhcp.conf <<EOF#接口设置,在94行interface=ens33,lo#监听IP地址,在99行#listen-address=192.168.10.7#绑定接口,在112行#bind-interfaces#域名,#在132行domain=sky.com# DHCP range-leases,dhcp租约信息,在145行dhcp-range= ens33,192.168.10.100,192.168.10.200,255.255.255.0,1h#ip和mac地址绑定#dhcp-host=aa:bb:cc:dd:ee:ff,192.168.111.50# PXE启动文件和tftp服务器ip地址,在418行dhcp-boot=pxelinux.0,pxeserver,192.168.10.7 # Gateway网关,在312行dhcp-option=3,192.168.10.1# DNS服务器IP地址dhcp-option=6,114.114.114.114,8.8.8.8server=8.8.4.4# Broadcast Address广播地址dhcp-option=28,192.168.10.255# NTP Server网络时间服务器IP地址,dhcp-option=42,0.0.0.0#pxe启动时的命令提示,在448行pxe-prompt="Press F8 for menu.", 5#pxe服务,在458行pxe-service=x86PC,"Install CentOS 7 from network server 192.168.10.7", pxelinux#启用tftp服务,在473行enable-tftp#tftp文件共享的根目录,在476行tftp-root=/var/lib/tftpbootEOFgrep -Ev '^#|^$' /etc/dnsmasq.d/dhcp.confyum install -y syslinux mlocateyum install -y tftp-server xinetdls /usr/share/syslinuxupdatedblocate pxelinux.0cp -v /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/mkdir /var/lib/tftpboot/pxelinux.cfgtouch /var/lib/tftpboot/pxelinux.cfg/defaultcat > /var/lib/tftpboot/pxelinux.cfg/default <<EOFdefault menu.c32prompt 0timeout 15ONTIMEOUT localmenu title ########## PXE Boot Menu ##########label 1menu label ^1)InstallCentOS7 x64 withLocalRepomenu defaultkernel centos7/vmlinuzappend initrd=centos7/initrd.img method= devfs=nomount#ks=ftp://192.168.10.7/ks/7text.kslabel 2menu label ^2)InstallCentOS7 x64 with Repokernel centos7/vmlinuzappend initrd=centos7/initrd.img method= devfs=nomount ip=dhcplabel 3menu label ^3)InstallCentOS7 x64 withLocalRepousing VNCkernel centos7/vmlinuzappend initrd=centos7/initrd.img method= devfs=nomount inst.vnc inst.vncpassword=passwordlabel 4menu label ^4)Bootfromlocal driveEOFcat /var/lib/tftpboot/pxelinux.cfg/default mkdir -v /var/lib/tftpboot/centos7/cp -v /dvd/isolinux/* /var/lib/tftpboot/centos7/pkill -9 dnsmasqsystemctl restart dnsmasqsystemctl restart vsftpdsystemctl status dnsmasqsystemctl status vsftpdsystemctl enable dnsmasqsystemctl enable vsftpdnetstat -atunlp|grep dnsmasq#脚本编写完毕