About UFW
UFW (Uncomplicated Firewall) is a command-line program that enables you to quickly define firewall access rules for your server. You can grant or deny access to IP addresses and specific services (such as SSH and HTTP), block ping requests, and more. UFW is available in the Debian and Ubuntu Linux distributions. The following procedures demonstrate how to install UFW and set up some basic firewall rules. For additional information, please see the UFW documentation.Installing UFW
To install UFW on your server, follow these steps:- Log in to your server using SSH.
-
At the command prompt, type the following command:
-
To see the current UFW status, type the following command:
- After initial installation, UFW is disabled so you can configure access rules (and not lock yourself out if using an SSH connection). When you are ready to enable the firewall, type the ufw enable command.
Granting access
With just a few commands you can set up rules to grant access to your server.Granting access to IP addresses
To grant access to a specific IP address, type the following command. Replace 192.168.1.1 with the IP address you want to use:Allowing an entire subnet
To grant access to an entire subnet of IP addresses, type the following command. Replace 192.168.1.0/24 with the IP address and network mask you want to allow:Granting access to services
In addition to granting access to IP addresses, you can permit access to specific services on the server, such as SSH and HTTP. To see a list of available application services, type the following command:Make sure you include the quotation marks around the application name.
Denying access
With just a few commands you can set up rules to deny access to your server.Blocking access by IP address
To deny access from a specific IP address, type the following command. Replace 192.168.1.1 with the IP address you want to block:Blocking an entire subnet
To deny access from an entire subnet of IP addresses, type the following command. Replace 192.168.1.0/24 with the IP address and network mask you want to block:Disabling ping (ICMP) responses
To disable sending ping (ICMP) responses from the server, follow these steps:- Open the /etc/ufw/before.rules file in your preferred text editor.
-
Comment out the ICMP configuration lines by typing a # character at the start of each line as follows:
- Save your changes to the /etc/ufw/before.rules file, and then exit the text editor.
-
Type the following command to load the new configuration: