Mastering Linux Security: 100 Essential Tips for Distro Hardening

Linux distributions are renowned for their robustness and flexibility, but in an era of escalating cyber threats, securing your system is paramount. Whether you’re running Ubuntu, Fedora, Arch, or any other distro, implementing strong security practices can mitigate risks from malware, unauthorized access, and data breaches. This comprehensive article outlines 100 practical security tips, organized into thematic sections for clarity. These tips draw from best practices applicable across most Linux distros, with commands and tools that are widely available. We’ll include relevant diagrams and images to illustrate key concepts visually.

image 48

What is Ubuntu? Security Features and How It Enhances …

Section 1: System Updates and Patches (Tips 1-10)

Keeping your system up-to-date is the foundation of security. Vulnerabilities are constantly discovered, and patches address them promptly.

  1. Enable Automatic Updates: Configure your package manager to install security updates automatically. On Debian-based systems, use unattended-upgrades; on RPM-based, dnf-automatic.
  2. Regularly Check for Updates: Run sudo apt update && sudo apt upgrade (Debian/Ubuntu) or sudo dnf update (Fedora) daily to ensure all packages are current.
  3. Subscribe to Security Alerts: Join mailing lists like Ubuntu Security Notices or Fedora Security Advisories to stay informed about vulnerabilities.
  4. Use a Stable Distro Release: Opt for LTS (Long Term Support) versions like Ubuntu LTS for fewer updates but more stability and security focus.
  5. Kernel Updates: Always reboot after kernel updates to apply new security features; use uname -r to verify the running kernel.
  6. Remove Unused Packages: Periodically run sudo apt autoremove to eliminate obsolete packages that could harbor vulnerabilities.
  7. Enable Security Repositories: Ensure your sources.list includes security repos, e.g., deb http://security.ubuntu.com/ubuntu focal-security main.
  8. Use Snap or Flatpak for Isolated Apps: These provide sandboxing, reducing the impact of vulnerabilities in applications.
  9. Monitor CVE Databases: Use tools like cve-search to scan your system for known Common Vulnerabilities and Exposures.
  10. Backup Before Updates: Always create a system snapshot with tools like Timeshift to rollback if an update causes issues.
image 49

The World’s Major Technology Providers Converge to Improve the …

Section 2: User and Account Management (Tips 11-20)

Proper user management prevents privilege escalation and unauthorized access.

  1. Use Strong Passwords: Enforce complex passwords with passwd and tools like pwgen for generation.
  2. Disable Root Login: Set PermitRootLogin no in /etc/ssh/sshd_config to force sudo usage.
  3. Implement sudo: Add users to the sudo group with usermod -aG sudo username and use visudo for fine-grained control.
  4. Account Lockout Policies: Use pam_tally2 in PAM configuration to lock accounts after failed login attempts.
  5. Two-Factor Authentication (2FA): Enable Google Authenticator for SSH and sudo with PAM modules.
  6. Regularly Audit Users: Run getent passwd to list users and remove unnecessary accounts with userdel.
  7. Password Aging: Set password expiration with chage -M 90 username to force periodic changes.
  8. Key-Based SSH Authentication: Generate keys with ssh-keygen and disable password auth in SSH config.
  9. Limit sudo Access: Use /etc/sudoers to restrict commands, e.g., allowing only specific tools.
  10. Use Separate Accounts for Services: Create dedicated users for daemons like Apache with useradd -r -s /bin/false apache.

Section 3: Firewall and Network Security (Tips 21-30)

Firewalls and network controls block unwanted traffic.

  1. Enable UFW or Firewalld: On Ubuntu, sudo ufw enable; on Fedora, sudo firewall-cmd --set-default-zone=drop.
  2. Allow Only Necessary Ports: Use sudo ufw allow 22/tcp for SSH, denying all else by default.
  3. Rate Limiting: Configure UFW to limit connections with sudo ufw limit ssh.
  4. IPv6 Firewall Rules: Ensure rules apply to IPv6; disable IPv6 if unused via sysctl.
  5. Install Fail2Ban: Use Fail2Ban to ban IPs after failed logins; configure jails for SSH, etc.
  6. Network Segmentation: Use VLANs or namespaces for isolating services.
  7. Disable Unneeded Services: Run sudo systemctl disable avahi-daemon for unused daemons.
  8. Secure SSH: Set MaxAuthTries 3 and use fail2ban integration.
  9. Use VPN for Remote Access: Configure WireGuard or OpenVPN for encrypted tunnels.
  10. Monitor Network Traffic: Install iftop or nethogs to watch for anomalies.
image 51

Section 4: File System and Permissions (Tips 31-40)

Tight permissions prevent unauthorized file access.

  1. Set Strict Permissions: Use chmod 600 on sensitive files like ~/.ssh/id_rsa.
  2. Use umask: Set umask 027 in /etc/profile for secure default permissions.
  3. Audit File Permissions: Run find / -perm -002 to find world-writable files.
  4. Enable SELinux or AppArmor: On Fedora, setenforce 1; on Ubuntu, enable AppArmor profiles.
  5. Mount Options: Add noexec,nodev,nosuid to /etc/fstab for /tmp and /var.
  6. Chroot Jails: Use chroot for services like FTP to isolate them.
  7. ACLs for Fine Control: Use setfacl to set access control lists on files.
  8. Regular Backups: Use rsync or duplicity with encryption for data integrity.
  9. Immutable Files: Make critical files immutable with chattr +i /etc/passwd.
  10. Scan for SUID/SGID: Find and review with find / -perm -4000 -o -perm -2000.

