Console9

How to allow traffic from a specific IP address with UFW

Restrict a UFW firewall rule to accept connections only from a specific IP address or subnet.

Create a UFW rule that allows inbound traffic only from a specific IP address or CIDR subnet on Ubuntu.

Prerequisites

  • UFW enabled on the server
  • The IP address or subnet to allow

Step-by-Step: Allow Traffic from a Specific IP with UFW

1. Allow All Traffic from a Specific IP

sudo ufw allow from 203.0.113.50

2. Allow a Specific Port from a Specific IP

sudo ufw allow from 203.0.113.50 to any port 3306

UFW allows MySQL connections (port 3306) only from the specified IP address.

3. Allow a Subnet

sudo ufw allow from 10.0.0.0/8

How to Verify

sudo ufw status