Sunday, March 16, 2025

Raspberry Pi auto-optimizes scripts

This script removes unnecessary packages, disables unneeded services, optimizes the kernel, and improves system performance.
Script: optimize.sh

#!/bin/bash
set -e

echo "🔧 Removing unnecessary packages..."
apt remove --purge -y snapd lxd lxc* cloud-init cloud-guest-utils 
    ubuntu-advantage-tools landscape-common unattended-upgrades 
    multipath-tools apport
apt autoremove -y && apt clean

echo "🛑 Disabling unnecessary systemd services..."
systemctl disable --now systemd-resolved ufw apt-daily{,-upgrade}.{timer,service} systemd-journald.service
rm -f /etc/resolv.conf && echo "nameserver 8.8.8.8" > /etc/resolv.conf

echo "📌 Disabling swap..."
swapoff -a && sed -i '/swap/d' /etc/fstab

echo "🚀 Optimizing kernel parameters..."
cat <<EOF > /etc/sysctl.d/99-optimization.conf
fs.file-max = 2097152
vm.swappiness = 10
vm.vfs_cache_pressure = 50
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_congestion_control = bbr
EOF
sysctl --system

echo "✅ Optimization completed! Please reboot the system."

How to Use
Save the script as optimize.sh, then execute the following commands:

chmod +x optimize.sh
./optimize.sh

After running the script, reboot the system to apply all changes. 🚀

Add comment

Fill out the form below to add your own comments