Section 5: Encryption and Data Protection (Tips 41-50)

Encrypt data to protect against theft.

  1. Full Disk Encryption: Use LUKS during installation or cryptsetup post-install.
  2. Encrypt Home Directories: Enable ecryptfs for user homes.
  3. Secure Swap: Encrypt swap space with LUKS to prevent data leaks.
  4. Use HTTPS Everywhere: Configure services like Apache with Let’s Encrypt certificates.
  5. GPG for Files: Encrypt sensitive files with gpg -c file.txt.
  6. VPN Encryption: Ensure all remote traffic is encrypted.
  7. Secure Boot: Enable UEFI Secure Boot to prevent rootkits.
  8. Password Managers: Use tools like pass or KeePassXC for secure storage.
  9. Data at Rest: Use dm-crypt for external drives.
  10. Wipe Free Space: Use shred or wipe to securely delete files.
image 52

Section 6: Monitoring and Logging (Tips 51-60)

Detect issues early with monitoring.

  1. Enable Syslog: Configure rsyslog or systemd-journald for centralized logs.
  2. Log Rotation: Use logrotate to manage log sizes.
  3. Auditd for Auditing: Install and configure auditd to track system calls.
  4. Intrusion Detection: Use Snort or OSSEC for real-time alerts.
  5. Monitor Processes: Use top, htop, or ps regularly.
  6. Systemd Timers for Checks: Set up timers for security scans.
  7. Email Alerts: Configure tools to send emails on anomalies.
  8. Prometheus and Grafana: For advanced monitoring dashboards.
  9. Check for Rootkits: Use rkhunter or chkrootkit weekly.
  10. Log Analysis Tools: Employ logwatch for daily summaries.
image 53
Your guide to superior linux server monitoring | Checkmk

Section 7: Software and Package Management (Tips 61-70)

Secure your software sources.

  1. Verify Package Signatures: Always check GPG keys for repos.
  2. Use Official Repos: Avoid third-party PPAs unless trusted.
  3. AppArmor Profiles for Apps: Create profiles for untrusted software.
  4. Sandbox with Firejail: Run browsers in firejail.
  5. Minimal Installations: Start with a base system and add only needed packages.
  6. Static Code Analysis: Use cppcheck for custom code.
  7. Container Security: If using Docker, scan images with Trivy.
  8. Disable Autostart Services: Review with systemctl list-unit-files.
  9. Secure Pip and Gem: Use virtualenvs for Python packages.
  10. Regular Vulnerability Scans: Use lynis for system audits.

Section 8: Service Hardening (Tips 71-80)

Harden running services.

  1. Apache Security: Use mod_security and disable directory listing.
  2. Nginx Configuration: Set server_tokens off and use SSL.
  3. Database Security: For MySQL, remove anonymous users.
  4. FTP vs SFTP: Prefer SFTP over FTP.
  5. Email Server Hardening: Use Postfix with TLS.
  6. Disable Telnet: Use SSH instead.
  7. Secure Samba: Restrict shares and use passwords.
  8. Cron Job Security: Lock down /etc/cron.d permissions.
  9. Web Application Firewall: Integrate ModSecurity.
  10. Service Isolation: Use systemd’s PrivateTmp and ProtectHome.

Section 9: Kernel and System Hardening (Tips 81-90)

Strengthen the core system.

  1. Sysctl Hardening: Set net.ipv4.tcp_syncookies=1 for SYN flood protection.
  2. Disable Core Dumps: Set ulimit -c 0 to prevent info leaks.
  3. ASLR Enabling: Ensure Address Space Layout Randomization is on.
  4. Grsecurity/PaX: Compile kernel with these patches if possible.
  5. Module Blacklisting: Blacklist unused kernel modules in /etc/modprobe.d.
  6. Bootloader Password: Set GRUB password.
  7. Disable USB Autorun: Use udev rules.
  8. Harden /proc: Mount /proc with hidepid=2.
  9. Use Seccomp: For processes like Chrome.
  10. Kernel Parameters: Tune with sysctl for security.

Section 10: Miscellaneous and Advanced Tips (Tips 91-100)

Additional layers of protection.

  1. Antivirus Installation: Use ClamAV for scans.
  2. Firewall Logging: Enable logging in UFW.
  3. Secure Bootloader: Use signed kernels.
  4. Network Sniffing Detection: Use tools like promiscdetect.
  5. Passwordless Sudo for Specific Commands: Configure carefully.
  6. Regular Reboots: To apply updates.
  7. Honeypots: Deploy for detection.
  8. BIOS/UEFI Password: Set firmware passwords.
  9. Physical Security: Lock servers physically.
  10. Continuous Education: Stay updated via resources like OWASP or Linux security forums.

In conclusion, implementing these 100 tips will significantly enhance your Linux distro’s security posture. Start with the basics like updates and firewalls, then layer on advanced features. Remember, security is an ongoing process—regular audits are key.

image 54

Tags: ,