日常脚本分享(持续更新中...)

centos7/8系统初始化脚本(懒人福音)

bash 复制代码
#! /bin/bash

VERSION_ID=$(grep -oP 'VERSION_ID="\K\d+' /etc/os-release)
DIR=/etc/yum.repos.d
file_path=/etc/chrony.conf


update_yum() {
    cd $DIR && rm -rf * && yum clean all
    if [ "$VERSION_ID" -eq 7 ]; then
        curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
    else
        curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
    fi
    echo "阿里云yum源配置成功!!!" && sleep 2s
}

# 关闭防火墙和SELINUX
firewalld() {
    systemctl stop firewalld && systemctl disable firewalld &> /dev/null
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
    echo "SElinux已禁用,重新启动后才可生效" && sleep 2s
}

# 修改主机名称
set_ps1() {
    echo "PS1='\[\e[35m\][\u@\h \W]#\[\e[m\] '" >> /root/.bashrc
    echo "提示符已修改成功,请重新登录生效" && sleep 2s
}


#设置时间同步,为确保某些机器无法通过正常修改时区实现时间同步所以这里直接修改配置文件

datetime() {
    if [ -e "$file_path" ]; then
        echo "chrony服务已经安装,正在配置"
    else
        yum -y install chrony &> /dev/null
        echo "chrony 已成功安装" && sleep 2s
    fi
    cat <<EOF > /etc/chrony.conf
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html)
# 国家服务器
server 0.cn.pool.ntp.org
server 1.cn.pool.ntp.org
server 2.cn.pool.ntp.org
server 3.cn.pool.ntp.org
# 阿里
server ntp.aliyun.com
# 腾讯
server time1.cloud.tencent.com
server time2.cloud.tencent.com
server time3.cloud.tencent.com
server time4.cloud.tencent.com
server time5.cloud.tencent.com
# if its offset is larger than 1 second.
makestep 1.0 3
# Enable kernel synchronization of the real-time clock (RTC).
rtcsync
# Specify directory for log files.
logdir /var/log/chrony 
EOF
    timedatectl set-timezone Asia/Shanghai && systemctl restart chronyd
    echo "时间同步已同步完成" && sleep 2s
}

# 修改epel源
update_epel() {
    yum clean all &> /dev/null
    if sudo yum -y install epel-release &> /dev/null ; then
    sudo sed -e 's!^metalink=!#metalink=!g' \
    -e 's!^#baseurl=!baseurl=!g' \
    -e 's!https\?://download\.fedoraproject\.org/pub/epel!https://mirrors.tuna.tsinghua.edu.cn/epel!g' \
    -e 's!https\?://download\.example/pub/epel!https://mirrors.tuna.tsinghua.edu.cn/epel!g' \
    -i /etc/yum.repos.d/epel*.repo
    fi
    echo "epel源配置完毕" && sleep 2s
}

# 安装一些常用软件
yum_install(){
    yum update &> /dev/null
    echo "正在安装常用软件..."
    yum -y install vim wget tree net-tools lrzsz zip lsof tcpdump  &> /dev/null
    echo "软件安装完成,所有配置已就绪,系统将在5秒后重启..." && sleep 5s && reboot
}

start(){
    update_yum
    firewalld
    set_ps1
    datetime
    update_epel
    yum_install
}

start
相关推荐
陈苏同学7 分钟前
[已解决] VS Code / Cursor / Trae 的 PowerShell 终端 conda activate 进不去环境的常见问题
linux·windows·conda
我不是秃头sheep14 分钟前
Ubuntu 安装 Docker(镜像加速)完整教程
linux·ubuntu·docker
靡樊26 分钟前
网络基础概念
linux·服务器·网络·c++·学习
Kusunoki_D1 小时前
速查 Linux 常用指令 II
linux·运维·服务器
xmweisi021 小时前
Ansible内置模块之 group
linux·运维·ansible·rhce·rhca·红帽认证
小猪写代码2 小时前
Ubuntu 系统默认已安装 python,此处只需添加一个超链接即可
linux·python·ubuntu
孤寂大仙v2 小时前
【Linux笔记】——Linux线程理解与分页存储的奥秘
linux·运维·笔记
有谁看见我的剑了?3 小时前
ubuntu 22.04 wifi网卡配置地址上网
linux·运维·ubuntu
码农新猿类3 小时前
Ubuntu摄像头打开失败
linux·运维·ubuntu
PWRJOY3 小时前
Ubuntu磁盘空间分析:du命令及常用组合
linux·运维·ubuntu