📋 Table of Contents
⚙️ Configuring Swappiness and Cache Pressure
Before making changes to kernel parameters, it's essential to understand what swappiness and cache pressure control on your Linux system.
Swappiness is a kernel parameter that controls how aggressively the system swaps memory pages from RAM to disk. The value ranges from 0 to 100, where a lower value means less swapping.
Step 1: Check Current Settings
First, let's examine the current kernel parameters using the sysctl command:
sudo sysctl -a
This command displays all kernel parameters. Look for these two important values:
| Parameter | Default Value | Recommended Value | Purpose |
|---|---|---|---|
| vm.swappiness | 60 | 1 | Controls memory swapping behavior |
| vm.vfs_cache_pressure | 100 | 50 | Controls cache reclamation tendency |
Step 2: Create Override Configuration File
Navigate to the sysctl configuration directory and create a custom override file:
cd /etc/sysctl.d/sudo nano custom_overrides.conf
# Swappiness and cache pressure settingsvm.swappiness=1vm.vfs_cache_pressure=50
Configuration Flow Diagram
Settings
sysctl -a
File
custom_overrides.conf
Directives
vm.swappiness=1
Server
sudo reboot
Step 3: Apply Changes
Save the file and reboot the server to apply the changes:
sudo reboot
Step 4: Verify Configuration
After rebooting, verify that the changes have been applied:
sudo sysctl -a | grep swappinesssudo sysctl -a | grep cache_pressure
vm.swappiness = 1
vm.vfs_cache_pressure = 50
🚫 Disabling IPv6
While IPv6 offers many benefits, it can introduce complexity and potential security vulnerabilities if not properly managed. Here are key considerations:
Reasons to Disable IPv6
- Increased Complexity: Managing both IPv4 and IPv6 configurations increases network administration complexity
- Security Risks: IPv6 introduces new attack vectors that may not be covered by existing security tools
- Compatibility Issues: Older hardware and software may not fully support IPv6
- Performance Concerns: Unoptimized IPv6 infrastructure can lead to slower network speeds
- Troubleshooting Challenges: Dual-stack configurations can make issue diagnosis more difficult
Checking IPv6 Status
First, verify if IPv6 is currently enabled:
ip a | grep inet6
Disabling IPv6 Using GRUB
1Navigate to the GRUB configuration directory:
cd /etc/default
2Open the GRUB configuration file:
sudo nano grub
3Find the line containing GRUB_CMDLINE_LINUX and add
the following between the quotation marks:
ipv6.disable=1
GRUB_CMDLINE_LINUX="ipv6.disable=1"
4Update the GRUB bootloader:
sudo update-grub
5Reboot the server:
sudo reboot
6Verify IPv6 is disabled:
ip a | grep inet6
IPv6 Disabling Workflow
Status
Configuration
Bootloader
Verify
🌐 Network Layer Hardening and Optimization
Hardening and optimizing the network layer improves both security and performance. This involves configuring multiple kernel parameters to protect against attacks and handle network traffic efficiently.
Optimization Parameters
| Parameter | Purpose | Benefit |
|---|---|---|
| net.ipv4.ip_local_port_range | Expands available port range | More concurrent connections |
| fs.file-max | Increases maximum open files | Better handle multiple connections |
| fs.suid_dumpable | Disables core dumps for SUID | Enhanced security |
| net.core.somaxconn | Increases connection queue | Handle more connection requests |
| net.core.netdev_max_backlog | Increases packet queue size | Better handle traffic bursts |
Security Hardening Measures
- IP Spoofing Prevention: Blocks packets with false source addresses
- SYN Flood Protection: Defends against denial-of-service attacks
- Source Routing Disable: Prevents malicious traffic redirection
- Exec Shield: Provides additional memory protection
Configuration Steps
1Navigate to the sysctl configuration directory:
cd /etc/sysctl.d/
2Edit the custom overrides file:
sudo nano custom_overrides.conf
3Add network hardening directives:
# IP Spoofing protectionnet.ipv4.conf.all.rp_filter=1net.ipv4.conf.default.rp_filter=1
# SYN flood protectionnet.ipv4.tcp_syncookies=1net.ipv4.tcp_max_syn_backlog=2048net.ipv4.tcp_synack_retries=2net.ipv4.tcp_syn_retries=5
# Disable source routingnet.ipv4.conf.all.accept_source_route=0net.ipv4.conf.default.accept_source_route=0net.ipv6.conf.all.accept_source_route=0net.ipv6.conf.default.accept_source_route=0
4Add network optimization directives:
# Increase number of usable portsnet.ipv4.ip_local_port_range=1024 65535# Increase file handles and inode cachefs.file-max=2097152# Restrict core dumpsfs.suid_dumpable=0# Increase incoming connectionsnet.core.somaxconn=65535net.core.netdev_max_backlog=5000# Increase memory buffersnet.core.optmem_max=25165824# Configure send/receive buffersnet.core.rmem_default=31457280net.core.rmem_max=67108864net.core.wmem_default=31457280net.core.wmem_max=67108864
5Save the file and reboot:
sudo reboot
Network Security Layers
IP Spoofing
Protection
SYN Flood
Defense
Source Routing
Disabled
Performance
Optimization
Understanding SYN Flood Attacks
A SYN flood is a form of denial-of-service attack where an attacker rapidly initiates connections without completing them. The server spends resources waiting for half-open connections, potentially making it unresponsive to legitimate traffic. Our configuration protects against this by enabling SYN cookies and limiting retry attempts.
📝 Summary
By following this comprehensive guide, you have successfully:
- Optimized memory management through swappiness and cache pressure configuration
- Hardened shared memory to prevent unauthorized access
- Disabled IPv6 to reduce complexity and potential attack vectors
- Implemented robust network layer security measures
- Optimized network performance for better throughput and connection handling
- Always create backups before modifying system files
- Document all changes with comments in configuration files
- Test configurations in a development environment first
- Monitor system performance after applying changes
- Keep your server updated with security patches
- Regularly review and adjust parameters based on workload
Your Ubuntu server is now hardened against common attack vectors and optimized for better performance. Regular maintenance and monitoring will ensure continued security and efficiency.