Server Security Checklist

From Server rental store
Jump to navigation Jump to search

Server Security Checklist

A comprehensive security audit is crucial for any production server to protect sensitive data, maintain service availability, and prevent unauthorized access. This checklist provides a practical, hands-on approach to auditing and hardening your Linux server.

Prerequisites

Before beginning, ensure you have:

  • Root or sudo access to the server.
  • Basic understanding of Linux command line and networking concepts.
  • SSH access to the server.
  • A reliable backup of your server's data and configuration.
  • A list of all running services and their purpose.

1. Secure SSH Access

SSH (Secure Shell) is the primary way to manage your server remotely. Securing it is paramount.

  1. Disable Root Login:
  2. * Edit the SSH configuration file:
  3. sudo nano /etc/ssh/sshd_config
  4. * Find the line PermitRootLogin and change it to:
  5. PermitRootLogin no
  6. * Why: Direct root login bypasses user privilege escalation and is a common target for brute-force attacks.
  7. * Troubleshooting: If you accidentally lock yourself out, you may need console access or to reboot into single-user mode to revert the change.
  8. # Use SSH Keys Instead of Passwords:
  9. # * Generate an SSH key pair on your local machine:
  10. #
    ssh-keygen -t rsa -b 4096
  11. # * Copy your public key to the server:
  12. #
    ssh-copy-id your_username@your_server_ip
  13. # * Why: SSH keys are significantly more secure than passwords and are immune to brute-force attacks.
  14. # * Troubleshooting: Ensure file permissions on ~/.ssh/authorized_keys on the server are correct (usually 600).
  15. #
  16. # # Change the Default SSH Port:
  17. # # * In /etc/ssh/sshd_config, change Port 22 to a non-standard port (e.g., Port 2222).
  18. # # * Why: While not a foolproof security measure, it significantly reduces automated scanning and brute-force attempts targeting the default port.
  19. # # * Troubleshooting: Remember to specify the new port when connecting: ssh -p 2222 your_username@your_server_ip.
  20. # #
  21. # # # Limit SSH User Access:
  22. # # # * In /etc/ssh/sshd_config, use AllowUsers or DenyUsers directives.
  23. # # #
    AllowUsers user1 user2
  24. # # # * Why: Restricts who can log in via SSH, minimizing the attack surface.
  25. # # #
  26. # # # # Restart SSH Service:
  27. # # # #
    sudo systemctl restart sshd
  28. # # # # * Why: Applies the configuration changes.
  29. #
  30. # == 2. Firewall Configuration ==
  31. #
  32. # A firewall is your server's first line of defense against unwanted network traffic.
  33. #
  34. # # Install and Configure UFW (Uncomplicated Firewall):
  35. # # * Install UFW if not already present:
  36. # #
    sudo apt update && sudo apt install ufw
  37. # # * Why: UFW simplifies firewall management.
  38. # #
  39. # # # Set Default Policies:
  40. # # #
    sudo ufw default deny incoming
  41. # # #
    sudo ufw default allow outgoing
  42. # # # * Why: Denies all incoming connections by default, allowing only explicitly permitted services.
  43. # # #
  44. # # # # Allow Necessary Services:
  45. # # # # * Allow SSH (using your custom port if changed):
  46. # # # #
    sudo ufw allow 2222/tcp
  47. # # # # * Allow HTTP/HTTPS:
  48. # # # #
    sudo ufw allow 80/tcp
  49. # # # #
    sudo ufw allow 443/tcp
  50. # # # * Why: Opens specific ports for essential services.
  51. # # # * Troubleshooting: If you lose SSH access, you'll need console access to re-enable port 2222.
  52. # # #
  53. # # # # Enable the Firewall:
  54. # # # #
    sudo ufw enable
  55. # # # # * Why: Activates the firewall rules.
  56. # # # #
  57. # # # # # Check Firewall Status:
  58. # # # # #
    sudo ufw status verbose
  59. # # # # # * Why: Verifies that the rules are active and correctly configured.
  60. # # # #
  61. # # # # == 3. Regular System Updates ==
  62. # # #
  63. # # # Unpatched vulnerabilities are a major security risk.
  64. # # #
  65. # # # # Update Package Lists and Upgrade Packages:
  66. # # # #
    sudo apt update && sudo apt upgrade -y
  67. # # # # * Why: Ensures all installed software is up-to-date with the latest security patches. The -y flag automatically answers yes to prompts.
  68. # # # * Troubleshooting: Sometimes upgrades require a reboot. Monitor for prompts or messages indicating this.
  69. # # #
  70. # # # # Enable Automatic Security Updates (Optional but Recommended):
  71. # # # # * Install the unattended-upgrades package:
  72. # # # #
    sudo apt install unattended-upgrades
  73. # # # # * Configure it (e.g., /etc/apt/apt.conf.d/50unattended-upgrades).
  74. # # # # * Why: Automates the patching process for critical security updates.
  75. # # # * Security Implication: While convenient, ensure you have rollback plans in case an automatic update causes issues.
  76. # # #
  77. # # # == 4. Minimize Installed Software ==
  78. # #
  79. # # Every installed package is a potential entry point for attackers.
  80. # #
  81. # # # List Installed Packages:
  82. # # #
    dpkg -l | less
    (Debian/Ubuntu)
  83. # # #
    rpm -qa | less
    (CentOS/RHEL)
  84. # # # * Why: Helps you identify unnecessary software.
  85. # # #
  86. # # # # Remove Unused Packages:
  87. # # # #
    sudo apt autoremove --purge
    (Debian/Ubuntu)
  88. # # # #
    sudo yum autoremove
    (CentOS/RHEL)
  89. # # # * Why: Removes packages that were installed as dependencies but are no longer needed.
  90. # # #
  91. # # # # Uninstall Unnecessary Services:
  92. # # # # * Identify running services:
  93. # # # #
    sudo systemctl list-units --type=service --state=running
  94. # # # # * Stop and disable a service if not needed:
  95. # # # #
    sudo systemctl stop apache2 && sudo systemctl disable apache2
  96. # # # * Why: Reduces the attack surface by disabling or removing services you don't actively use.
  97. # #
  98. # # == 5. Intrusion Detection & Monitoring ==
  99. #
  100. # Proactive monitoring can help detect and respond to security incidents quickly.
  101. #
  102. # # Install and Configure Fail2ban:
  103. # # * Install Fail2ban:
  104. # #
    sudo apt install fail2ban
  105. # # * Configure it by copying the default jail.conf to jail.local:
  106. # #
    sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
  107. # # * Edit jail.local to customize settings, e.g., SSH port, ban time, and email notifications.
  108. # # * Why: Scans log files for malicious activity (like repeated failed login attempts) and temporarily or permanently bans the offending IP addresses.
  109. # # * Troubleshooting: Ensure Fail2ban is enabled and running: sudo systemctl status fail2ban. Check its logs: /var/log/fail2ban.log.
  110. # #
  111. # # # Monitor System Logs:
  112. # # # * Regularly review logs in /var/log/, especially:
  113. # # # * /var/log/auth.log (or secure on RHEL-based systems) for authentication attempts.
  114. # # # * /var/log/syslog (or messages) for general system activity.
  115. # # # * Application-specific logs (e.g., web server logs).
  116. # # * Why: Log analysis is crucial for identifying suspicious patterns and security breaches.
  117. # # * Tools: Consider tools like logwatch or centralized logging solutions (e.g., ELK stack, Graylog) for larger deployments.
  118. # #
  119. # # # Implement File Integrity Monitoring (Optional):
  120. # # # * Install AIDE (Advanced Intrusion Detection Environment):
  121. # # #
    sudo apt install aide aide-common
  122. # # # * Initialize the database:
  123. # # #
    sudo aideinit
  124. # # # * Run a check:
  125. # # #
    sudo aide.wrapper --check
  126. # # * Why: Detects unauthorized modifications to critical system files.
  127. # # * Security Implication: The AIDE database itself must be protected. Store a copy offline if possible.
  128. #
  129. # == 6. User and Permission Management ==
  130. #
  131. # Least privilege principle is key.
  132. #
  133. # # Review User Accounts:
  134. # #
    cat /etc/passwd
  135. # # * Why: Identify any dormant or unauthorized user accounts.
  136. # #
  137. # # # Remove Unused User Accounts:
  138. # # #
    sudo userdel -r username
  139. # # * Why: Removes accounts that are no longer needed, reducing potential entry points.
  140. # #
  141. # # # Use Strong Passwords and Enforce Policies:
  142. # # # * Ensure users have strong, unique passwords. Consider password complexity requirements and expiration policies using PAM (Pluggable Authentication Modules).
  143. # # * Why: Weak passwords are easily compromised.
  144. # #
  145. # # # Regularly Review File Permissions:
  146. # # # * Use find to locate world-writable files or directories:
  147. # # #
    sudo find / -xdev -type d -perm -0002 -print
  148. # # #
    sudo find / -xdev -type f -perm -0002 -print
  149. # # * Why: Prevents unauthorized modification of sensitive files.
  150. #
  151. # == 7. Security Hardening for Web Servers (Example: Nginx) ==
  152. #
  153. # If your server hosts web applications, hardening the web server is critical.
  154. #
  155. # # Keep Web Server Software Updated:
  156. # # * Follow the instructions in Section 3.
  157. # #
  158. # # # Configure SSL/TLS:
  159. # # # * Obtain and install an SSL certificate (e.g., Let's Encrypt).
  160. # # * Why: Encrypts traffic between the client and server, protecting data in transit.
  161. # # * See also: Let's Encrypt
  162. # #
  163. # # # Disable Unnecessary Modules:
  164. # # # * Consult your web server's documentation to disable modules that are not in use.
  165. # # * Why: Reduces the attack surface.
  166. # #
  167. # # # Implement Security Headers:
  168. # # # * Configure headers like Strict-Transport-Security, X-Frame-Options, X-Content-Type-Options, and Content-Security-Policy in your web server configuration.
  169. # # * Why: Mitigates various web vulnerabilities like clickjacking, XSS, and man-in-the-middle attacks.
  170. # #
  171. # # # Limit Request Methods and Body Sizes:
  172. # # # * Configure your web server to only allow necessary HTTP methods (e.g., GET, POST) and limit the size of request bodies to prevent certain types of denial-of-service attacks.
  173. # #
  174. # # == 8. Regular Audits and Testing ==
  175. #
  176. # Security is an ongoing process.
  177. #
  178. # # Perform Regular Vulnerability Scans:
  179. # # * Use tools like OpenVAS, Nessus, or Nmap scripts to scan your server for known vulnerabilities.
  180. # # * Why: Identifies potential weaknesses before attackers do.
  181. # #
  182. # # # Review Security Logs Periodically:
  183. # # * Dedicate time to manually review critical security logs.
  184. # # * Why: Helps catch subtle or ongoing threats that automated tools might miss.
  185. # #
  186. # # # Test Your Backups:
  187. # # * Regularly attempt to restore data from your backups to ensure they are valid and complete.
  188. # # * Why: A backup is useless if it cannot be restored.
  189. #
  190. # == Dedicated Servers for Enhanced Security ==
  191. #
  192. # For critical applications requiring maximum control and security, consider dedicated servers. Dedicated servers, such as those offered at PowerVPS, provide full root access, allowing you to implement these security measures with complete freedom and without the overhead of shared environments. This level of control is essential for implementing advanced security configurations and ensuring compliance with stringent security policies.
  193. #
  194. # == Conclusion ==
  195. #
  196. # Implementing this checklist is a significant step towards a more secure server environment. Remember that security is not a one-time task but an ongoing commitment. Regularly review and update your security practices as threats evolve.
  197. #
  198. #
  199. #
  200. #