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
:
- Check Firewall Status:
sudo systemctl status firewalld
- Install Firewalld (if not installed):
sudo yum install firewalld
- Start and Enable Firewalld:
sudo systemctl start firewalld
sudo systemctl enable firewalld
- Allow SSH (if necessary):
sudo firewall-cmd --permanent --add-service=ssh
- Reload Firewalld for Changes to Take Effect:
sudo firewall-cmd --reload
Using iptables
:
- Check if
iptables
is installed:
sudo yum install iptables-services
- Start and Enable
iptables
:
sudo systemctl start iptables
sudo systemctl enable iptables
- Allow SSH (if necessary):
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
- Save
iptables
Rules:
sudo service iptables save
- 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
:
- Check UFW Status:
sudo ufw status
- Install UFW (if not installed):
sudo apt-get update
sudo apt-get install ufw
- Enable UFW:
sudo ufw enable
- Allow SSH (if necessary):
sudo ufw allow 22
- 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.