服务器地址:192.168.1.110
网段:192.168.1.0
子网掩码:255.255.255.0
网关:192.168.1.254
[[email protected] ~]# cat dhcp.sh
#!/bin/bash
#auto deploy dhcp server for 192.168.1.0/24 netmask
#this script need you have you yum repository
echo "____________mount-cdrom_____"
umount /dev/cdrom &> /dev/null # 卸载光盘
mount /dev/cdrom /mnt &> /dev/null && #重新挂载系统光盘
touch /etc/yum.repos.d/yum.repo # 创建yum的配置文件,名称yum.repo
if [ $? -eq 0 ]
then # 判断创建yum文件成功就执行写入以下参数
cat <<EOF > /etc/yum.repos.d/yum.repo
[server]
name=rehat server yum
baseurl=file:///mnt/Server # URL的访问路径
enabled=1 #启动yum的仓库
gpgcheck=0 #不启用软件包的签名
EOF
else #否则打印出安装yum错误!
echo "_-----install yum error___"
fi
#安装dhcp软件包
echo "___-install dhcp packet__ "
yum -y install dhcp &> /dev/null &&
if [ -f /etc/dhcpd.conf ] #判断是否有/etc/dhcpd.conf这个主配置文件
then #执行
mv /etc/dhcp.conf /etc/dhcpd.conf.save #保存原有的配置文件
fi
#变量定义,主要包括网络,子网,地址等信息
NET=192.168.1.0
MASK=255.255.255.0
RANGE="192.168.1.50 192.168.1.100"
DOMAIN_NAME="breaklinux.com"
ROUTER=192.168.1.254
DNS=8.8.8.8
#创建新的配置文件
echo "______create Configuration file______"
cat <<EOF > /etc/dhcpd.conf
ddns-update-style interim;
ignore client-updates;
subnet $NET netmask $MASK {
# --- default gateway
option routers $ROUTER;
option subnet-mask $MASK;
option nis-domain "$DOMAIN_NAME";
option domain-name "$DOMAIN_NAME";
option domain-name-servers $DNS;
option time-offset -18000; # Eastern Standard Time
# option ntp-servers 192.168.1.1;
# option netbios-name-servers 192.168.1.1;
# --- Selects point-to-point node (default is hybrid). Don‘t change this unless
# -- you understand Netbios very well
# option netbios-node-type 2;
range dynamic-bootp $RANGE;
default-lease-time 21600;
max-lease-time 43200;
# we want the nameserver to appear at a fixed address
host ns {
next-server marvin.redhat.com;
hardware ethernet 12:34:56:78:AB:CD;
fixed-address 207.175.42.254;
}
}
EOF
echo "_____________dhcp Configuration file correctly "
service dhcpd restart #启动dhcp服务
chkconfig dhcpd on #开机启动
搭建dhcp服务器完成
希望大家多多关注breaklinux.com 工作室,有错误的地方请指出,谢谢!
作者:---新
shell 脚本快速部署dhcp服务器!