温馨提示×

如何使用CentOS Cleanup优化系统

小樊
45
2025-03-11 16:03:35
栏目: 智能运维

要使用CentOS Cleanup来优化系统,可以按照以下步骤进行操作:

1. 删除不必要的自带软件包

使用 yum 命令删除不需要的软件包组:

yum groupremove "Development Libraries" "Text-based Internet" "Games and Entertainment" "System Tools"

2. 升级系统和清理缓存

更新系统并清理缓存:

yum update
yum clean all

3. 禁用不必要的服务和进程

禁用不必要的服务和进程以减少资源使用:

# 停止并禁用不必要的服务
service acpid off
service atd stop
service auditd stop
service avahi-daemon stop
service bluetooth stop
# 更多服务...

4. 关闭 SELinux

如果不需要 SELinux,可以将其设置为 disabled:

sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

5. 禁止 IPV6

禁用 IPV6 以减少网络配置的复杂性:

echo "IPV6INIT no" >> /etc/sysconfig/network-scripts/ifcfg-eth0

6. 清理日志文件

清理 /var/log 目录中的日志文件:

find /var/log -type f -name "*.log" -mtime +30 -exec rm -f {} \;

7. 清理 YUM 缓存

清理 YUM 缓存:

yum clean all

8. 删除孤立包

查找并删除孤立包:

package-cleanup --quiet --leaves --exclude-bin | xargs yum remove -y

9. 删除旧内核

只保留最近的几个内核版本:

package-cleanup --oldkernels --count=2

10. 调整系统内核参数

优化内核参数以提高系统性能:

# 编辑 /etc/sysctl.conf 文件
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_tw_reuse = 1
net.core.somaxconn = 4096
# 使配置生效
sysctl -p

11. 使用一键优化脚本

可以使用一键优化脚本来自动化上述步骤:

#!/usr/bin/env bash
# CentOS 7 初始化脚本
clear
echo -ne "\\033[0;33m"
cat<<EOT_oo0oo_08888888088
. "88(| -_- |)0\\ = /0___/'---'\\___.' \\\\\\\\| |// './ \\\\\\\\||| : |||// \\\\/_ ||||| -:- |||||- \\\\| | \\\\\\\\\\\\ - /// | || \\_| ''\\---/'' |_/ |\\ .-\\__ '-' __/-. /___'. .' /--.--\\ '. .'___."" '< '.___\\_<|>_/___.' >' "".| | : '- \\'.;'\\ _ /';.'/ - ' : | |\\ \\ '_. \\_ __\\ /__ _/ .-' / /====='-.____'.___ \\_____/___.-'____.-'====='=---='^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
建议系统 CentOS7
EOT
echo -ne "\\033[m"
init_hostname() {
    while read -p "请输入您想设定的主机名:" namedo
    do
        if [ -z "$name" ];then
            echo -e "\033[31m 您没有输入内容,请重新输入 \033[0m"
            continue
        fi
        read -p "您确认使用该主机名吗?[y/n]: " var
        if [ $var == 'y' -o $var == 'yes' ];then
            hostnamectl set-hostname $name
            break
        fi
    done
}
init_security() {
    systemctl stop firewalldsystemctl disable firewalld &>/dev/null
    setenforce 0
    sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config
    sed -i '/^GSSAPIAu/ s/yes/no/' /etc/ssh/sshd_config
    sed -i '/^#UseDNS/ {s/^#//;s/yes/no/}' /etc/ssh/sshd_config
    systemctl enable sshd
    crond &> /dev/null
    echo -e "\033[32m [安全配置] ==> OK \033[0m"
}
init_yumsource() {
    if [ !-d /etc/yum.repos.d/backup ];then
        mkdir /etc/yum.repos.d/backup
    fimv /etc/yum.repos.d/* /etc/yum.repos.d/backup 2>/dev/null
    if !ping -c 2 baidu.com &>/dev/null
    then
        echo "您无法上外网,不能配置yum源"
        exit
    fi
    curl -o /etc/yum.repos.d/163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo &>/dev/null
    curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo &>/dev/null
    timedatectl set-timezone Asia/Shanghai
    echo "nameserver 114.114.114.114" > /etc/resolv.conf
    echo "nameserver 8.8.8.8" >> /etc/resolv.conf
    chattr +i /etc/resolv.conf
}
init_security
init_yumsource


通过这些步骤,可以有效地优化 CentOS 系统,提高其性能和稳定性。

0