This article provides essential steps to secure your Linux server by implementing best practices in authentication, network configuration, and system maintenance. By following these guidelines, you can significantly reduce the risk of unauthorized access and ensure that your server remains robust against common threats.
1. Secure SSH Access
1.1 Disable Password Authentication
To enhance security, disable password authentication for SSH and use key-based authentication instead.
- Generate an SSH key pair on your local machine if you don't already have one:
- Copy the public key to your server:
- Edit the SSH daemon configuration file:
- Set
PasswordAuthentication noand ensurePubkeyAuthentication yes. - Restart the SSH service to apply changes:
ssh-keygen -t rsa -b 4096
ssh-copy-id user@your_server_ip
nano /etc/ssh/sshd_config
sudo systemctl restart sshd
1.2 Change Default SSH Port
Changing the default SSH port can help reduce automated attacks.
- Edit the SSH daemon configuration file:
- Set a new port number, e.g.,
Port 2222. - Restart the SSH service to apply changes:
- Update your firewall rules to allow traffic on the new port.
nano /etc/ssh/sshd_config
sudo systemctl restart sshd
2. Configure UFW Firewall
The Uncomplicated Firewall (UFW) provides a simple way to manage firewall settings.
- Install UFW if it's not already installed:
- Allow SSH access on your chosen port (e.g., 2222):
- Enable UFW:
- Check the status of UFW to ensure it's active and configured correctly:
sudo apt-get install ufw
sudo ufw allow 2222/tcp
sudo ufw enable
sudo ufw status verbose
3. Install Fail2Ban
Fail2Ban scans log files for suspicious activity and blocks malicious IP addresses.
- Install Fail2Ban:
- Create a custom configuration file by copying the default jail configuration:
- Edit the custom configuration file to adjust settings as needed:
- Start and enable Fail2Ban:
sudo apt-get install fail2ban
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
nano /etc/fail2ban/jail.local
sudo systemctl start fail2ban
sudo systemctl enable fail2ban
4. Enable Automatic Security Updates
Keeping your system up to date is crucial for maintaining security.
- Install the unattended-upgrades package:
- Enable automatic updates by editing the configuration file:
- Add or modify the following lines to enable upgrades:
sudo apt-get install unattended-upgrades
nano /etc/apt/apt.conf.d/20auto-upgrades
Apt::Periodic::Update-Package-Lists "1";
Apt::Periodic::Unattended-Upgrade "1";
5. Remove Unused Services
Disabling unnecessary services reduces the attack surface of your server.
- List all running services:
- Identify and stop unused services. For example, to disable Apache:
sudo systemctl list-units --type=service
sudo systemctl stop apache2
sudo systemctl disable apache2
Troubleshooting
- SSH connection issues after changing the port: Ensure that your SSH client is configured to use the new port and that UFW allows traffic on this port.
- Fail2Ban not working as expected: Check the Fail2Ban logs for errors:
sudo tail -f /var/log/fail2ban.log - Automatic updates failing: Verify that your system is configured to use a reliable package repository and check the update logs:
cat /var/log/unattended-upgrades/unattended-upgrades.log