阿里云ECS(Ubuntu 22.04 LTS)优化脚本

#!/bin/bash

set -e

echo "🔧 Updating system..."
apt update -y && apt upgrade -y

echo "⛔ Removing unused services..."
# 关闭 snap,节省数百MB内存
systemctl disable --now snapd snapd.socket || true
apt purge -y snapd gnome-software-plugin-snap || true
rm -rf ~/snap /snap /var/snap /var/lib/snapd

echo "💾 Creating 2G swap file..."
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile none swap sw 0 0' >> /etc/fstab

echo "🧰 Installing common tools..."
apt install -y curl wget git net-tools htop unzip build-essential

echo "⚙️ Tuning sysctl parameters..."
cat >> /etc/sysctl.conf <<EOF

# Reduce swapiness, speed up response
vm.swappiness=10
vm.vfs_cache_pressure=50
EOF
sysctl -p

echo "🚀 Disabling unnecessary timers and services..."
systemctl disable --now apt-daily.timer apt-daily-upgrade.timer || true
systemctl mask systemd-oomd || true

echo "🌐 Setting aliyun apt source..."
sed -i 's@http://.*.ubuntu.com@http://mirrors.aliyun.com@g' /etc/apt/sources.list
apt update -y

echo "✅ Done. System is now optimized."
posted @ 2025-05-13 19:18  江南烟雨梦  阅读(57)  评论(0)    收藏  举报