20

I'm trying to comment an existing ufw firewall rule, but I can't find the exact command

I can easily add a rule with comment like:

sudo ufw allow in on eth0 to any port 80 comment 'test'

But how do I comment an existing rule?

26

If you add exactly same rule, then the existing rule gets overwritten and comment is updated there.

E.g.:

recyber@linux:~$ sudo ufw allow from 10.0.0.0/24 to any port 1234 comment "Comment"

Rule updated
3
  • Tested under ubuntu 18.04, existing rule skipped even when comment was applied.
    – Chris
    Oct 3 '19 at 6:51
  • For the record, comment was added in version 0.35
    – Joril
    Dec 2 '19 at 21:24
  • Note that insert and prepend options cannot be used when updating a rule. ufw will complain with "Skipping inserting existing rule". Mar 26 '20 at 18:18
4

According to ufw man page, there is no way how to edit existing rules in-place.

Here are your options:

A) Delete, and then recreate your rule with comment

B) Use ufw app profiles to define a service, then use description section for your comment.

0

In my installation ufw 0.36 on Ubuntu 18.04 ufw does not update rules, you will need to delete and recreate a rule. As ufw denies everything inbound by default and you open ports as needed. I assume it also does this in other versions too so you may not have your own deny rules to consider but if you do, the numbering may be important to you so you can do the following.

An example with comment.

ufw status numbered
ufw delete <rule number>
ufw insert <old rule number> allow from <IP address> to any port <port or service (ssh smtp imap etc...> comment '<your comment>'
ufw reload
ufw status

This should place the rule back exactly where it was which might be important to your rule configurations for example if you have a blanket deny rule you will want your allow rule before it. If you do not need to do this just omit the insert from the commands and the replacement rule will be appended.

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.