Rocky 8 初始化环境变量 shell 脚本

       

       随着 CentOS 7在2024年6月30日停止技术服务支持,很多采用 CentOS 7 的操作系统开始选择其它可替代操作系统,目前很多企业考虑使用 Rocky 操作系统作为 CentOS 的替代品。

        日常在安装数据库和应用时,通常会对操作系统上的防火墙、时间同步进行设置。为此整理了一个如下的脚本便于初始化服务器系统。

        其它操作系统可参照此脚本进行修改。

--
#!/bin/bash
 
# 日志输出函数
log() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1"
}
 
# 动态获取 IP 地址,排除 127.0.0.1
get_ip_address() {
    log "Detecting IP address (excluding 127.0.0.1)..."
    ip_address=$(ip -o -4 addr list | awk '{print $4}' | cut -d'/' -f1 | grep -v '^127\.0\.0\.1' | head -n1)
 
    if [ -z "$ip_address" ]; then
        log "Error: No valid IP address detected (excluding 127.0.0.1)."
        exit 1
    fi
 
    log "Detected IP address: $ip_address"
}
 
# 获取 IP 地址并提取后两位
get_ip_address
ip_last_two_digits=$(echo "$ip_address" | awk -F '.' '{print $(NF-1) "-" $NF}')
log "IP last two digits detected: $ip_last_two_digits"
 
# 示例:动态生成与 IP 相关的主机信息
hostname_prefix=$(hostname | cut -d'-' -f1-3)
log "Hostname prefix detected: $hostname_prefix"
 
new_hostname="$hostname_prefix-$ip_last_two_digits"
log "Generated hostname (for reference): $new_hostname"
 
# 更新 Rocky Linux 8 的 YUM 源
update_yum_repo() {
    log "Updating YUM repositories for Rocky Linux 8..."
    [ -f /etc/yum.repos.d/CentOS-Base.repo ] && mv /etc/yum.repos.d/CentOS-Base.repo{,.bak}
    [ -f /etc/yum.repos.d/epel.repo ] && mv /etc/yum.repos.d/epel.repo{,.bak}
    cat > /etc/yum.repos.d/Rocky-Base.repo <<EOF
[BaseOS]
name=Rocky Linux 8 - BaseOS
baseurl=http://mirrors.aliyun.com/rockylinux/8/BaseOS/x86_64/os/
enabled=1
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/rockylinux/RPM-GPG-KEY-rockyofficial
EOF
 
    cat > /etc/yum.repos.d/Rocky-AppStream.repo <<EOF
[AppStream]
name=Rocky Linux 8 - AppStream
baseurl=http://mirrors.aliyun.com/rockylinux/8/AppStream/x86_64/os/
enabled=1
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/rockylinux/RPM-GPG-KEY-rockyofficial
EOF
 
    yum clean all || { log "Error: Failed to clean YUM cache"; exit 1; }
    yum makecache || { log "Error: Failed to create YUM cache"; exit 1; }
    log "YUM repositories updated for Rocky Linux 8."
}
update_yum_repo
 
# 设置时区和同步时间
set_timezone() {
    log "Setting timezone to Asia/Shanghai..."
    timedatectl set-timezone Asia/Shanghai
    yum install -y chrony || { log "Error: Failed to install chrony"; exit 1; }
    systemctl enable --now chronyd || { log "Error: Failed to enable chrony service"; exit 1; }
    chronyc tracking || { log "Error: Time synchronization failed"; exit 1; }
    log "Timezone updated and time synchronized using chrony."
}
set_timezone
 
# 更新 limits.conf
update_limits() {
    log "Updating limits.conf..."
    local limits=(
        "* soft nofile 327680"
        "* hard nofile 327680"
        "* soft nproc 131072"
        "* hard nproc 131072"
    )
    local limits_file="/etc/security/limits.conf"
    for limit in "${limits[@]}"; do
        if ! grep -q "$limit" "$limits_file"; then
            echo "$limit" >> "$limits_file"
        fi
    done
    log "limits.conf updated."
}
update_limits
 
# 更新 sysctl.conf
update_sysctl() {
    log "Updating sysctl.conf..."
    local sysctls=("vm.swappiness=1")
    local sysctl_file="/etc/sysctl.conf"
    for sysctl in "${sysctls[@]}"; do
        if ! grep -q "$sysctl" "$sysctl_file"; then
            echo "$sysctl" >> "$sysctl_file"
        fi
    done
    sysctl -p
    log "sysctl.conf updated."
}
update_sysctl
 
# 禁用 NUMA 和 THP
disable_numa_thp() {
    log "Disabling NUMA and THP..."
    sed -i '/^GRUB_CMDLINE_LINUX=/s/quiet"$/quiet numa=off transparent_hugepage=never"/' /etc/default/grub
    if [ -d /sys/firmware/efi ]; then
        grub2-mkconfig -o /boot/efi/EFI/rocky/grub.cfg
    else
        grub2-mkconfig -o /etc/grub2.cfg
    fi
    yum install -y numactl || { log "Error: Failed to install numactl"; exit 1; }
    log "NUMA and THP disabled."
}
disable_numa_thp
 
# 禁用 SELinux
disable_selinux() {
    log "Disabling SELinux..."
    sed -i '/SELINUX=/s/enforcing/disabled/' /etc/selinux/config
    log "SELinux disabled."
}
disable_selinux
 
# 禁用 firewalld
disable_firewalld() {
    log "Disabling firewalld..."
    systemctl stop firewalld
    systemctl disable firewalld
    log "Firewalld disabled."
}
disable_firewalld
 
# 安装额外软件包
install_additional_packages() {
    log "Installing additional packages..."
    yum install -y net-tools sysstat telnet vim || { log "Error: Failed to install packages"; exit 1; }
    log "Additional packages installed."
}
install_additional_packages
 
log "All tasks completed successfully. Please reboot the system!"
 
 
-- 修改脚本权限
chmod +x init_fddb_env.sh
 
-- 执行脚本
sh init_fddb_env.sh

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尚雷_TechTalk01

感谢您的鼓励,我会再接再厉。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值