How to add comments to iptables rules on Linux

To add comments to iptables rules on Linux, you can use the -m comment option. For example, to add a comment “Allow SSH” to an iptables rule that allows incoming traffic on port 22 (the default port for SSH), you can use the following command:

iptables -A INPUT -p tcp --dport 22 -m comment --comment "Allow SSH" -j ACCEPT

You can also use -j option with COMMENT to add comments to iptables rules. For example,

iptables -A INPUT -p tcp --dport 22 -j COMMENT --comment "Allow SSH"

You can also use -j option with LOG to log the rule and add comments in the log message. For example,

iptables -A INPUT -p tcp --dport 22 -j LOG --log-prefix "SSH: "

You can view the comments by using the iptables -L -v -n command.

Leave a Comment