Cloud Service >> Knowledgebase >> Linux >> How to Set Up a Firewall on a Linux Server
submit query

Cut Hosting Costs! Submit Query Today!

How to Set Up a Firewall on a Linux Server

Setting up a firewall is essential to safeguard your Linux server from unauthorized access, malicious attacks, and data breaches. Whether your server operates on a cloud platform or traditional hosting, a properly configured firewall acts as a critical line of defense in your security strategy.

This guide explains how to set up a firewall on a Linux server using common tools, ensuring your server remains protected while accommodating necessary traffic.

Why Firewalls Are Crucial for Linux Servers

Firewalls filter incoming and outgoing traffic based on predetermined security rules. They protect your server from threats by blocking unauthorized access while allowing legitimate communication. For cloud and hosting servers, this is particularly important due to their exposure to the internet.

Benefits of using a firewall include:

Preventing unauthorized access.

Blocking malicious traffic.

Enhancing overall server performance by reducing unwanted load.

Common Firewall Tools for Linux

Before diving into the setup process, familiarize yourself with popular firewall tools available for Linux:

UFW (Uncomplicated Firewall): A user-friendly tool, ideal for beginners.

iptables: A more advanced option, offering granular control over traffic.

firewalld: A dynamic firewall with zone-based configuration, often used on Red Hat-based distributions.

For servers in a cloud environment, hosting providers may also offer additional built-in firewall management tools.

Step-by-Step Guide to Set Up a Firewall

1. Install the Firewall Software

Most Linux distributions come with a firewall tool pre-installed. If it’s not installed, you can add it using the package manager:

For UFW:
sudo apt update

sudo apt install ufw

For firewalld:
sudo yum install firewalld

2. Enable and Start the Firewall

Activate the firewall service to ensure it runs when the server starts:

For UFW:
sudo ufw enable

For firewalld:
sudo systemctl start firewalld

sudo systemctl enable firewalld

3. Set Up Basic Rules

Firewalls operate by applying rules to control traffic. Common rules include:

Allow SSH Traffic: To maintain access to your server, allow SSH:

UFW:
sudo ufw allow ssh

firewalld:
sudo firewall-cmd --permanent --add-service=ssh

sudo firewall-cmd --reload

Allow Web Traffic: For hosting websites, allow HTTP and HTTPS traffic:

UFW:
sudo ufw allow http

sudo ufw allow https

firewalld:
sudo firewall-cmd --permanent --add-service=http

sudo firewall-cmd --permanent --add-service=https

sudo firewall-cmd --reload

Block Unwanted Traffic: Block all traffic by default and allow only specific services:

UFW:
sudo ufw default deny incoming

sudo ufw default allow outgoing

iptables:
sudo iptables -P INPUT DROP

sudo iptables -P FORWARD DROP

sudo iptables -P OUTPUT ACCEPT

4. Test Firewall Rules

To ensure the firewall is working correctly:

Check UFW status:
sudo ufw status

Check firewalld zones and rules:
sudo firewall-cmd --list-all

Testing rules ensures the firewall protects your Linux server without disrupting legitimate traffic, especially for cloud hosting applications.

5. Use Advanced Features

For more complex setups, consider advanced configurations:

Port Forwarding: Useful for redirecting traffic in cloud or shared hosting environments.

Custom Rules: Use iptables for specific traffic patterns:
sudo iptables -A INPUT -p tcp --dport 8080 -j ACCEPT

Securing Cloud and Hosting Servers with Firewalls

In cloud environments, hosting providers like AWS, Google Cloud, and Azure offer integrated firewalls. These allow you to define rules at the instance level:

Configure firewall rules directly in the provider's dashboard.

Combine the cloud firewall with on-server firewalls for layered security.

For traditional hosting, ensure your hosting control panel (e.g., cPanel or Plesk) has a firewall enabled to manage server-wide traffic.

Tips to Maintain Firewall Security

Regularly Update Rules: Adapt to evolving traffic patterns and security requirements.

Monitor Logs: Review firewall logs to detect unusual activity.
sudo tail -f /var/log/ufw.log

Backup Configuration: Save your firewall settings to restore them in case of accidental changes.

Conclusion

 

Setting up a firewall on a Linux server is a fundamental step toward enhancing security, whether you're managing a cloud-based server or a traditional hosting setup. By configuring rules, monitoring activity, and staying vigilant, you can significantly reduce the risk of unauthorized access and maintain a secure environment for your server's operations.

Cut Hosting Costs! Submit Query Today!

Grow With Us

Let’s talk about the future, and make it happen!