Blog

穆哈文

Web Server Security Configuring Firewall (UFW firewalld) Fail2ban and SSL TLS on Ubuntu Webmin

Web Server Security: Configuring Firewall (UFW/firewalld), Fail2ban, and SSL/TLS on Ubuntu/Webmin

Managing your own web server, whether for personal projects or client hosting, comes with significant responsibility, especially regarding security. Servers exposed to the internet are vulnerable to various automated and manual attacks. Fortunately, there are several fundamental yet highly effective steps we can take to strengthen our Ubuntu server’s defenses.

This article will guide you through configuring three pillars of web server security: the Firewall (using UFW), an intrusion prevention system (Fail2ban), and connection encryption (SSL/TLS using Let’s Encrypt/Certbot). We will also briefly touch upon how some of these configurations can be managed through the Webmin panel.

1. Firewall: The First Line of Defense (UFW)

A firewall acts like a security guard at your server’s entrance. It controls incoming (ingress) and outgoing (egress) network traffic based on predefined rules. On Ubuntu, the Uncomplicated Firewall (UFW) is a user-friendly frontend for managing iptables firewall rules.

Why is a Firewall Important?

Without a firewall, all ports on your server are potentially open, allowing anyone to attempt connections to running services, including those that shouldn’t be publicly accessible (like databases or management ports).

UFW Configuration on Ubuntu (Command Line)

  1. Check UFW Status: Ensure UFW is installed (it usually comes pre-installed on modern Ubuntu). Check its status:

    sudo ufw status verbose

    If inactive, you’ll see Status: inactive.

  2. Set Default Rules: Best practice is to deny all incoming connections by default and allow all outgoing connections.

    sudo ufw default deny incoming
    sudo ufw default allow outgoing
  3. Allow Essential Connections: Before enabling UFW, it’s critical to allow necessary connections, especially SSH (port 22), so you don’t lock yourself out. Also allow HTTP (port 80) and HTTPS (port 443) if this is a web server.

    sudo ufw allow ssh       # Or sudo ufw allow 22/tcp
    sudo ufw allow http      # Or sudo ufw allow 80/tcp
    sudo ufw allow https     # Or sudo ufw allow 443/tcp

    You can add other ports your applications require (e.g., a database port if external access is needed, though generally not recommended).

  4. Enable UFW: Once essential rules are added, enable the firewall:

    sudo ufw enable

    Confirm with y when prompted.

  5. Check Rules: Review the active rules again:

    sudo ufw status numbered
  6. Delete Rules (If Necessary): If you mistakenly added a rule, use the number from the command above to delete it:

    sudo ufw delete <rule_number>

Firewall Configuration via Webmin

(Note: If your server runs an RHEL-based distro like CentOS or Fedora, you’d likely use firewalld instead. The commands differ, e.g., sudo firewall-cmd --permanent --add-service=http).

2. Fail2ban: Thwarting Brute-Force Attacks

Fail2ban is an intrusion prevention software framework that monitors server log files (e.g., SSH logs, web server logs) for suspicious patterns like repeated failed login attempts from the same IP address (a brute-force attack). If a pattern is detected, Fail2ban automatically blocks that IP address using the firewall for a specified duration.

Why is Fail2ban Important?

Brute-force attacks on services like SSH are extremely common. Fail2ban helps mitigate the risk of unauthorized access by stopping these attempts before they succeed.

Fail2ban Configuration on Ubuntu

  1. Installation:

    sudo apt update
    sudo apt install fail2ban

    The Fail2ban service should start automatically after installation.

  2. Basic Configuration (Local Jail): The main configuration is in /etc/fail2ban/jail.conf. However, never edit this file directly, as it will be overwritten during updates. Copy this file to jail.local to create your custom configuration:

    sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

    Now edit the jail.local file:

    sudo nano /etc/fail2ban/jail.local
  3. Adjust Settings in jail.local:

    • Find the [DEFAULT] section. Here you can set global parameters:
      • bantime: Duration for which an IP is banned (e.g., 1h for 1 hour, 1d for 1 day).
      • findtime: The window of time during which failed attempts are counted (e.g., 10m for 10 minutes).
      • maxretry: The number of failed attempts before an IP is banned.
    • Locate sections for specific services, e.g., [sshd]. Ensure enabled = true to activate protection for that service. SSH is usually enabled by default. You can enable other jails if needed (e.g., for Nginx or Apache if using authentication).
    • Save the file (Ctrl+X, then Y, then Enter in nano).
  4. Restart Fail2ban: Apply the configuration changes:

    sudo systemctl restart fail2ban
  5. Check Status: View the overall status and the status of specific jails (e.g., sshd):

    sudo fail2ban-client status
    sudo fail2ban-client status sshd

    This command will show the number of IPs currently banned by the sshd jail.

Fail2ban Configuration via Webmin

3. SSL/TLS: Securing Connections with HTTPS

SSL (Secure Sockets Layer) and its successor, TLS (Transport Layer Security), are cryptographic protocols that provide secure communication over computer networks. For web servers, this means enabling HTTPS (HTTP Secure).

Why is SSL/TLS (HTTPS) Important?

Obtaining and Installing SSL/TLS Certificates (Let’s Encrypt)

Let’s Encrypt is a free, automated, and open Certificate Authority (CA). The easiest way to use it on Ubuntu is with Certbot.

  1. Install Certbot: Install Certbot and the plugin for your web server (e.g., Nginx or Apache).

    # For Nginx
    sudo apt install certbot python3-certbot-nginx
    
    # For Apache
    # sudo apt install certbot python3-certbot-apache
  2. Obtain and Install Certificate: Run Certbot with the appropriate plugin. Certbot will detect the domain configurations in your web server and guide you through the process.

    # For Nginx
    sudo certbot --nginx
    
    # For Apache
    # sudo certbot --apache
    • You’ll be prompted to enter an email address (for renewal notices).
    • Agree to the Terms of Service.
    • Choose which domain(s) you want to enable HTTPS for.
    • Choose whether to redirect HTTP traffic to HTTPS (recommended).

    If successful, Certbot will configure your web server to use the SSL/TLS certificate and enable HTTPS.

  3. Verify Automatic Renewal: Certbot typically adds a systemd timer or cron job to automatically renew certificates before they expire. You can test the renewal process (without actually renewing if it’s not due yet) with:

    sudo certbot renew --dry-run

SSL/TLS Configuration via Webmin

Conclusion

Securing a web server is a layered process. By configuring a Firewall (UFW) to control network access, Fail2ban to fend off brute-force attacks, and SSL/TLS (HTTPS) to encrypt communications, you’ve built a strong defensive foundation for your Ubuntu server.

While Webmin can provide a convenient graphical interface for some of these tasks, understanding the command-line configuration gives you full control and deeper insight. Remember that security is not a one-time task; always keep your system updated, monitor logs, and review your security configurations periodically.