A firewall is a tool for monitoring and filtering incoming and outgoing network traffic. It defines a set of security rules that allow or block certain traffic. UFW is a user-friendly view for managing iptables firewall rules. Its main purpose, as its name implies, is to make firewall management easier. In this article, we are going to teach you How to Limit SSH with UFW Firewall on CentOS. You can visit the packages available in Eldernode if you wish to purchase a CentOS VPS server.
Table of Contents
How to Limit SSH with UFW Firewall on CentOS [7, 8]
How to Setup UFW Firewall on CentOS
To fully learn this tutorial, we need to teach you the Setup UFW Firewall On CentOS first in this section. To do this, just follow the steps below.
In the first step, it is necessary to make sure you have activated REPO EPEL. So need to install UFW with the following command:
sudo yum -y install ufw
Now you can view the UFW installation status using the following command:
sudo ufw status
Limit SSH with UFW Firewall on CentOS [7, 8]
In this section, we are going to show you How to Limit SSH with UFW Firewall on CentOS. To do this, pay attention to the continuation of this tutorial. Now the point to note is that a basic firewall denies all input traffic and allowing output traffic, a good place to get started. You can easily adjust the default rules using the following commands:
sudo ufw default deny incoming
sudo ufw default allow outgoing
You can open any services you want on the Internet if you want. For example, you can open the firewall like the following commands:
sudo ufw allow ssh
or
sudo ufw allow https
As you see in the following command, for non-standard or unusual ports you can specify the port number instead of the service:
sudo ufw allow 2222
You can further narrow this down to protocol too by adding it to the end using the following command:
sudo ufw allow 2222/tcp
You can add a range of ports for FTP using the following command:
sudo ufw allow 3000:4000/tcp
You may have a fixed IP and want to allow all network traffic. In this case, you should use the following command:
sudo ufw allow from 123.123.123.123
You can also block traffic via IP by running the following command:
sudo ufw deny from 123.123.123.123
You can adjust the firewall with the help of the following command:
sudo systemctl enable ufw
Finally, you can issue the following command to disable the firewall:
sudo ufw disable
Conclusion
By default, the UFW firewall blocks all incoming and outgoing connections and allows all outgoing connections. This means that anyone who wants to access your server will not be able to connect unless you specifically open the port. In this article, we tried to teach you How to Limit SSH with UFW Firewall on CentOS. You can refer to the article Tutorial Limit SSH with UFW on Ubuntu 20.04 if you wish.