Linux Production Checklist: Harden Your Server
A hardened Linux server is one where the attack surface has been minimized and the remaining services are configured to resist compromise.
Here’s a checklist to run through for production systems:
1. Secure SSH Access
- Problem: SSH is a primary entry point. Default configurations are often too permissive.
- Diagnosis: Check
/etc/ssh/sshd_configfor settings likePermitRootLogin,PasswordAuthentication,AllowUsers,AllowGroups,Port. - Common Causes & Fixes:
- Root Login Enabled:
PermitRootLogin no- Prevents direct root logins, forcing users to log in as their own user and thensuorsudoto root. This provides an audit trail. - Password Authentication Enabled:
PasswordAuthentication no- Disables password-based logins entirely. This forces the use of SSH keys, which are much harder to brute-force. Ensure you havePubkeyAuthentication yesset. - Unnecessary User/Group Access:
AllowUsers user1 user2orAllowGroups sshusers- Explicitly define who can log in via SSH. If noAllowUsersorAllowGroupsdirective is present, all users can attempt to log in. - Default Port (22):
Port 2222- Changing the default SSH port can reduce automated bot scans, though it’s not a security panacea. Ensure you update any firewall rules accordingly. - Protocol Version 1:
Protocol 2- SSHv1 is deprecated and insecure. Ensure only SSHv2 is allowed.
- Root Login Enabled:
- Restart SSH:
sudo systemctl restart sshd
2. Firewall Configuration (iptables/firewalld)
- Problem: Unnecessary open ports expose services to the network.
- Diagnosis:
iptables:sudo iptables -L -v -nfirewalld:sudo firewall-cmd --list-all
- Common Causes & Fixes:
- Too Many Open Ports: Review the output of the diagnostic commands. For example, if you see
ACCEPTfor port80and443but no web server is running, close it.iptables:sudo iptables -D INPUT -p tcp --dport 80 -j ACCEPT(to remove the rule allowing port 80). Then,sudo iptables-save > /etc/sysconfig/iptables(or equivalent for your distro) to persist.firewalld:sudo firewall-cmd --zone=public --remove-port=80/tcp --permanentfollowed bysudo firewall-cmd --reload.
- Default Policy is ACCEPT:
sudo iptables -P INPUT ACCEPTis dangerous. Change it toDROPorREJECT.iptables:sudo iptables -P INPUT DROP. This requires explicitly allowing all necessary traffic.firewalld: The default zone policy is usually restrictive, but ensure you’re not leaving the default zone wide open.
- Missing Established Connection Tracking: Ensure you have a rule like
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT. This allows return traffic for connections initiated from your server.
- Too Many Open Ports: Review the output of the diagnostic commands. For example, if you see
- Persistence:
iptables:sudo service iptables saveorsudo netfilter-persistent save(depending on distro).firewalld: Changes made with--permanentare persistent.
3. Unnecessary Services
- Problem: Every running service is a potential vulnerability.
- Diagnosis:
sudo systemctl list-units --type=service --state=runningsudo ss -tulnp(shows listening sockets and the processes using them)
- Common Causes & Fixes:
- Unused Daemons: If
ssshows a service listening on a port (e.g.,rpcbindon111/tcporudp) and you don’t need it for NFS or other RPC services, disable it.sudo systemctl disable --now <service-name>(e.g.,sudo systemctl disable --now rpcbind)
- NetworkManager on Servers: On production servers,
NetworkManagercan sometimes interfere with static IP configurations or be an unnecessary service.sudo systemctl disable --now NetworkManager(ensure you have networking configured vianetplan,ifcfg-scripts, etc., before disabling).
- AVAHI/Avahi-daemon: Used for Zeroconf networking, typically not needed on servers.
sudo systemctl disable --now avahi-daemon
- Unused Daemons: If
4. File Integrity Monitoring
- Problem: Detects unauthorized modifications to critical system files.
- Diagnosis: Install and configure AIDE (Advanced Intrusion Detection Environment) or Tripwire.
- Common Causes & Fixes:
- No FIM Installed:
sudo apt update && sudo apt install aide(Debian/Ubuntu) orsudo yum install aide(RHEL/CentOS). - Database Not Initialized: After installation, you must create the initial database.
sudo aide --initsudo mv /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gz
- Regular Checks Not Scheduled: Schedule
aide --checkto run daily via cron or systemd timers.
- No FIM Installed:
- Why it works: AIDE creates a database of file checksums and attributes. When run again, it compares the current state to the database, flagging any changes.
5. Kernel Security Parameters (sysctl)
- Problem: Fine-tune the kernel’s network and memory behavior to resist certain attacks.
- Diagnosis: View current settings with
sudo sysctl -a. Edit/etc/sysctl.confor files in/etc/sysctl.d/. - Common Causes & Fixes:
- IP Spoofing Vulnerability:
net.ipv4.conf.all.rp_filter = 1- Enables reverse path filtering, which checks if the incoming packet’s source IP address is legitimate for the interface it arrived on. - SYN Flood Attacks:
net.ipv4.tcp_syncookies = 1- Enables TCP SYN cookies, a mechanism to mitigate SYN flood attacks without dropping legitimate connections. - ICMP Redirects:
net.ipv4.conf.all.accept_redirects = 0andnet.ipv6.conf.all.accept_redirects = 0- Disables the acceptance of ICMP redirects, which can be used in man-in-the-middle attacks. - Source Routed Packets:
net.ipv4.conf.all.accept_source_route = 0andnet.ipv6.conf.all.accept_source_route = 0- Disables the acceptance of source-routed packets, another potential vector for network attacks. - Apply Changes:
sudo sysctl -p
- IP Spoofing Vulnerability:
6. Log Management and Auditing
- Problem: Logs are crucial for incident response and detecting suspicious activity.
- Diagnosis: Check
/etc/rsyslog.conf(or/etc/syslog-ng/syslog-ng.conf),/var/log/contents, andauditdstatus. - Common Causes & Fixes:
- Insufficient Logging: Ensure critical services are logging to appropriate files.
- No Centralized Logging: For multiple servers, logs should be sent to a central, secure log server. Configure
rsyslogorsyslog-ngto forward logs to a remote host (e.g.,*.* @logserver.example.com). - Auditd Not Running/Configured:
auditdlogs detailed system events (file access, command execution, etc.).- Install:
sudo apt install auditdorsudo yum install auditd. - Enable:
sudo systemctl enable --now auditd. - Configure rules in
/etc/audit/rules.d/. A common rule is to watch sensitive directories:-w /etc/passwd -p wa -k identity(watch for write/attribute changes to passwd, tag with 'identity').
- Install:
- Log Rotation Issues: Ensure logs are rotated (
logrotateconfiguration in/etc/logrotate.d/) to prevent disks from filling up.
7. Software Updates and Patching
- Problem: Unpatched software is a common source of vulnerabilities.
- Diagnosis: Regularly check for available updates.
- Common Causes & Fixes:
- Manual Updates Only: Relying on manual checks is prone to oversight.
- Automated Security Updates: Configure unattended upgrades.
- Debian/Ubuntu:
sudo apt install unattended-upgrades && sudo dpkg-reconfigure --priority=low unattended-upgrades - RHEL/CentOS:
sudo yum install yum-cron && sudo systemctl enable --now yum-cron
- Debian/Ubuntu:
- Regular Manual Review: Even with automation, periodically run
sudo apt update && sudo apt list --upgradableorsudo yum check-updateto see what’s being applied and if manual intervention is needed.
The next error you’ll likely encounter is related to SELinux or AppArmor blocking legitimate operations due to strict security profiles.