blocking ip range with ufw
You can block an entire IP range using CIDR (Classless Inter-Domain Routing) notation with ufw
. To block all addresses under 57.141.2.x
, you'd specify the network as 57.141.2.0/24
.1
Here is the command:
sudo ufw deny from 57.141.2.0/24
## 💡 How it Works
The /24
is CIDR notation that defines the size of the network block. Think of an IP address as being made of four 8-bit blocks, totaling 32 bits.
-
/24
means the first 24 bits (57.141.2
) are fixed. -
The remaining 8 bits (
32 - 24 = 8
) can be any value. -
This effectively blocks the entire range from
57.141.2.0
to57.141.2.255
.
You can also block larger or smaller ranges:
-
Block a larger range (e.g.,
57.141.0.0
to57.141.255.255
):Bashsudo ufw deny from 57.141.0.0/16
-
Block a specific port from that range:
Bashsudo ufw deny from 57.141.2.0/24 to any port 443
## 🛡️ Managing Your Rules
After adding a rule, it's a good idea to check your firewall status.
-
Check the status and see rule numbers:
Bashsudo ufw status numbered
-
Delete a rule by its number (if you make a mistake):
Bash# Replace '5' with the actual rule number from the command above sudo ufw delete 5
This semi worked but the order was wrong, so it was still allowing traffic. needed to delete the rune and recreate it.