Sunday, February 2, 2025

Automated Optimization Script (debian11_optimize.sh)

Here’s the automated optimization script for Debian 11 on a KVM VPSï¼
Save the following script as debian11_optimize.sh and run it:
#!/bin/bash

echo "🚀 Starting Debian 11 CLI Optimization for KVM VPS"

# 1. Update System
apt update && apt upgrade -y
apt autoremove --purge -y
apt clean

# 2. Remove Unnecessary Packages
apt remove --purge -y apport popularity-contest avahi-daemon cups rpcbind xserver-common x11-common

# 3. Install KVM-Optimized Kernel
apt install -y linux-image-cloud-amd64

# 4. Update GRUB and Apply Optimizations
sed -i 's/GRUB_CMDLINE_LINUX_DEFAULT="[^"]*/& mitigations=off noibrs no_stf_barrier/g' /etc/default/grub
update-grub

# 5. Enable BBR TCP Congestion Control
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
sysctl -p

# 6. Configure DNS for Faster Resolution
sed -i 's/#DNS=/DNS=8.8.8.8 1.1.1.1/g' /etc/systemd/resolved.conf
systemctl restart systemd-resolved

# 7. SSH Hardening - Change SSH Port
sed -i 's/#Port 22/Port 22022/g' /etc/ssh/sshd_config
systemctl restart ssh

# 8. Install Fail2Ban to Prevent SSH Brute Force
apt install -y fail2ban

# 9. Setup Firewall with UFW
apt install -y ufw
ufw allow 22022/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable

# 10. Disk & I/O Optimization
tune2fs -o journal_data_writeback /dev/vda1
tune2fs -m 1 /dev/vda1
echo "vm.swappiness=10" >> /etc/sysctl.conf
sysctl -p

# 11. CPU & Memory Optimization - Disable Unnecessary Services
systemctl disable --now systemd-resolved avahi-daemon cups
echo "performance" | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

echo "✅ Optimization Complete! Please reboot your server."

Article 9 can be removed, because the firewall is not properly set up, it is easy for you to not even be able to log in to SSH.

1.How to Run the Script:
Create the script file:

nano debian11_optimize.sh

2.Paste the script content into the file, save, and exit.

3.Make the script executable:

chmod +x debian11_optimize.sh

4.Run the script with sudo:

sudo ./debian11_optimize.sh

This will automatically apply all optimizations for your Debian 11 CLI on a KVM VPS. After the script finishes, a reboot is recommended for all settings to take effect.

Add comment

Fill out the form below to add your own comments