dnsmasq中文参考资料(必看):https://wiki.archlinux.org/index.php/Dnsmasq_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)
目标:dnsmasq部署pxe服务器脚本
网络环境:
kvm+vsftpd服务器:192.168.10.7
dnsmasq简介
Dnsmasq 提供 DNS 缓存和 DHCP 服务功能。作为域名解析服务器(DNS),dnsmasq可以通过缓存 DNS 请求来提高对访问过的网址的连接速度。作为DHCP 服务器,dnsmasq 可以用于为局域网电脑分配内网ip地址和提供路由。
DNS和DHCP两个功能可以同时或分别单独实现。dnsmasq轻量且易配置,适用于个人用户或少于50台主机的网络。由于dnsmasq可以管理tftp-server服务。因此可以用来搭建PXE服务器。
#临时测试:
systemctl stop firewalld
systemctl stop libvirtd
setenforce 0
#准备本地yum源
mkdir -pv /dvd
mount /dev/cdrom /dvd
chmod +x /etc/rc.d/rc.local
grep dvd /etc/rc.d/rc.local || echo ‘mount /dev/cdrom /dvd‘ > /etc/rc.d/rc.local
source /etc/rc.d/rc.local
cd /etc/yum.repos.d
mkdir -pv bak/
mv -v *.repo bak/
cat > dvd.repo <<EOF
[centos73]
name=centos linux 7.3 local
baseurl=file:///dvd
enabled=1
gpgcheck=0
EOF
yum clean all
yum repolist
yum install -y vsftpd
mkdir -pv /var/ftp/ks/
yum install -y dnsmasq curl tftp-server xinetd syslinux mlocate
mkdir -v /var/lib/tftpboot/pxelinux.cfg /var/lib/tftpboot/centos7/ /var/ftp/dvd/
systemctl restart vsftpd
mount /dev/cdrom /var/ftp/dvd
updatedb
locate pxelinux.0
cp -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 <<EOF
default menu.c32
prompt 0
timeout 300
ONTIMEOUT local
menu title ########## PXE Boot Menu ##########
label 1
menu label ^1)InstallCentOS7 x64 withLocalRepo
kernel centos7/vmlinuz
append initrd=centos7/initrd.img method=ftp://192.168.10.7/dvd devfs=nomount
#ks=ftp://192.168.10.7/ks/7text.ks
label 2
menu label ^2)InstallCentOS7 x64 with http://mirror.centos.org Repo
kernel centos7/vmlinuz
append initrd=centos7/initrd.img method=http://mirror.centos.org/centos/7/os/x86_64/ devfs=nomount ip=dhcp
label 3
menu label ^3)InstallCentOS7 x64 withLocalRepousing VNC
kernel centos7/vmlinuz
append initrd=centos7/initrd.img method=ftp://192.168.10.7/pub devfs=nomount inst.vnc inst.vncpassword=password
label 4
menu label ^4)Bootfromlocal drive
EOF
cat /var/lib/tftpboot/pxelinux.cfg/default
测试dnsmasq手动启动(已测OK):
netstat -atunlp|grep dnsmasq
systemctl stop libvirtd
systemctl stop dnsmasq
pkill -9 dnsmasq
dnsmasq --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/bash
setenforce 0
getenforce
sed -i ‘/^SELINUX=/c\SELINUX=disable‘ /etc/selinux/config
grep ^SELINUX /etc/selinux/config
systemctl stop firewalld
systemctl disable firewalld
systemctl stop libvirtd
systemctl disable libvirtd
read -p ‘selinux和firewalld已关闭,按回车键继续配置本地yum源...‘
#准备本地yum源
mkdir -pv /dvd
mount /dev/cdrom /dvd
chmod +x /etc/rc.d/rc.local
grep dvd /etc/rc.d/rc.local || echo ‘mount /dev/cdrom /dvd‘ > /etc/rc.d/rc.local
source /etc/rc.d/rc.local
cd /etc/yum.repos.d
mkdir -pv bak/
mv -v *.repo bak/
cat > dvd.repo <<EOF
[centos73]
name=centos linux 7.3 local
baseurl=file:///dvd
enabled=1
gpgcheck=0
EOF
yum clean all
yum repolist
mkdir -pv /var/ftp/ks/
yum install -y dnsmasq vsftpd curl tftp-server xinetd
cp -v /etc/dnsmasq.conf /etc/dnsmasq.conf.bak
grep -Ev ‘^#|^$‘ /etc/dnsmasq.conf
cat >/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.8
server=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/tftpboot
EOF
grep -Ev ‘^#|^$‘ /etc/dnsmasq.d/dhcp.conf
yum install -y syslinux mlocate
yum install -y tftp-server xinetd
ls /usr/share/syslinux
updatedb
locate pxelinux.0
cp -v /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
mkdir /var/lib/tftpboot/pxelinux.cfg
touch /var/lib/tftpboot/pxelinux.cfg/default
cat > /var/lib/tftpboot/pxelinux.cfg/default <<EOF
default menu.c32
prompt 0
timeout 15
ONTIMEOUT local
menu title ########## PXE Boot Menu ##########
label 1
menu label ^1)InstallCentOS7 x64 withLocalRepo
menu default
kernel centos7/vmlinuz
append initrd=centos7/initrd.img method=ftp://192.168.10.7/dvd devfs=nomount
#ks=ftp://192.168.10.7/ks/7text.ks
label 2
menu label ^2)InstallCentOS7 x64 with http://mirror.centos.org Repo
kernel centos7/vmlinuz
append initrd=centos7/initrd.img method=http://mirror.centos.org/centos/7/os/x86_64/ devfs=nomount ip=dhcp
label 3
menu label ^3)InstallCentOS7 x64 withLocalRepousing VNC
kernel centos7/vmlinuz
append initrd=centos7/initrd.img method=ftp://192.168.10.7/dvd devfs=nomount inst.vnc inst.vncpassword=password
label 4
menu label ^4)Bootfromlocal drive
EOF
cat /var/lib/tftpboot/pxelinux.cfg/default
mkdir -v /var/lib/tftpboot/centos7/
cp -v /dvd/isolinux/* /var/lib/tftpboot/centos7/
pkill -9 dnsmasq
systemctl restart dnsmasq
systemctl restart vsftpd
systemctl status dnsmasq
systemctl status vsftpd
systemctl enable dnsmasq
systemctl enable vsftpd
netstat -atunlp|grep dnsmasq
#脚本编写完毕
原文地址:http://1364952.blog.51cto.com/1354952/1965677