13

Whenever I run a command like ufw allow 22, ufw automatically adds the firewall rules to both ipv4 and ipv6. If I want to only open a port on ipv4, is there a way to do so? Something like ufw allow 22 proto ipv4.

19

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
1
  • I don't think this actually is a complete answer. Please see mine below. Thanks Feb 13 '19 at 4:20
14

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

5
  • It works fantastic, and it even causes deletion of all the duplicate IPv6 rules immediately upon reload. Thank you
    – spaceman
    Feb 12 '20 at 12:35
  • @spaceman no problem, glad it worked out Feb 12 '20 at 19:57
  • Warning: All IPv6 rules will be deleted and not be restored if setting IPV6=yes again. Sep 24 '20 at 13:58
  • Big question, does it gonna skip v6 ports and let them free/open?
    – M. Rostami
    Feb 4 at 13:18
  • 1
    @M.Rostami no. It disables IPv6 completely and removes all current IPv6 rules. Feb 4 at 16:00
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.