Securing a Linux server is a continuous process that goes beyond just installation and basic setup. Linux servers, while powerful and flexible, are not immune to various types of cyber threats like brute-force login attempts, malware, misconfigurations, and other vulnerabilities. This comprehensive guide will walk you through essential security steps to harden your Linux server, with detailed explanations and practical examples for each step. Let’s secure your Linux server!
1. Disable Root Login
Why?
The root user has full administrative privileges, making it a primary target for attackers. Allowing direct root login via SSH increases the risk of brute-force attacks on a critical account. Disabling root login forces attackers to compromise an individual user account first, offering an extra layer of security.
How to Do It:
- Open the SSH configuration file:
sudo nano /etc/ssh/sshd_config
- Find the line:
PermitRootLogin yes
- Change it to:
PermitRootLogin no
- Save and close the file.
- Restart the SSH service to apply changes:
sudo systemctl restart sshd
2. Use Key-Based SSH Authentication
Why?
Password-based logins are vulnerable to brute-force attacks. SSH keys, however, are much more secure because they rely on cryptographic pairs, making them harder to compromise.
How to Do It:
- Generate an SSH key pair on your local machine:
ssh-keygen -t rsa -b 4096
- Copy the public key to your server:
ssh-copy-id username@server_ip
- To disable password authentication, open the SSH configuration file on the server:
sudo nano /etc/ssh/sshd_config
- Set
PasswordAuthentication
tono
:PasswordAuthentication no
- Restart SSH to apply the change:
sudo systemctl restart sshd
3. Enforce Strong Password Policies
Why?
Weak passwords are one of the most common attack vectors. Enforcing strong passwords helps prevent brute-force attacks and ensures better protection of user accounts.
How to Do It:
- Open the password quality configuration file:
sudo nano /etc/security/pwquality.conf
- Set policies for minimum length and complexity:
minlen = 12
minclass = 3minlen
requires a minimum of 12 characters.minclass
requires at least 3 of the following character types: uppercase, lowercase, digits, and special characters.
4. Keep the System Updated
Why?
Software updates frequently include security patches that address known vulnerabilities. Failing to apply updates leaves your server open to exploitation.
How to Do It:
- Update packages on Debian/Ubuntu:On CentOS/RHEL:
sudo apt update && sudo apt upgrade -y
sudo yum update -y
- To enable automatic updates (on Ubuntu), install
unattended-upgrades
:sudo apt install unattended-upgrades
5. Configure a Firewall
Why?
A firewall restricts incoming and outgoing traffic, allowing only authorized traffic and services. This limits the potential attack surface of your server.
How to Do It:
- Install and configure
ufw
(Uncomplicated Firewall) on Ubuntu:sudo apt install ufw
- Allow essential services like SSH, HTTP, and HTTPS:
sudo ufw allow 22 # Allow SSH
sudo ufw allow 80 # Allow HTTP
sudo ufw allow 443 # Allow HTTPS - Enable the firewall:
sudo ufw enable
6. Install and Configure Intrusion Detection (Fail2Ban)
Why?
Fail2Ban helps protect your server from brute-force attacks by blocking IP addresses after multiple failed login attempts.
How to Do It:
- Install Fail2Ban:
sudo apt install fail2ban
- Configure Fail2Ban by editing the jail configuration:
sudo nano /etc/fail2ban/jail.conf
- Enable SSH monitoring with the following settings:
[sshd]
enabled = true
maxretry = 5
bantime = 3600 # 1 hour - Restart Fail2Ban to apply changes:
sudo systemctl restart fail2ban
7. Disable Unnecessary Services
Why?
Running unnecessary services increases the attack surface of your server. Each active service could potentially be exploited, so disabling non-essential services improves security.
How to Do It:
- List all active services:
sudo systemctl list-unit-files --type=service --state=enabled
- Disable unnecessary services:
sudo systemctl disable service_name
8. Set Proper File Permissions
Why?
Incorrect file permissions on sensitive files like SSH keys or log files can lead to unauthorized access. Proper permissions restrict access to critical system files.
How to Do It:
- Set permissions on the SSH configuration file:
sudo chmod 600 /etc/ssh/sshd_config
- Set permissions on log files:
sudo chmod 640 /var/log/auth.log
9. Enable Logging and Monitoring
Why?
Logging records system activities, helping you detect unusual behavior and investigate potential security incidents.
How to Do It:
- Use
rsyslog
for basic logging or consider advanced monitoring solutions like ELK (Elasticsearch, Logstash, Kibana). - Ensure logs are being generated and stored in
/var/log
by checking log files such as/var/log/auth.log
.
10. Implement Auditing with auditd
Why?
Audit logs allow you to monitor and track critical system events, such as file modifications, to detect suspicious activities.
How to Do It:
- Install
auditd
for auditing:sudo apt install auditd
- Add rules to track important files, e.g., tracking
/etc/passwd
for changes:Add:sudo nano /etc/audit/audit.rules
-w /etc/passwd -p wa -k passwd_changes
- Restart the auditing service:
sudo systemctl restart auditd
11. Secure SSH Configuration
Why?
SSH is a common attack vector. Properly configuring SSH limits potential vulnerabilities and improves the overall security of your server.
How to Do It:
- Edit the SSH configuration file:
sudo nano /etc/ssh/sshd_config
- Adjust settings to:
- Change the default SSH port (optional but recommended):
Port 2222
- Disable password authentication:
PasswordAuthentication no
- Only allow SSH Protocol 2:
Protocol 2
- Change the default SSH port (optional but recommended):
- Restart SSH:
sudo systemctl restart sshd
12. Harden Kernel Parameters
Why?
Kernel hardening helps mitigate certain types of attacks, such as denial-of-service (DoS) or spoofing.
How to Do It:
- Open the kernel parameters file:
sudo nano /etc/sysctl.conf
- Add or modify these settings to enhance security:
net.ipv4.tcp_syncookies = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0 - Apply the changes:
sudo sysctl -p
13. Schedule Regular Backups
Why?
Regular backups are essential in case of cyberattacks, data loss, or system failure. Backups allow you to restore your data.
How to Do It:
- Use
rsync
for scheduled backups:rsync -av /important_data /backup_location
14. Set Resource Limits
Why?
Setting resource limits helps mitigate DoS attacks by limiting how much CPU, memory, and processes each user can consume.
How to Do It:
- Edit the limits configuration file:
sudo nano /etc/security/limits.conf
- Add limits like:
* soft nproc 4096
* hard nproc 8192
15. Use Security Scanning Tools
Why?
Security scanning tools help identify vulnerabilities and misconfigurations, enabling you to fix issues before attackers can exploit them.
How to Do It:
- Install a security scanning tool like
Lynis
:sudo apt install lynis
- Run a system audit:
sudo lynis audit system
By following these steps, you can significantly improve the security of your Linux server. Remember, security is an ongoing process, so always stay vigilant, keep your system updated, and regularly audit your server’s security posture.