CentOS 系统启动慢可以通过以下方法进行优化:
在系统启动时,加载过多不必要的服务会拖慢启动速度。可以通过以下命令查看系统启动时加载的服务,并禁用一些不必要的服务:
systemctl list-unit-files --type=service --state=enabled
根据需要禁用一些不必要的服务,例如打印服务、网络服务等。
sudo systemctl disable <service_name>
编辑 /etc/sysctl.conf
文件,根据需要进行内核参数优化。例如:
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_mem = "94500000 915000000 927000000"
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
使用以下命令应用更改:
sudo sysctl -p
禁用 SELinux 和 DNS 反向解析可以加快 SSH 登录速度。编辑 /etc/ssh/sshd_config
文件:
sed -i 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/' /etc/ssh/sshd_config
sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config
重启 SSH 服务:
sudo systemctl restart sshd
禁用 SELinux:
sed -i 's/SELINUXenforcing/SELINUXdisabled/' /etc/selinux/config
禁用 DNS 反向解析:
echo "options ipv6 off" >> /etc/modprobe.d/ipv6.conf
echo "net.ipv4.conf.all.disable_ipv6 = 1" >> /etc/sysctl.conf
sudo sysctl -p
编辑 /etc/inittab
文件,将默认运行级别设置为 3(文本模式):
id:3:initdefault:
将系统的安装源设置为国内的镜像源,例如网易的源:
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget http://mirrors.163.com/.help/CentOS6-Base-163.repo -O /etc/yum.repos.d/CentOS-Base.repo
定期清理系统垃圾文件,例如旧的系统日志文件和临时文件:
sudo journalctl --vacuum-time 7d
sudo systemd-tmpfiles --clean
如果系统内存不足,可以配置 Swap 分区来扩展虚拟内存。创建 Swap 文件并添加到 /etc/fstab
中:
sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
通过以上方法,可以有效优化 CentOS 系统的启动速度。