A Comprehensive Guide to Server Security and Performance Tuning
After completing the fundamental steps in server hardening, we now move to more advanced aspects of server security and optimization. This guide covers essential topics that will help you squeeze every bit of performance out of your Ubuntu server while maintaining robust security.
Set server time zone to match your local time for accurate task scheduling
Configure virtual memory to prevent server crashes during memory exhaustion
Secure shared memory space against potential exploits
Disable IPv6 via GRUB configuration when not needed
Harden and optimize default TCP/IP stack configuration
Implement BBR algorithm to increase throughput and reduce latency
Disable access time tracking for performance boost
Increase maximum open files per process
Setting your server's time zone to match your local time is crucial for several reasons:
sudo timedatectl
sudo timedatectl list-timezones
# Filter for specific region
sudo timedatectl list-timezones | grep Paris
# Filter by continent
sudo timedatectl list-timezones | grep Europe
sudo timedatectl set-timezone Europe/Paris
# Verify the change
sudo timedatectl
# Alternative verification
date
Swap is disk space used when physical RAM is full. It acts as a safety net but is NOT a substitute for physical RAM.
Fast Access
Primary Memory
Limited Size
Slower Access
Virtual Memory
Safety Net
Slowest Access
Permanent Storage
Large Capacity
When you run sudo swapon -s and see /dev/zram0, you have zram
swap instead of a traditional swap file.
sudo swapon -s
# Alternative check with htop
htop
# Look at Swap line: Swp 0K/2.34G
If your system has zram and is running fine, keep it and just optimize the settings:
# Check current swappiness
cat /proc/sys/vm/swappiness
# Set swappiness to 1 (use swap only as last resort)
sudo sysctl vm.swappiness=1
# Set cache pressure to 50
sudo sysctl vm.vfs_cache_pressure=50
# Make these permanent
sudo nano /etc/sysctl.conf
Add these lines at the end of the file:
vm.swappiness=1
vm.vfs_cache_pressure=50
Then save (Ctrl+O, Enter, Ctrl+X) and apply:
sudo sysctl -p
# Verify
cat /proc/sys/vm/swappiness
cat /proc/sys/vm/vfs_cache_pressure
Only do this if you specifically need a traditional swap file (usually not necessary).
| RAM Amount | Recommended Swap Size | Notes |
|---|---|---|
| 1 GB | 2 GB | 2x RAM size |
| 2-8 GB | 4 GB | Equal to RAM |
| 16-24 GB | 8 GB | Half of RAM |
| > 24 GB | 8-16 GB | Swap becomes less critical |
# 1. Disable swap
sudo swapoff /swapfile
# 2. Backup fstab
cd /etc
sudo cp fstab fstab.back
# 3. Edit fstab and remove swap entry
sudo nano fstab
# Remove line: /swapfile swap swap defaults 0 0
# 4. Delete swap file
cd /
sudo rm /swapfile
# 5. Verify removal
sudo swapon -s
# Should return no output
# 6. Reboot to confirm
sudo reboot
# 1. Disable zram
sudo swapoff /dev/zram0
# 2. Remove zram package (to prevent it from restarting)
sudo apt remove zram-config
# 3. Then proceed to create traditional swap file
Formula for Block Count:
# Create 2GB swap file
sudo dd if=/dev/zero of=/swapfile bs=1024 count=2097152
# Set correct permissions (only root can read/write)
sudo chmod 600 /swapfile
# Format as Linux swap
sudo mkswap /swapfile
# Activate swap
sudo swapon /swapfile
# Verify swap status
sudo swapon -s
# Edit fstab
cd /etc
sudo nano fstab
# Add this line at the bottom:
/swapfile swap swap defaults 0 0
# Save and reboot
sudo reboot
# After reboot, verify
sudo swapon -s
htop
Swappiness is a Linux kernel property (range: 0-100) that defines how aggressively the system uses swap space:
VFS (Virtual File System) Cache Pressure controls the kernel's tendency to reclaim memory used for caching directory and inode objects. We set this to 50 for optimal performance.
Low swappiness keeps more data in RAM → Reduces disk access → More space for VFS cache → Stores frequently used directory/file information → Improved overall performance
# Check current swappiness
cat /proc/sys/vm/swappiness
# Set swappiness to 1 (temporary)
sudo sysctl vm.swappiness=1
# Set VFS cache pressure to 50 (temporary)
sudo sysctl vm.vfs_cache_pressure=50
# Make changes permanent - edit sysctl.conf
sudo nano /etc/sysctl.conf
# Add these lines at the end:
vm.swappiness=1
vm.vfs_cache_pressure=50
# Apply changes
sudo sysctl -p
# Verify settings
cat /proc/sys/vm/swappiness
cat /proc/sys/vm/vfs_cache_pressure
| Parameter | Default Value | Recommended Value | Purpose |
|---|---|---|---|
| vm.swappiness | 60 | 1 | Minimize swap usage for database performance |
| vm.vfs_cache_pressure | 100 | 50 | Balance between reclaiming cache and keeping it in memory |
Scenario: You upgrade your server from 1GB to 2GB RAM
Action Required (for traditional swap):
Action Required (for zram):
# Check file sizes
ls -l /etc/fstab*
# Restore from backup
sudo cp /etc/fstab.back /etc/fstab
# Verify restoration
ls -l /etc/fstab*
# Both files should now have the same size
# Real-time monitoring with htop
htop
# Press F2 for setup, configure display columns
# Check swap usage
free -h
# Detailed swap information
sudo swapon --show
# Check if swap entry exists in fstab
cat /etc/fstab | grep swap
# If missing, add it
sudo nano /etc/fstab
# Add: /swapfile swap swap defaults 0 0
# Ensure you're using sudo
sudo dd if=/dev/zero of=/swapfile bs=1024 count=2097152
# Check if you're in the root directory
pwd
# Should output: /
Possible Causes:
Solutions:
# Monitor with:
vmstat 1 # to see swap activity
# Reinstall zram if you removed it and want it back
sudo apt install zram-config
sudo reboot
# Verify
sudo swapon -s
Advanced server hardening and optimization is essential for maintaining a secure, high-performance Ubuntu server. The topics covered in this guide provide a solid foundation for: