Ubuntu系统初始化(脚本)

本文介绍了一个bash脚本,用于Ubuntu20.04(Focal)和22.04(Jammy)系统的初始化,包括设置非root用户权限、安装SSH、修改源列表、安装基本软件并重启,确保使用阿里云镜像加速更新。
摘要由CSDN通过智能技术生成

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值