Learn how to set up and configure a basic firewall using UFW to protect your Linux VPS.
Introduction
Setting up a firewall is essential for securing your Linux VPS. This guide will show you how to use UFW (Uncomplicated Firewall) for basic firewall configuration to enhance the security of your server by controlling incoming and outgoing traffic.
Step 1: Install UFW
Installing UFW is the first step to configuring your firewall on a Linux VPS. UFW provides a user-friendly interface for managing iptables firewall rules.
Update Package Lists:
Before installing UFW, it’s a good practice to update the package lists to ensure you get the latest version of the software:
sudo apt update
This command updates the package lists for the repositories and ensures you have the latest information.
Install UFW:
Install UFW using the following command:
sudo apt install ufw -y
This command installs UFW and confirms the installation with the -y
flag, which automatically answers ‘yes’ to any prompts.
Step 2: Configure UFW
Configuring UFW involves setting up rules to allow or deny traffic to specific services on your VPS.
Allow SSH Connections:
To prevent being locked out of your VPS, allow SSH connections through the firewall:
sudo ufw allow ssh
This command ensures that you can still access your VPS remotely.
Allow Other Necessary Services:
Depending on your needs, you might also want to allow traffic for web services like HTTP and HTTPS:
sudo ufw allow http
sudo ufw allow https
These commands allow incoming traffic on ports 80 (HTTP) and 443 (HTTPS), which are essential for web servers.
Step 3: Enable UFW
Enabling UFW activates the firewall with the rules you have configured.
Enable the Firewall:
To enable UFW and start enforcing the rules, use the following command:
sudo ufw enable
You will be prompted to confirm the action. Type ‘y’ and press Enter.
Check UFW Status:
After enabling UFW, you can check its status to ensure it’s active and see the currently enforced rules:
sudo ufw status
This command displays the status of UFW and the list of rules currently in effect.
Conclusion
UFW is now configured and actively protecting your Linux VPS. You can adjust the rules as needed to secure additional services and fine-tune your firewall settings. By using UFW, you enhance your server’s security by controlling which services are accessible and protecting against unauthorized access.