CentOS系统优化脚本

脚本介绍 安装基本常用软件 调整最大文件数量 关闭历史文件记录 开启时间同步 安装docker 脚本下载 下载

脚本介绍

  • 安装基本常用软件

  • 调整最大文件数量

  • 关闭历史文件记录

  • 开启时间同步

  • 安装docker

脚本下载

下载https://drive.flycloud.club/d/Shell/system-optimal.sh

脚本预览

#!/bin/bash

set -e  # 启用错误处理机制,遇到错误即退出脚本

# 询问是否优化系统源
read -p "是否优化系统源?(Y/N): " opt
if [[ "$opt" =~ ^(Y|y)$ ]]; then
    optimize_system_source
fi

# 安装基本常用软件
install_basic_packages

# 调整最大文件数量
adjust_max_file_limit

# 关闭历史命令记录
disable_command_history

# 开启时间同步
enable_time_sync

# 询问是否安装 Docker
read -p "是否安装 Docker?(Y/N): " docker_opt
if [[ "$docker_opt" =~ ^(Y|y)$ ]]; then
    install_docker

    # 询问是否安装 Docker-Compose
    read -p "是否安装 Docker-Compose?(Y/N): " docker_compose_opt
    if [[ "$docker_compose_opt" =~ ^(Y|y)$ ]]; then
        install_docker_compose
    fi
fi

# 退出脚本
exit 0

# 函数定义

# 优化系统源
optimize_system_source() {
    curl -sSL https://drive.flycloud.club/d/Shell/main.sh > /tmp/main.sh
    bash /tmp/main.sh
}

# 安装基本常用软件
install_basic_packages() {
    yum install -y wget lrzsz tree bash-completion vim yum-utils nfs-utils autofs net-tools chrony htop iftop git ansible
}

# 调整最大文件数量
adjust_max_file_limit() {
    if ! grep -q "* soft nofile 1000000" /etc/security/limits.conf; then
        echo "* soft nofile 1000000" >> /etc/security/limits.conf
        echo "* hard nofile 1000000" >> /etc/security/limits.conf
        echo '* -    nofile 1000000' >> /etc/security/limits.conf
        ulimit -n 1000000
    fi
}

# 关闭历史命令记录
disable_command_history() {
    if ! grep -q "export HISTFILESIZE=0" ~/.bash_profile; then
        echo "export HISTFILESIZE=0" >> ~/.bash_profile
        source ~/.bash_profile
    fi
}

# 开启时间同步
enable_time_sync() {
    timedatectl set-timezone Asia/Shanghai
    timedatectl set-ntp yes
}

# 安装 Docker
install_docker() {
    curl -o /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    yum clean all && yum makecache && yum -y install docker-ce
    systemctl enable --now docker
    cat << EOF | tee /etc/docker/daemon.json
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "bip": "172.17.0.1/24",
  "registry-mirrors": ["http://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn"],
  "live-restore": true
}
EOF
    systemctl daemon-reload && systemctl restart docker
}

# 安装 Docker-Compose
install_docker_compose() {
    curl -sSL -o /usr/local/bin/docker-compose https://www.gitlink.org.cn/attachments/entries/get_file?download_url=https://www.gitlink.org.cn/api/lengleng/mirror/raw/docker-compose-linux-x86_64?ref=master
    chmod +x /usr/local/bin/docker-compose
    docker-compose -v
}
LICENSED UNDER CC BY-NC-SA 4.0
评论
访问统计
总访问量: 👀| 访客数: 🚶‍♂️