This article describes how to install UFW (Uncomplicated Firewall) and set up some basic configuration rules.
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.
To install UFW on your server, follow these steps:
apt install ufw
To see the current UFW status, type the following command:
ufw status
With just a few commands you can set up rules to grant access to your server.
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:
ufw allow from 192.168.1.1
If you later decide you want to remove this rule, type the following command:
ufw delete allow from 192.168.1.1
You can also use line numbers in the delete command. To obtain a list of rules with their associated line numbers, type the following command:
ufw status numbered
For example, to delete the fourth rule, type the following command:
ufw delete 4
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:
ufw allow from 192.168.1.0/24
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:
ufw app list
To allow access to one of those services, type the following command. Replace application with the application name (for example, OpenSSH):
ufw allow "application"
For example, the following command enables access for secure (HTTPS) and non-secure (HTTP) connections on Apache:
ufw allow "Apache Full"
Remember that SSH connections on A2 Hosting servers use port 7822 by default. To allow access to port 7822, type the following command:
ufw allow 7822
With just a few commands you can set up rules to deny access to your server.
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:
ufw deny from 192.168.1.1
If you later decide you want to remove this rule, type the following command:
ufw delete deny from 192.168.1.1
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:
ufw deny from 192.168.1.0/24
To disable sending ping (ICMP) responses from the server, follow these steps:
# ok icmp codes for INPUT #-A ufw-before-input -p icmp --icmp-type destination-unreachable -j ACCEPT #-A ufw-before-input -p icmp --icmp-type time-exceeded -j ACCEPT #-A ufw-before-input -p icmp --icmp-type parameter-problem -j ACCEPT #-A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT # ok icmp code for FORWARD #-A ufw-before-forward -p icmp --icmp-type destination-unreachable -j ACCEPT #-A ufw-before-forward -p icmp --icmp-type time-exceeded -j ACCEPT #-A ufw-before-forward -p icmp --icmp-type parameter-problem -j ACCEPT #-A ufw-before-forward -p icmp --icmp-type echo-request -j ACCEPT
Type the following command to load the new configuration:
ufw reload
The server now ignores ping requests.
To view the documentation for UFW, please visit https://help.ubuntu.com/community/UFW.
Subscribe to receive weekly cutting edge tips, strategies, and news you need to grow your web business.
No charge. Unsubscribe anytime.
Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.
We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.