Is there any way to add an exception for a port in the firewall setting, permanently?

Hello,

I want to add a port in the firewall exception list so that my application can be accessed over network even if firewall is disabled. I am using iptables command to add exception.

The problem is, after setting the rule if I change the firewall setting i.e. on/off then it is overwriting all the existing rules.

Let me give you two scenarios to help you understand the problem better:
Scenario 1) Firewall is On. I have added a rule in the iptables to allow an input access to a particular port. Now my (Web based) application can be accessed from other systems over the network. Now I disable the firewall. Still my app can be accessed over network but the rule is gone.

Scenario 2) Firewall is Off. I have added a rule in the iptables to allow an input access to a particular port. Now my (Web based) application can be accessed from other systems over the network. I enable the firewall. Now the app cannot be accessed over network because all the existing rules have been overwritten.

Is there any way to add an exception for a particular port in the firewall setting so that disabling or enabling the firewall (any number of times) won't affect the existing rule set.

Note: I can only use 'commands'. Doing changes using graphical interface won't be helpful in my case as I am using a script to install my app and configure the port settings.

Yes, you can save the settings. Have a look at the man page for iptables-save.

If you are on Fedora/Redhat/Centos you can save your configuration with

service iptables save

and reload that configuration with

service iptables reload

Other flavors of Linux have similar functionality but use different syntax.