Linux初始化脚本

使用菜单选项进行初始化配置
脚本下载链接

#!/bin/bash
#
#********************************************************************
#Author:				chenjiahao
#QQ:					1938191576
#Date:					2022-12-12
#FileName:				initialize.sh
#URL:					https://www.placjh.com
#Description:			The deploy script
#Copyright (C):			2022 All rights reserved
#********************************************************************

initialize() {
	Variable_setting() {
	#变量设置
		PREINSTALL_HOSTNAME=test.placjh.com
		ETH=eth0
		PREINSTALL_IPADDR=10.0.0.10
		PREINSTALL_NETMASK=8
		PREINSTALL_GATEWAY=10.0.0.2
		PREINSTALL_DNS1=10.0.0.2
		PREINSTALL_MIRRORS=tsinghua
		
		. /etc/os-release
	}
	
	installing_software() {
	#安装软件
		if [ $ID == "centos" -o $ID == "rocky" ];then
			yum -y install lrzsz tar wget vim zip unzip &>/dev/null
		elif [ $ID == "ubuntu" ];then
			apt -y install lrzsz tar wget vim zip unzip &>/dev/null
		else
			echo "不支持的OS"
			exit
		fi
	}
	
	close_selinux() {
	#关闭selinux
		sed -i 's/^SELINUX=/SELINUX=disabled/' /etc/selinux/config &>/dev/null
		setenforce 0 &>/dev/null
	}
	
	close_firewalld(){
	#关闭防火墙
		if [ $ID == "centos" ];then
			systemctl disable --now firewalld &>/dev/null
		elif [ $ID == "ubuntu" ];then
			systemctl disable --now ufw &>/dev/null
		else
			echo "不支持的OS"
			exit
		fi
	}
	
	Changing_the_Time_Zone() {
	#修改时区,安装chrony
		sudo timedatectl set-timezone Asia/Shanghai &>/dev/null
		if [ $ID == "centos" -o $ID == "rocky" ];then
			yum -y install chronyd &>/dev/null
		elif [ $ID == "ubuntu" ];then
			apt -y install chrony &>/dev/null
		else
			echo "不支持的OS"
			exit
		fi
	}
	
	Modifying_a_Host_Name() {
	#修改主机名称
		read -p "请输入主机名,默认为$PREINSTALL_HOSTNAME" -t 15 MY_HOSTNAME
		hostnamectl set-hostname ${MY_HOSTNAME:-$PREINSTALL_HOSTNAME} &>/dev/null
	}
	
	Configuring_the_Source() {
	#配置源
		read -p "你要使用清华源还是阿里源[aliyun/tsinghua],默认$PREINSTALL_MIRRORS,:" -t 15 MY_MIRRORS
		case ${MY_MIRRORS:-$PREINSTALL_MIRRORS} in
		tsinghua)
			if [ $ID == "centos" ];then
				sudo sed -e 's|^mirrorlist=|#mirrorlist=|g' -e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.tuna.tsinghua.edu.cn|g' -i.bak /etc/yum.repos.d/CentOS-*.repo &>/dev/null
				wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-$VERSION_ID.repo &>/dev/null
			elif [ $ID == "ubuntu" ];then
				sudo sed -i "s@http://.*archive.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list &>/dev/null
				sudo sed -i "s@http://.*security.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list &>/dev/null
			else
				echo "不支持的OS!"
				exit
			fi
			;;
		aliyun)
			if [ $ID == "centos" ];then
				sudo sed -e 's|^mirrorlist=|#mirrorlist=|g' -e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.aliyun.com|g' -i.bak /etc/yum.repos.d/CentOS-*.repo &>/dev/null
				wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-$VERSION_ID.repo &>/dev/null
			elif [ $ID == "ubuntu" ];then
				sudo sed -i "s@http://.*archive.ubuntu.com@https://mirrors.aliyun.com@g" /etc/apt/sources.list &>/dev/null
				sudo sed -i "s@http://.*security.ubuntu.com@https://mirrors.aliyun.com@g" /etc/apt/sources.list &>/dev/null
			else
				echo "不支持的OS!"
				exit
			fi
			;;
		*)
			echo "错误!请输入[aliyun/tsinghua]"
			exit
		esac
	}
	
	Changing_a_NIC_Name() {
	#修改网卡名字为$ETH
		grep 'net.ifnames=0' /etc/default/grub &>/dev/null
		if ! [ $? -eq 0 ];then
			sudo sed -i '/^GRUB_CMDLINE_LINUX=/s/"$/net.ifnames=0"/' /etc/default/grub &>/dev/null
		fi
		ip a|grep eth0
		if ! [ $? -eq 0 ];then
			if [ $ID == "centos" ];then
				grub2-mkconfig -o /boot/grub2/grub.cfg &>/dev/null
			elif [ $ID == "ubuntu" ];then
				grub-mkconfig -o /boot/grub/grub.cfg &>/dev/null
			fi
		fi
	}
	
	Configuring_a_Static_IP_Address() {
	#配置静态IP
		read -p "请输入IP,默认为$PREINSTALL_IPADDR,:" -t 15 MY_IPADDR
		read -p "请输入掩码,默认为$PREINSTALL_NETMASK,:" -t 15 MY_NETMASK
		read -p "请输入网关,默认为$PREINSTALL_GATEWAY,:" -t 15 MY_GATEWAY
		read -p "请输入DNS,默认为$PREINSTALL_DNS1,:" -t 15 MY_DNS1
		if [ $ID == "centos" ];then
			cat > /etc/sysconfig/network-scripts/ifcfg-$ETH <<-EOF
			NAME=$ETH
			DEVICE=$ETH
			NOBOOT=yes
			IPADDR=${MY_IPADDR:-$PREINSTALL_IPADDR}
			PREFIX=${MY_NETMASK:-$PREINSTALL_NETMASK}
			GATEWAY=${MY_GATEWAY:-$PREINSTALL_GATEWAY}
			DNS1=${MY_DNS1:-$PREINSTALL_DNS1}
			EOF
			nmcli c reload && nmcli c up $ETH &>/dev/null
		elif [ $ID == "ubuntu" ];then
			cat >> /etc/netplan/$ETH-installer-config.yaml <<-EOF
			network:
			  version: 2
			  ethernets:
			    $ETH:
			    addresses: [${MY_IPADDR:-$PREINSTALL_IPADDR}/${MY_NETMASK:-$PREINSTALL_NETMASK}]
			    gateway4: ${MY_GATEWAY:-$PREINSTALL_GATEWAY}
			    nameservers:
			      addresses: [${MY_DNS1:-$PREINSTALL_DNS1}]
			EOF
			netplan apply &>/dev/null
		else
			echo "不支持的OS!"
			exit
		fi
	}
	
	start_menu() {
		menu="
		安装基础软件
		关闭SELinux
		关闭防火墙
		修改时区,安装chrony
		修改主机名称
		配置源
		修改网卡名字
		配置静态IP
		执行以上8项
		重启
		返回上级
		"
		PS3="请输入要进行的操作:"
		select menu in $menu;do
		case $REPLY in
		1)
			echo "开始安装软件"
			installing_software
			;;
		2)
			echo "关闭SELinux"
			close_selinux
			;;
		3)
			echo "关闭防火墙"
			close_firewalld
			;;
		4)
			echo "修改时区,安装chrony"
			Changing_the_Time_Zone
			;;
		5)
			echo "修改主机名称"
			Modifying_a_Host_Name
			;;
		6)
			echo "配置源"
			Configuring_the_Source
			;;
		7)
			echo "修改网卡名字"
			Changing_a_NIC_Name
			;;
		8)
			echo "配置静态IP"
			Configuring_a_Static_IP_Address
			;;
		9)
			echo "开始初始化"
			installing_software
			close_selinux
			close_firewalld
			Changing_the_Time_Zone
			Modifying_a_Host_Name
			Configuring_the_Source
			Changing_a_NIC_Name
			Configuring_a_Static_IP_Address
			;;
		10)
			echo "重启"
			Restart_the_server
			;;
		11)
			func_menu
		esac
		done
	}
	
	Variable_setting
	start_menu
}

Restart_the_server() {
#重启
	reboot
}

Exit_script() {
#退出脚本
	echo "Bye~"
	exit
}

#initialize
func_menu() {
menu="
菜单
退出脚本
"
PS3="请输入要进行的操作:"
select menu in $menu;do
case $REPLY in
1)
	initialize
	;;
2)
	Exit_script
	;;
*)
	echo "错误!"
esac
done
}
func_menu
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值