Linux servers power much of our digital infrastructure, from corporate intranets to cloud services. Their security is paramount in today’s threat landscape.
This comprehensive hardening guide provides concrete steps to secure your Linux servers against various attack vectors, complete with configuration examples and commands.
Initial System Configuration and Documentation
1. Document Host Information
Before beginning any hardening work, document your server’s vital information:
text# Create a hardening documentation file
echo "Server Hardening Documentation" > ~/server_hardening.txt
echo "Machine name: $(hostname)" >> ~/server_hardening.txt
echo "IP address: $(hostname -I)" >> ~/server_hardening.txt
echo "MAC address: $(ip link | grep link/ether | awk '{print $2}')" >> ~/server_hardening.txt
echo "Date of hardening: $(date)" >> ~/server_hardening.txt
This documentation helps track what security measures have been implemented and when.
2. BIOS/UEFI Protection
Secure the hardware layer first:
- Set strong BIOS/UEFI passwords
- Enable Secure Boot if available
- Disable booting from external devices
- Update firmware to latest versions
3. Disk Encryption with LUKS
Implement full disk encryption to protect data at rest:
text# Encrypt root partition during installation or
# for existing non-root partitions:
cryptsetup luksFormat /dev/sdb1
cryptsetup luksOpen /dev/sdb1 encrypted_volume
mkfs.ext4 /dev/mapper/encrypted_volume
LUKS supports multiple key slots, allowing for recovery keys and user passphrases while maintaining strong security.
4. Secure GRUB Boot Loader
Protect your boot process with password protection:
text# Generate a password hash
grub-mkpasswd-pbkdf2
# Add to /etc/grub.d/40_custom
cat >> /etc/grub.d/40_custom << EOF
set superusers="root"
password_pbkdf2 root grub.pbkdf2.sha512.10000.[hash]
EOF
# Modify /etc/grub.d/10_linux to add --unrestricted
# Update GRUB configuration
update-grub
This prevents unauthorized modification of kernel parameters during boot.
User and Authentication Management
5. Enforce Strong Password Policies
Configure strong password requirements:
text# Edit /etc/pam.d/common-password to add:
password requisite pam_pwquality.so retry=3 minlen=12 difok=3 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1 enforce_for_root
# Set password expiration policies in /etc/login.defs
PASS_MAX_DAYS 90
PASS_MIN_DAYS 7
PASS_WARN_AGE 14
These settings enforce complex passwords with minimum lengths and character requirements.
6. Configure Multi-Factor Authentication for SSH
Add an additional layer of protection:
text# Install required packages
apt install libpam-google-authenticator
# Configure PAM for SSH
echo "auth required pam_google_authenticator.so" >> /etc/pam.d/sshd
# Edit /etc/ssh/sshd_config
ChallengeResponseAuthentication yes
UsePAM yes
# Restart SSH service
systemctl restart sshd
Run google-authenticator as each user to set up TOTP authentication.
Network Security
7. Configure Firewall with UFW
Ubuntu’s Uncomplicated Firewall provides strong protection:
text# Install UFW if not present
apt install ufw
# Set default policies
ufw default deny incoming
ufw default allow outgoing
# Allow necessary services
ufw allow ssh
ufw allow 80/tcp
ufw allow 443/tcp
# Enable UFW
ufw enable
UFW manages iptables rules behind the scenes, simplifying firewall management.
8. Configure Advanced iptables Rules
For more granular control, use direct iptables rules:
text# Basic hardened ruleset
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP
iptables -F
# Allow established connections
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED -j ACCEPT
# Allow loopback
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
# Allow SSH
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT
# Save rules
iptables-save > /etc/iptables/rules.v4
Remember to allow necessary services to prevent lockouts.
9. Secure SSH Configuration
Harden SSH with these key configurations:
text# Edit /etc/ssh/sshd_config
Port 2222 # Change default port
Protocol 2 # Use only secure protocol
PermitRootLogin no # Prevent direct root login
PasswordAuthentication no # Force key-based auth
PubkeyAuthentication yes # Enable key authentication
AuthorizedKeysFile .ssh/authorized_keys
AllowUsers username # Restrict to specific users
ClientAliveInterval 300 # Connection timeout
ClientAliveCountMax 2
Restart the SSH service after configuration changes.
System Monitoring and Protection
10. Configure Fail2ban
Protect against brute force attacks:
text# Install Fail2ban
apt install fail2ban
# Create jail.local configuration
cat > /etc/fail2ban/jail.local << EOF
[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 3
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 5
[wordpress]
enabled = true
filter = wordpress
logpath = /var/www/html/logs/access.log
port = http,https
EOF
Create custom filters for your specific applications in the filter.d directory.
11. Set Up System Auditing
Configure auditd for comprehensive logging:
text# Install audit
apt install auditd audispd-plugins
# Add basic audit rules
auditctl -w /etc/passwd -p wa -k user_changes
auditctl -w /etc/shadow -p wa -k password_changes
auditctl -w /etc/sudoers -p wa -k sudoers_changes
auditctl -w /var/log/auth.log -p wa -k auth_logs
# Make rules persistent
auditctl -l > /etc/audit/rules.d/custom.rules
These rules track changes to critical system files.
Access Control and File System Security
12. Enable and Configure SELinux/AppArmor
Both provide Mandatory Access Control:
text# For SELinux (RHEL-based)
# Check status
sestatus
# Set enforcing mode
setenforce 1
# Edit /etc/selinux/config
SELINUX=enforcing
text# For AppArmor (Debian/Ubuntu)
apt install apparmor-utils
aa-enforce /etc/apparmor.d/*
These tools offer fine-grained control over which applications can access resources.
13. Implement Proper File Permissions
Secure file permissions for sensitive directories:
text# Secure /etc/shadow and /etc/passwd
chmod 600 /etc/shadow
chmod 644 /etc/passwd
# Set proper permissions for home directories
find /home -type d -exec chmod 750 {} \;
# Secure SSH files
find /etc/ssh/ -name "ssh_host_*_key" -exec chmod 600 {} \;
System Updates and Vulnerability Detection
14. Configure Automatic Security Updates
Keep your system patched:
text# For Ubuntu/Debian
apt install unattended-upgrades
dpkg-reconfigure -plow unattended-upgrades
# For RHEL/CentOS
dnf install dnf-automatic
systemctl enable --now dnf-automatic.timer
15. Install Rootkit Detection Tools
Scan for rootkits and other malware:
text# Install Rkhunter
apt install rkhunter
# Update database
rkhunter --update
# Run initial scan
rkhunter --check
# Configure automated scans
echo "CRON_DAILY_RUN=\"true\"" >> /etc/default/rkhunter
Rkhunter scans your system for rootkits, backdoors, and local exploits.
Kernel Hardening
16. Implement sysctl Security Settings
Harden the kernel with these settings:
text# Create /etc/sysctl.d/99-security.conf
cat > /etc/sysctl.d/99-security.conf << EOF
# Network security
net.ipv4.conf.all.rp_filter=1
net.ipv4.conf.default.rp_filter=1
net.ipv4.tcp_syncookies=1
net.ipv4.conf.all.accept_redirects=0
net.ipv4.conf.default.accept_redirects=0
net.ipv6.conf.all.accept_redirects=0
net.ipv6.conf.default.accept_redirects=0
# Prevent core dumps
fs.suid_dumpable=0
# Restrict kernel pointers
kernel.kptr_restrict=2
EOF
# Apply settings
sysctl -p /etc/sysctl.d/99-security.conf
These settings protect against common network attacks and reduce information leakage.
Conclusion
Properly hardened Linux servers form the foundation of a secure infrastructure. This checklist addresses the most critical security aspects, but remember that security is an ongoing process, not a one-time task.
Regularly audit your systems, stay up to date with security advisories, and update your hardening techniques as new threats emerge.
Consider implementing compliance frameworks, such as CIS Benchmarks, to establish standardized security baselines across your infrastructure.
Find this News Interesting! Follow us on Google News, LinkedIn, & X to Get Instant Updates!
