Problems activating your account? Send notification email to: [email protected]
Host4Fun Budget VPS Hosting
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Save iptables on Linux
#1
heelo .
i want to save iptables rules for ever in my linux server but when i restart server rules got removed and i should to put iptables commands again to set rules ... how can i save it for ever?
i searched the internet and many users had this problem, but they were not answered correctly.
is there any software that will automatically put the iptables command after restart?
i even wrote that command in crontab so that crontab would enter iptables commands for me after each restart, but he wouldn't do that, I don't know why. (i entered the commands correctly).i'm really confused, I don't know what to do. Please help me .
Thank you to Post4VPS and RackNerd for my awesome VPS 2!  

Reply
#2
I find it very difficult to believe that there was no useful information on this subject when you did a search on Google and similar search engines.

When it comes to iptables, I can think of 2 methods:
  • iptables-save & iptables-restore (manual method, can be automated through cronjob)
  • iptables-persistent (automated method)


iptables-save & iptables-restore

iptables-save
iptables-save > /etc/iptables/rules.v4 (Debian/Ubuntu)
iptables-save > /etc/sysconfig/iptables (RHEL/CentOS)
ip6tables-save > /etc/iptables/rules.v6 (IPv6 Debian/Ubuntu)
ip6tables-save > /etc/sysconfig/ip6tables (IPv6 RHEL/CentOS)

iptables-restore
iptables-restore > /etc/iptables/rules.v4 (Debian/Ubuntu)
iptables-restore > /etc/sysconfig/iptables (RHEL/CentOS)
ip6tables-restore > /etc/iptables/rules.v6 (IPv6 Debian/Ubuntu)
ip6tables-restore > /etc/sysconfig/ip6tables (IPv6 RHEL/CentOS)

To automate this you have to create a cronjob that runs the iptables-restore commands at boot/reboot to load the rules. Don't forget to always save your rules when you add new rules or modify existing rules.


iptables-persistent

iptables-persistent is a software package that you can install with your package manager. It also has a save command that will automatically save IPv4 and IPv6 rules. With this solution you don't require any cronjob to restore the rules. The software will do it for you automatically on boot/reboot.

References

For CentOS 7 and above you no longer really use iptables because RHEL 7 and above comes with firewalld.

To save rules in firewalld you simply add the permanent flag/option to your command.

Example:
firewall-cmd --zone=public --add-port=80/tcp --permanent

After that reload the rules to apply the rule as it is not instantly applied like with iptables:
firewall-cmd --reload

Of course you can actually disable firewalld and use iptables: https://serverfault.com/a/739465
Не стой как столб, не спи на посту,
Голосуй поднятой рукой вверх, бастуй.
Reply
#3
for me i use iptables-persistent package . on ubuntu i just use apt-get install iptables-persistent to install it
another alternative is to save the iptables each time you make a changes that works and make a startup script to import the saved iptables

but for easier use . use ufw instead . it is much easier than iptables and persistence .
Terminal
[email protected]:~$ Thanks To Shadow Hosting And Post4VPS for VPS 5
Reply
 


Possibly Related Threads...
Thread Author Replies Views Last Post
  The Smallest Linux Distros for Desktop Computers xdude 20 1,058 05-02-2020, 05:25 PM
Last Post: curious_dg
  Linux for a Windows Alternative - what are you using and how have you set it up? deanhills 11 584 02-26-2020, 07:14 AM
Last Post: humanpuff69
  Best Linux UI c0re 19 8,611 09-11-2017, 12:01 PM
Last Post: AmirGT
  Windows Game over Linux OS Vuluts 11 5,053 06-01-2017, 12:54 PM
Last Post: meetdilip
  Work software on Linux meetdilip 11 4,605 04-22-2017, 07:20 AM
Last Post: humanpuff69

Forum Jump:


Users browsing this thread: 1 Guest(s)

Sponsors: VirMach - Host4Fun - CubeData - Evolution-Host - HostDare - Hyper Expert - Shadow Hosting - Bladenode - Hostlease - RackNerd - ReadyDedis


About Post4VPS

Post4VPS is a forum/destiny where you can Delploy Your Free VPSs just by the Power of Posts.

We Provide VPSs of many locations like Germany,US,Canada,France,London,etc.

We also Provide VPSs of Both Linux and Windows OS.