Get a VPS server with a 15% discount
Sale ends in
00Days
:
00Hours
:
00Minutes
:
00Seconds

Blocking Access to Private Networks on Ubuntu Using UFW

You may suddenly start receiving complaints from the data center about private network scanning even though you did not intend to do this. To prevent such outbound connections, you can block private networks with the ufw firewall.

Installing ufw

First, check whether ufw is installed on your server and install it if needed.

Installing ufw on the server
Installing the ufw utility.

Before enabling the firewall, open the required ports so you do not lose access to your services. In most cases, you should allow SSH, HTTP, and HTTPS.

↙ Click a line to copy it

After that, enable the firewall.

Enabling ufw
Enabling the ufw firewall.

You can check the current firewall status with the following command.

Checking ufw status
Current ufw status.

Blocking private networks

Now you can block private networks to prevent any outbound connections to them from your server.

The following ranges are treated as private or special-use networks:

  • 10.0.0.0/8
  • 172.16.0.0/12
  • 192.168.0.0/16
  • 100.64.0.0/10
  • 198.18.0.0/15
  • 169.254.0.0/16

To block these ranges, add the following rules.

↙ Click a line to copy it

After adding the rules, verify that they are present.

↙ Click a line to copy it
Checking blocking rules in ufw
Reviewing the added blocking rules.

If you try to access an address from a blocked range, the connection will fail. For example, you can test this with .

Failed access to a blocked network
Example of access to an address from a blocked range.

This completes the private network blocking setup.

Unblocking networks if required

If you later need to restore access to one of the blocked networks, first display the numbered list of current ufw rules.

Numbered list of ufw rules
Viewing ufw rules with numbers.

Then delete the required rule by its number. Replace with the actual number from the previous command output.

For example, you can remove rule 7.

Deleting a ufw rule by number
Removing the selected rule from ufw.

After removing the rule, access to the corresponding address will no longer be restricted.

Access check after removing the rule
Checking access after unblocking the network.

You now know how to block and unblock your server access to private networks with ufw.