How to Build Secure Linux Server?
Securing a Linux server goes beyond installation and setup. Every server is vulnerable to attacks, from brute-force login attempts to malware and misconfigurations. This guide offers essential steps to strengthen your Linux server’s security, complete with detailed steps and the reasons behind them. Let’s make your Linux server as resilient as possible! | |
Word From Our Sponsor | |
Kloudnative is committed to being a valuable resource for tech enthusiasts seeking the latest updates on cloud-native technologies. To support our work, you can visit the sponsored link below. So check our sponsors today!!! | |
Learn AI in 5 minutes a day | |
What’s the secret to staying ahead of the curve in the world of AI? Information. Luckily, you can join 800,000+ early adopters reading The Rundown AI — the free newsletter that makes you smarter on AI with just a 5-minute read per day. | |
1. Disable Root Login | |
Why? | |
The root user has unlimited access, which makes it a target for attackers. Disabling root login prevents attackers from attempting brute-force attacks directly on this powerful account. Instead, a user with limited permissions is used, reducing the risk. | |
How to Do It | |
Open the SSH configuration file: | |
| |
Find the line: | |
| |
Change it to: | |
| |
Save and close the file. | |
Restart the SSH service to apply changes: | |
| |
2. Use Key-Based SSH Authentication | |
Why? | |
Password-based logins can be weak points in server security. SSH key pairs are much harder to crack than passwords, adding a strong layer of security. | |
How to Do It | |
Generate an SSH key on your local machine: | |
| |
This creates a public-private key pair for secure login. | |
Copy your public key to the server: | |
| |
To disable password-based login, open | |
| |
Set | |
| |
3. Enforce Strong Password Policies | |
Why? | |
Strong password policies prevent weak, easily guessed passwords, reducing the likelihood of brute-force attacks. | |
How to Do It | |
Open the password policy configuration file: | |
| |
Set policies like minimum length and complexity: | |
| |
| |
4. Keep the System Updated | |
Why? | |
Updates contain patches for known vulnerabilities. Failing to update leaves your server exposed to known attacks. | |
How to Do It | |
Run the update command: | |
| |
Enable automatic updates (on Ubuntu): | |
| |
5. Configure a Firewall | |
Why? | |
A firewall limits access to specific services and blocks unauthorized traffic, reducing the risk of intrusion. | |
How to Do It | |
For Ubuntu: Install and configure | |
| |
| |
6. Install and Configure Intrusion Detection (Fail2Ban) | |
Why? | |
Fail2Ban protects your server from brute-force attacks by blocking IPs with too many failed login attempts. | |
How to Do It | |
Install Fail2Ban: | |
| |
Configure Fail2Ban by editing | |
| |
Enable SSH monitoring with: | |
| |
| |
7. Disable Unnecessary Services | |
Why? | |
Running fewer services means fewer potential entry points for attackers, improving overall security. | |
How to Do It | |
List all active services: | |
| |
Disable unneeded services: | |
| |
8. Set Proper File Permissions | |
Why? | |
Sensitive files like SSH and log files should have strict permissions to prevent unauthorized access or modification. | |
How to Do It | |
Restrict access to important files: | |
| |
9. Enable Logging and Monitoring | |
Why? | |
Logging provides a record of system events, helping you detect unusual activities and analyze incidents. | |
How to Do It | |
| |
10. Implement Auditing with | |
Why? | |
Auditing monitors critical files and actions, alerting you to unauthorized changes or suspicious activity. | |
How to Do It | |
Install and configure | |
| |
Add rules in | |
| |
Restart | |
| |
11. Secure SSH Configuration | |
Why? | |
Configuring SSH settings hardens your server against attacks by limiting login options. | |
How to Do It | |
| |
| |
Adjust settings: | |
| |
Restart SSH: | |
| |
12. Harden Kernel Parameters | |
Why? | |
Kernel hardening secures network settings and mitigates certain attacks by restricting network behaviors. | |
How to Do It | |
Open | |
| |
Apply changes: | |
| |
13. Schedule Regular Backups | |
Why? | |
Backups ensure data is recoverable in case of a cyberattack, accidental deletion, or system failure. | |
How to Do It | |
| |
| |
14. Set Resource Limits | |
Why? | |
Resource limits help prevent denial-of-service (DoS) attacks by limiting user resource consumption. | |
How to Do It | |
Edit | |
| |
Set limits: | |
| |
15. Use Security Scanning Tools | |
Why? | |
Security scanners identify misconfigurations and vulnerabilities, helping you fix issues before attackers exploit them. | |
How to Do It | |
Install Lynis: | |
| |
Run a system scan: | |
| |
16. Protect Against Malware | |
Why? | |
Linux can still be vulnerable to malware, especially in environments with internet access or file sharing. | |
How to Do It | |
Install ClamAV: | |
| |
Update and scan: | |
| |
17. Enable Multi-Factor Authentication (MFA) | |
Why? | |
MFA adds a second layer of verification, making it more difficult for attackers to gain access, even with a password. | |
How to Do It | |
Install Google Authenticator: | |
| |
Set up MFA: | |
| |
Enable MFA in PAM configuration: | |
| |
Add: | |
| |
18. Implement Network Segmentation | |
Why? | |
Network segmentation limits traffic between different parts of your infrastructure, reducing the impact if an attacker gains access. By isolating sensitive services on private subnets or VLANs, you limit exposure and protect data. | |
How to Do It | |
| |
Example: | |
| |
19. Restrict | |
Why? | |
Limiting sudo access minimizes the risk of privilege escalation. Only trusted users should have sudo privileges, as any commands they execute can affect the entire system. | |
How to Do It | |
Edit the sudoers file: | |
| |
Define specific permissions for each user or user group: | |
| |
Regularly audit the | |
20. Enforce | |
Why? | |
AppArmor and SELinux are mandatory access control systems that add fine-grained permissions, confining processes to a limited set of resources and actions. This limits the impact if a process is compromised. | |
How to Do It | |
For AppArmor (Ubuntu/Debian): | |
| |
| |
| |
For SELinux (CentOS/RHEL): | |
| |
| |
| |
| |
21. Use Port Knocking for SSH Access | |
Why? | |
Port knocking helps hide the SSH port by requiring a sequence of port “knocks” to open the SSH port, making it harder for attackers to detect your SSH service. | |
How to Do It | |
Install knockd on your server: | |
| |
Configure port knocking | |
| |
Start | |
| |
Now, only after knocking on ports 7000, 8000, and 9000 in that order will port 22 open for SSH. | |
22. Limit Open Ports to Reduce Attack Surface | |
Why? | |
Open ports represent entry points for potential attackers. Limiting them to necessary services reduces the risk of unauthorized access. | |
How to Do It | |
Use netstat or ss to view open ports: | |
| |
Close unnecessary ports by disabling or firewalling services: | |
| |
For example, if only SSH and HTTP/HTTPS are needed, ensure only ports 22, 80, and 443 are open. | |
23. Use File Integrity Monitoring (FIM) | |
Why? | |
File Integrity Monitoring (FIM) detects unauthorized changes to critical system files, helping identify potential compromises or malicious modifications. | |
How to Do It | |
Install an FIM tool like | |
| |
Initialize the AIDE database: | |
| |
Set up a cron job to run regular AIDE checks: | |
| |
Add: | |
| |
24. Implement Rate Limiting | |
Why? | |
Rate limiting protects against denial-of-service (DoS) attacks by limiting the number of requests or logins from a single IP address. | |
How to Do It | |
Use iptables to limit SSH connections: | |
| |
| |
Alternatively, configure rate limits with Fail2Ban by adjusting the | |
25. Encrypt Sensitive Data | |
Why? | |
Encryption protects data in case of a security breach by making it unreadable to unauthorized users. This applies to data stored on disk and transmitted over the network. | |
How to Do It | |
Data at Rest: Use encryption tools like ecryptfs or LUKS to encrypt sensitive files and partitions. | |
| |
Data in Transit: Ensure all data transfers use encrypted channels (e.g., HTTPS for web traffic, SFTP for file transfers). |
Comments
Post a Comment