Tutorial: How to Enable Firewall in CentOS 7, AlmaLinux 8, Rocky Linux, and Ubuntu

Securing your server is essential, and one fundamental step in achieving this is by enabling a firewall. This tutorial will guide you through the process of enabling a firewall on various Linux distributions, including CentOS 7, AlmaLinux 8, Rocky Linux, and Ubuntu. We’ll cover the steps for both firewalld and iptables on CentOS and AlmaLinux, as well as ufw on Ubuntu. Ensure to adjust rules according to your specific server setup.

CentOS 7 and AlmaLinux 8:

Using firewalld:

  1. Check Firewall Status:
   sudo systemctl status firewalld
  1. Install Firewalld (if not installed):
   sudo yum install firewalld
  1. Start and Enable Firewalld:
   sudo systemctl start firewalld
   sudo systemctl enable firewalld
  1. Allow SSH (if necessary):
   sudo firewall-cmd --permanent --add-service=ssh
  1. Reload Firewalld for Changes to Take Effect:
   sudo firewall-cmd --reload

Using iptables:

  1. Check if iptables is installed:
   sudo yum install iptables-services
  1. Start and Enable iptables:
   sudo systemctl start iptables
   sudo systemctl enable iptables
  1. Allow SSH (if necessary):
   sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
  1. Save iptables Rules:
   sudo service iptables save
  1. Restart iptables for Changes to Take Effect:
   sudo systemctl restart iptables

Rocky Linux:

Rocky Linux is similar to CentOS, and you can follow the steps outlined above for CentOS 7 and AlmaLinux 8.

Ubuntu:

Using ufw:

  1. Check UFW Status:
   sudo ufw status
  1. Install UFW (if not installed):
   sudo apt-get update
   sudo apt-get install ufw
  1. Enable UFW:
   sudo ufw enable
  1. Allow SSH (if necessary):
   sudo ufw allow 22
  1. Reload UFW for Changes to Take Effect:
   sudo ufw reload

Important Notes:

  • Always make sure to allow SSH (port 22) or the specific port you are using for remote access.
  • Adjust other rules based on your specific application needs.
  • When configuring firewalls, be cautious to avoid blocking your own access to the server.

Remember that configuring firewalls requires careful consideration of your server’s specific requirements and services. Adjust the rules accordingly to meet the security needs of your system.

Categorized in:

Server Management,

Last Update: December 9, 2023