ubuntu系统初始化
目前只支持 Ubuntu2004(focal)、2204(jammy)系统
在执行命令时默认要输入两次当前用户的密码,这时由于Ubuntu系统的特性原因,不可避免
脚本详情
#!/bin/bash
codename=$(lsb_release -c | awk '{print $2}')
exit_status=$?
usx="root:qingtongc1231"
#检测是否开启了桌面
check_desktop(){
# 检查是否安装了 GNOME 桌面环境
if [ -x "$(command -v gnome-session)" ]; then
systemctl set-default multi-user.target
fi
}
#修改非root用户权限
modify_user(){
sudo usermod -aG sudo $USER
sudo echo "$USER ALL=(ALL) NOPASSWD: ALL" | sudo tee -a /etc/sudoers > /dev/null 2>&1
}
# install ssh
apt_ssh(){
sudo apt update > /dev/null 2>&1
sudo apt -y install openssh-server > /dev/null 2>&1
sudo sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
sudo service ssh restart
# 判断执行结果,如果失败则退出
if [ $exit_status -ne 0 ]; then
echo "Failed to install and configure SSH server. Exiting..."
exit 1
fi
}
# set root password
set_root_password(){
echo $usx | sudo chpasswd
}
# modify yum sources
modify_yum(){
if [ $codename == "focal" ]; then
cat <<EOF | sudo tee /etc/apt/sources.list > /dev/null 2>&1
deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
# deb https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
EOF
elif [ $codename == "jammy" ]; then
cat <<EOF | sudo tee /etc/apt/sources.list > /dev/null 2>&1
deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
# deb https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
EOF
fi
}
apt_software(){
sudo apt update > /dev/null 2>&1
sudo apt -y install vim curl jq > /dev/null 2>&1
echo -e "\e[32m初始化完毕... 系统将在10秒后重启\e[0m"
echo -e "\e[32m请使用root账密登录: $usx \e[0m"
echo -e "\e[32m本机IP为 $(hostname -I) \e[0m"
sleep 10
# 判断执行结果,如果失败则退出
if [ $exit_status -ne 0 ]; then
echo "Failed to install required software. Exiting..."
exit 1
fi
}
start(){
modify_user
check_desktop
apt_ssh
set_root_password
modify_yum
apt_software
reboot
}
start