How do I use ufw to open ports on ipv4 only?

Solution 1:

You just have to use the fuller syntax and specify an address (range).

For example, allow connections to TCP port 22 on all IPv4 addresses:

ufw allow proto tcp to 0.0.0.0/0 port 22

Solution 2:

I would edit the ufw config file itself to turn off IPv6:

sudo nano /etc/default/ufw

Change the line that says: IPV6=yes to IPV6=no then restart the ufw service. You can even run sudo ufw reload if the ufw instance is already enabled.

This worked for me to ensure that all the rules I add are only added to IPv4.

Thanks


Solution 3:

Maybe only by subnet:

sudo ufw allow proto tcp from 192.168.0.0/24 to any port 22

More info: https://help.ubuntu.com/lts/serverguide/firewall.html