Installing and configuring UFW (Uncomplicated Firewall)
Linux: Installing and Configuring UFW (Uncomplicated Firewall)
Section titled “Linux: Installing and Configuring UFW (Uncomplicated Firewall)”Learn how to install UFW and set up basic firewall rules on your server. Root-level access is required.
About UFW
Section titled “About UFW”UFW (Uncomplicated Firewall) is a command-line program that allows you to quickly define firewall rules for your server. You can:
- Allow or deny access to specific IP addresses or services (SSH, HTTP, etc.)
- Block ping (ICMP) requests
- Configure additional firewall rules as needed
UFW is available on Debian and Ubuntu distributions.
Installing UFW
Section titled “Installing UFW”- Log in to your server via SSH.
- Install UFW using:
apt install ufw- Check UFW status:
ufw statusAfter installation, UFW is disabled by default, allowing you to configure rules without locking yourself out.
To enable the firewall when ready:
ufw enableGranting Access
Section titled “Granting Access”Grant access to a specific IP
Section titled “Grant access to a specific IP”ufw allow from 192.168.1.1To remove the rule later:
ufw delete allow from 192.168.1.1Tip: Use line numbers for deletion:
ufw status numberedufw delete 4 # Deletes the fourth ruleGrant access to a subnet
Section titled “Grant access to a subnet”ufw allow from 192.168.1.0/24Grant access to services
Section titled “Grant access to services”List available services:
ufw app listAllow a service (replace application with the service name, e.g., OpenSSH):
ufw allow "application"Example – allow both HTTP and HTTPS for Apache:
ufw allow "Apache Full"Allow SSH (port 22 by default, change if your account uses a custom port such as 7822):
ufw allow 22Denying Access
Section titled “Denying Access”Block a specific IP
Section titled “Block a specific IP”ufw deny from 192.168.1.1Remove the rule later:
ufw delete deny from 192.168.1.1Block an entire subnet
Section titled “Block an entire subnet”ufw deny from 192.168.1.0/24Disabling Ping (ICMP) Responses
Section titled “Disabling Ping (ICMP) Responses”- Open the UFW rules file:
nano /etc/ufw/before.rules- Comment out ICMP configuration lines by adding
#at the beginning:
# 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- Save changes and exit the editor.
- Reload UFW to apply the changes:
ufw reloadThe server now ignores ping requests.