all the steps to optimize performance on Ubuntu 22.04, along with exception handling. This script will be combined into a single shell script for easy execution.
# Optimize Ubuntu 22.04 Performance
This guide provides a comprehensive set of steps to optimize performance on Ubuntu 22.04. The steps include disabling unnecessary services, optimizing I/O scheduler, disabling Transparent Huge Pages (THP), enabling write caching, optimizing swappiness and VFS cache pressure, disabling IPv6, optimizing file descriptors, enabling CPU frequency scaling, disabling unnecessary kernel modules, optimizing boot parameters, enabling preload, and optimizing system updates.
## Steps
### 1. Disable Unnecessary Services
Disable unnecessary services to free up system resources.
```bash
sudo systemctl disable snapd.service
sudo systemctl disable snapd.socket
sudo systemctl disable snapd.seeded.service
sudo systemctl disable apt-daily.service
sudo systemctl disable apt-daily.timer
sudo systemctl disable apt-daily-upgrade.service
sudo systemctl disable apt-daily-upgrade.timerThe I/O scheduler determines how disk I/O requests are handled. For SSDs, the noop or none scheduler is often the best choice.
echo 'noop' | sudo tee /sys/block/sda/queue/schedulerTo make this change permanent, add it to /etc/rc.local or create a udev rule.
THP can cause performance issues in some workloads. Disable it for better performance.
echo never | sudo tee /sys/kernel/mm/transparent_hugepage/enabled
echo never | sudo tee /sys/kernel/mm/transparent_hugepage/defragTo make this change permanent, add it to /etc/rc.local or create a systemd service.
Enable write caching for better disk performance.
sudo hdparm -W 1 /dev/sdaAdjust swappiness and VFS cache pressure for better memory management.
sudo sysctl vm.swappiness=10
sudo sysctl vm.vfs_cache_pressure=50To make these changes permanent, add them to /etc/sysctl.conf.
Disable IPv6 if you don't need it.
echo 'net.ipv6.conf.all.disable_ipv6 = 1' | sudo tee -a /etc/sysctl.conf
echo 'net.ipv6.conf.default.disable_ipv6 = 1' | sudo tee -a /etc/sysctl.conf
echo 'net.ipv6.conf.lo.disable_ipv6 = 1' | sudo tee -a /etc/sysctl.conf
sudo sysctl -pIncrease the number of file descriptors for better performance.
echo '* soft nofile 65536' | sudo tee -a /etc/security/limits.conf
echo '* hard nofile 65536' | sudo tee -a /etc/security/limits.conf
echo 'session required pam_limits.so' | sudo tee -a /etc/pam.d/common-sessionEnable CPU frequency scaling for better power management.
sudo apt-get install cpufrequtils
echo 'GOVERNOR="performance"' | sudo tee /etc/default/cpufrequtils
sudo systemctl restart cpufrequtilsDisable unnecessary kernel modules to reduce boot time and resource usage.
sudo nano /etc/modprobe.d/blacklist.confAdd the following lines to disable unnecessary modules:
blacklist usb_storage
blacklist pcspkr
blacklist snd_pcsp
blacklist bluetooth
blacklist btusbOptimize boot parameters for better performance.
sudo nano /etc/default/grubAdd or modify the following line:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash intel_pstate=active no_timer_check"Update GRUB:
sudo update-grubPreload can improve application launch times by preloading frequently used applications into memory.
sudo apt-get install preload
sudo systemctl enable preload
sudo systemctl start preloadOptimize system updates by disabling automatic updates.
sudo nano /etc/apt/apt.conf.d/20auto-upgradesChange the following lines:
APT::Periodic::Update-Package-Lists "0";
APT::Periodic::Download-Upgradeable-Packages "0";
APT::Periodic::AutocleanInterval "0";
APT::Periodic::Unattended-Upgrade "0";#!/bin/bash
# Function to handle errors
handle_error() {
echo "Error: $1" >&2
exit 1
}
# 1. Disable unnecessary services
echo "Disabling unnecessary services..."
sudo systemctl disable snapd.service || handle_error "Failed to disable snapd.service"
sudo systemctl disable snapd.socket || handle_error "Failed to disable snapd.socket"
sudo systemctl disable snapd.seeded.service || handle_error "Failed to disable snapd.seeded.service"
sudo systemctl disable apt-daily.service || handle_error "Failed to disable apt-daily.service"
sudo systemctl disable apt-daily.timer || handle_error "Failed to disable apt-daily.timer"
sudo systemctl disable apt-daily-upgrade.service || handle_error "Failed to disable apt-daily-upgrade.service"
sudo systemctl disable apt-daily-upgrade.timer || handle_error "Failed to disable apt-daily-upgrade.timer"
# 2. Optimize I/O Scheduler
echo "Optimizing I/O scheduler..."
echo 'noop' | sudo tee /sys/block/sda/queue/scheduler || handle_error "Failed to set I/O scheduler"
# 3. Disable Transparent Huge Pages (THP)
echo "Disabling Transparent Huge Pages (THP)..."
echo never | sudo tee /sys/kernel/mm/transparent_hugepage/enabled || handle_error "Failed to disable THP"
echo never | sudo tee /sys/kernel/mm/transparent_hugepage/defrag || handle_error "Failed to disable THP defrag"
# 4. Enable Write Caching
echo "Enabling write caching..."
sudo hdparm -W 1 /dev/sda || handle_error "Failed to enable write caching"
# 5. Optimize Swappiness and VFS Cache Pressure
echo "Optimizing swappiness and VFS cache pressure..."
sudo sysctl vm.swappiness=10 || handle_error "Failed to set swappiness"
sudo sysctl vm.vfs_cache_pressure=50 || handle_error "Failed to set VFS cache pressure"
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf || handle_error "Failed to set swappiness permanently"
echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.conf || handle_error "Failed to set VFS cache pressure permanently"
# 6. Disable IPv6
echo "Disabling IPv6..."
echo 'net.ipv6.conf.all.disable_ipv6 = 1' | sudo tee -a /etc/sysctl.conf || handle_error "Failed to disable IPv6"
echo 'net.ipv6.conf.default.disable_ipv6 = 1' | sudo tee -a /etc/sysctl.conf || handle_error "Failed to disable IPv6"
echo 'net.ipv6.conf.lo.disable_ipv6 = 1' | sudo tee -a /etc/sysctl.conf || handle_error "Failed to disable IPv6"
sudo sysctl -p || handle_error "Failed to apply sysctl settings"
# 7. Optimize File Descriptors
echo "Optimizing file descriptors..."
echo '* soft nofile 65536' | sudo tee -a /etc/security/limits.conf || handle_error "Failed to set file descriptors"
echo '* hard nofile 65536' | sudo tee -a /etc/security/limits.conf || handle_error "Failed to set file descriptors"
echo 'session required pam_limits.so' | sudo tee -a /etc/pam.d/common-session || handle_error "Failed to set file descriptors"
# 8. Enable CPU Frequency Scaling
echo "Enabling CPU frequency scaling..."
sudo apt-get install -y cpufrequtils || handle_error "Failed to install cpufrequtils"
echo 'GOVERNOR="performance"' | sudo tee /etc/default/cpufrequtils || handle_error "Failed to set CPU governor"
sudo systemctl restart cpufrequtils || handle_error "Failed to restart cpufrequtils"
# 9. Disable Unnecessary Kernel Modules
echo "Disabling unnecessary kernel modules..."
sudo nano /etc/modprobe.d/blacklist.conf || handle_error "Failed to edit blacklist.conf"
# 10. Optimize Boot Parameters
echo "Optimizing boot parameters..."
sudo nano /etc/default/grub || handle_error "Failed to edit grub configuration"
sudo update-grub || handle_error "Failed to update GRUB"
# 11. Enable Preload
echo "Enabling preload..."
sudo apt-get install -y preload || handle_error "Failed to install preload"
sudo systemctl enable preload || handle_error "Failed to enable preload"
sudo systemctl start preload || handle_error "Failed to start preload"
# 12. Optimize System Updates
echo "Optimizing system updates..."
sudo nano /etc/apt/apt.conf.d/20auto-upgrades || handle_error "Failed to edit auto-upgrades configuration"
echo "Optimization complete!"This comprehensive script combines all the steps to optimize performance on Ubuntu 22.04, along with exception handling to ensure that any errors are caught and reported. Save this script to a file, make it executable, and run it to apply the optimizations.