当前位置:网站首页>Shell脚本完成pxe装机配置
Shell脚本完成pxe装机配置
2022-08-02 13:58:00 【正大光明瑞士卷】
目录
一、进入脚本界面
脚本界面代码:
#!/bin/bash
echo "-----------------------------------------------"
echo -e "欢迎使用pxe网络装机基础搭建脚本!\n作者:吕佳朋\n1.关闭防火墙,创建网卡ens36\n2.下载必要软件包并挂载系统光盘\n3.配置dhcp、tftp\n4.将装机必要文件移动到/var/lib/tftpboot\n5.exit"
echo "-----------------------------------------------"
read -p "please enter your choice:" i
二、必要手动设置
为虚拟机pxe服务端增加网卡设备,设置自定义仅主机模式(VMnet1)
这张网卡是作为dhcp服务器和ftp服务器为装机来分配ip并安装系统相关文件的,所有不需要设置网关地址和DNS,具体配置见下文。
三、设置网卡,关闭防火墙
firewalldset(){
read -p "请输入你的ens33网卡ip:" net1
read -p "请输入你作为dhcp服务端的网卡ip:" net
echo "=================================关闭系统防护=================================="
systemctl stop firewalld
setecforce 0
systemctl status firewalld
echo `#grep SELINNUX=disabled /etc/sysconfig/selinux`
grep SELINUX=disabled /etc/sysconfig/selinux
echo `#getenforce`
echo "==============================================================================="
echo "防火墙以及防护已关闭!"
echo "==============================================================================="
cp -p /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-ens36
sed -i '/UUID/d' /etc/sysconfig/network-scripts/ifcfg-ens36
#删除ens33网卡的UUID,否则会报错
sed -i 's/ens33/ens36/' /etc/sysconfig/network-scripts/ifcfg-ens36
#将ens33替换成ens36
sed -i s/$net1/$net/ /etc/sysconfig/network-scripts/ifcfg-ens36
#将ens33网卡地址进行替换
sed -i '/DNS/d' /etc/sysconfig/network-scripts/ifcfg-ens36
#将DNS行删除
sed -i '/GATEWAY/d' /etc/sysconfig/network-scripts/ifcfg-ens36
#将网关行删除
sed -i 's/dhcp\|none/static/' /etc/sysconfig/network-scripts/ifcfg-ens36
#将含有“dhcp”或“none”替换成static
ifdown ens36 && ifup ens36
ping -c 1 baidu.com > /dev/null
if [ $? -eq 0 ];then
echo "网络连接正常,设置完成!"
elif [ $? -ne 0 ];then
echo "网络设置出现错误,请检查"
fi
}
please enter your choice:1
请输入你的ens33网卡地址:192.168.116.22
请输入你作为dhcp服务端的网卡ip:192.168.100.100
检验:
四、下载相关服务并挂载系统光盘
yum下dhcp、tftp-server、syslinux、xinetd、system-config-kickstart、vsftpd
挂载系统光盘到/mnt下
install(){
echo "================================开始软件包安装================================"
yum install -y dhcp
yum install -y tftp-server
yum install -y syslinux
yum install -y xinetd
yum install -y system-config-kickstart
yum install -y vsftpd
echo "===============================开始系统光盘挂载==============================="
mount /dev/sr0 /mnt
echo "=============================================================================="
echo "必要软件包安装成功,系统光盘已挂载"
mount | grep /dev/sr0
echo "=============================================================================="
}
五、进行服务配置
复制一份dhcp.conf.example到/etc/dhcp/dhcpd.conf,并写入:
subnet 192.168.100.0 netmask 255.255.255.0 {
range 192.168.100.40 192.168.100.50;
option routers 192.168.100.100;
option domain-name-servers 114.114.114.114;
next-server 192.168.100.100;
filename \"pexlinux.0\";
其他的具体配置可以看之前的配置pxe装机的博客 :https://blog.csdn.net/m0_71518373/article/details/125923938?spm=1001.2014.3001.5502
serviceset(){
cp -p /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf
echo "ddns-update-style none;
subnet 192.168.100.0 netmask 255.255.255.0 {
range 192.168.100.40 192.168.100.50;
option routers 192.168.100.100;
option domain-name-servers 114.114.114.114;
next-server 192.168.100.100;
filename \"pexlinux.0\";
}">> /etc/dhcp/dhcpd.conf
systemctl enable dhcpd
systemctl start dhcpd
sed -i '/wait/s/yes/no/' /etc/xinetd.d/tftp
sed -i '/disable/s/yes/no/' /etc/xinetd.d/tftp
systemctl enable tftp
systemctl start tftp
mkdir /var/ftp/centos7
cp -rf /mnt/* /var/ftp/centos7
systemctl enable vsftpd
systemctl start vsftpd
mkdir /var/lib/tftpboot/pxelinux.cfg
echo "default auto
prompt 1
label auto
kernel vmlinuz
append initrd=initrd.img method=ftp://192.168.100.100/centos7 ks=ftp://192.168.100.100/ks.cfg
label linux text
kernel vmlinuz
append text initrd=initrd.img method=ftp://192.168.100.100/centos7 ks=ftp://192.168.100.100/ks.cfg
label linux rescue
kernel vmlinuz
append rescue initrd=initrd.img method=ftp://192.168.100.100/centos7 ks=ftp://192.168.100.100/ks.cfg">> /var/lib/tftpboot/pxelinux.cfg/default
echo "---------------------------------------------------------------------------"
echo "The main services are ready!"
echo "dhcpd:"
systemctl status dhcpd | grep -i active
echo "tftp:"
systemctl status tftp | grep -i active
echo "vsftpd:"
systemctl status vsftpd | grep -i active
echo "----------------------------------------------------------------------------"
}
六、将装机文件移动到tftp文件夹
简单的cp命令
filemv(){
cp /mnt/images/pxeboot/initrd.img /var/lib/tftpboot/
cp /mnt/images/pxeboot/vmlunz /var/lib/tftpboot/
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
echo "---------------------------------------------"
echo "The main pxe-files are already in position!"
echo "请进入桌面点击kickstart进行配置!"
echo "感谢使用,熊猫烧香"
echo "---------------------------------------------"
exit
}
kickstart这里还是需要在桌面环境中操作,并将保存的配置文件存放在 var/ftp/下
七、使用case让用户选择使用函数方法
main()
{
case $i in
1)
firewalldset
;;
2)
install
;;
3)
serviceset
;;
4)
filemv
;;
5)
echo "感谢使用,熊猫烧香"
exit
;;
*)
echo "错误的输入,请重新选择"
;;
esac
}
main $*
边栏推荐
- The world's largest Apache open source foundation is how it works?
- 打破文件锁限制,以存储力量助力企业增长新动力
- 数据机构---第六章图---图的遍历---选择题
- 泡利不相容原理适用的空间范围(系统)是多大?
- tinymce 如何实现动态国际化
- 【C语言】手撕循环结构 —— while语句
- 史上最全!47个“数字化转型”常见术语合集,看完秒懂~
- rpm包的卸载与安装[通俗易懂]
- [typescript] Use the RangePicker component in antd to implement time limit the previous year (365 days) of the current time
- 腾讯安全发布Tencent Cloud EdgeOne,为企业出海打造安全加速一体化服务
猜你喜欢
随机推荐
苹果,与Web3 “八字不合”
rpm包的卸载与安装[通俗易懂]
如何解决1045无法登录mysql服务器
不精确微分/不完全微分(Inexact differential/Imperfect differential)
The future of financial services will never stop, and the bull market will continue 2021-05-28
WiFi Association&Omnipeek抓包分析
C# 编译错误:Compiler Error CS1044
Selenium本地打开远程浏览器
第二届中国Rust开发者大会(RustChinaConf 2021~2022)线上大会正式开启报名
[C language] Analysis of function recursion (1)
Gstreamer Plugin注册流程详解
如何解决mysql服务无法启动1069
目前想通过提取本地excel文件创建数据表,在sql语句这出了一些问题
Summer training camp-week2 graph theory
如何选择正规的期货交易平台开户?
Configure zabbix auto-discovery and auto-registration.
RKMPP库快速上手--(一)RKMPP功能及使用详解
LeetCode(剑指 Offer)- 53 - II. 0~n-1中缺失的数字
微信小程序-最近动态滚动实现
腾讯安全发布Tencent Cloud EdgeOne,为企业出海打造安全加速一体化服务