LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 04-11-2009, 09:39 PM   #1
Red Squirrel
Senior Member
 
Registered: Dec 2003
Distribution: Mint 20.1 on workstation, Debian 11 on servers
Posts: 1,336

Rep: Reputation: 54
iptable-restore commands tutorial


Where can I find a fully detailed tutorial on all the commands and what they do for the iptable-restore command?

Rather then use iptables directly and blindly inject all sorts of rules into memory, I rather be editing an actual file so I can lay it out with comments etc, then just apply it which clears the rules and adds the new file. The commands unfortunatly are different, so all the iptables tutorials I find don't help me much.

Right now what I'm trying to do is a IP/Port forward but having trouble finding resources on how to do it.
 
Old 04-12-2009, 04:54 AM   #2
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Learning how to write iptables scripts can be really easy if you start with the method you know works best for you. It sounds like you want to get started with a pre-written script. If that's the case, there's tons of iptables scripts right here on LQ and all over the Web. The most respected tutorial AFAICT is Oskar Andreasson's. BTW, I'm moving this to Networking, as it's not a security issue (at least not directly).

Last edited by win32sux; 04-12-2009 at 04:57 AM.
 
Old 04-12-2009, 08:20 PM   #3
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
Quote:
Originally Posted by Red Squirrel View Post
Where can I find a fully detailed tutorial on all the commands and what they do for the iptable-restore command?
The post above details the best tutorial/manual for iptables, but you have asked for info on iptable-restore command. I don't know of a manual, per se, on this command, but all the info you could want on this particular command is contained in the man page (but, be aware that this is not an iptables or firewalling tutorial; it just concerns this one command).

You would also want to read the same info on iptables-save.

Quote:
Rather then use iptables directly and blindly inject all sorts of rules into memory, I rather be editing an actual file so I can lay it out with comments etc, then just apply it which clears the rules and adds the new file.
...which makes it difficult to know why you are asking iptables-restore, but that's up to you. The conventional way of doing what you are asking (not "blindly inject all sorts of rules into memory", by which I take it that you mean that you don't want to be dynamically adding rules once the firewalling system is running with, eg -A) is to write something in a scripting language (eg, BASH) that will build your desired set of rules. Look at http://www.linuxhomenetworking.com/w...Using_iptables or http://www.chinalinuxpub.com/doc/www...bles-intro.htm for an idea of how to build the shards of a script into the full system. (I think that these are both derived from the 'Linux Quick Fix notebook' by Harrison; in any case that is as clear a general exposition of how to assemble things as I have seen; the Andreasson/Frozentux 'tutorial' is excellent on describing the commands but really doesn't do much in telling you anything about how the general networking infrastructure and you firewall should mesh together, of the best way of building from the commands.)

You could do something other than build up your rules from a bash-shell script, but I don't see why you would; bash gives you all that ability to do math on network addresses that you might need and the selection or deselection of options.

You could argue for a different scripting language (say, python) but it would be an odd situation in which there is any real, definable, advantage except perhaps in readability. And, if you comment the bash script adequately, it is hard to see this advantage as decisive (but do bear in mind that this is a classic example of a situation in which you can't have over-adequate coments; it is difficult to underestimate the degree to which stuff that was to the front of your mind when writing the ruleset can have 'evaporated' by the time that you modify them six months later to make a trivial change...and bear in mind the old saw 'a trivial change is one than needs no testing before bringing down the entire system').

And if you are using a scripting language to build your ruleset, the only advantages that I see for the use of iptables-save and iptables-restore are if you want to preserve packet counts across reboots (I'm not saying that this is irrelevant in your situation, just that you haven't said anything to make it seem that this is something that you are thinking about doing...although the logic of this situation is then that you wouldn't be using iptables-restore, unless you need the packet counts).

Quote:
The commands unfortunatly are different, so all the iptables tutorials I find don't help me much.
Sorry, that didn't help me much to understand what you meant (the commands are all different from what? From each other? From some other language? Between the different examples that you have seen?).

Have a look at the example scripts, which won't be exactly what you want, but try to understand the principles. Then ask 'do I need to do this'? So, if, for you NTP isn't an issue, you can ignore that part, assuming that the policy blocks off the appropriate port as a default. Etc, etc, through the services that you may or may not be allowing through or blocking in your situation (dns, http/caching, https, nfs, samba come immediately to mind, but you may have other particular requirements and always remember that the detail is dependant on the physical network set up, so to just 'cut and paste without thought' is not appropriate).
 
Old 04-12-2009, 08:36 PM   #4
Red Squirrel
Senior Member
 
Registered: Dec 2003
Distribution: Mint 20.1 on workstation, Debian 11 on servers
Posts: 1,336

Original Poster
Rep: Reputation: 54
I decided to just switch my method and use a bash script instead. Will probably be easier to do in the long run especially if I want to call up external scripts/programs. I've been doing some testing now to ensure it works properly. the flush command just scares me as if I'm doing that remotely over SSH and for some reason the script does not fully execute it could lock me out. Guess I just have to be careful.
 
Old 04-13-2009, 01:31 AM   #5
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally Posted by Red Squirrel View Post
the flush command just scares me as if I'm doing that remotely over SSH and for some reason the script does not fully execute it could lock me out. Guess I just have to be careful.
One big potential problem with doing a flush (while working remotely) is that you forget you had your policy set to DROP, thereby locking you out. The simplest way I've found to deal with that is to use a "catch all" DROP rule at the end of the chain and leave the policy set to ACCEPT. IIRC this is (or at least used to be) the default approach used by Red Hat-based distros. You could also do stuff like issue a timed shutdown command whenever you are working with dangerous iptables commands, so as that if you lock yourself out the machine will reboot after X amount of time (and load with the default ruleset). Or you could make it so that the default ruleset is loaded, instead of the reboot. The point being, there's several things you can do which will help reduce the possibility of a lock out scenario.

Last edited by win32sux; 04-13-2009 at 01:34 AM.
 
Old 04-13-2009, 03:19 AM   #6
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
Quote:
Originally Posted by Red Squirrel View Post
...remotely over SSH...
OK, that would scare me too, particularly if the machine I was doing it to was located in a different time zone.

A thoughtful/scared/paranoid person could put some hooks into the generating script that relied on, say, you being able to ssh back in within, say, ten minutes to set a flag file after a change and if that didn't occur put back a known-safe, lowest common denominator, set of rules.

Whether you are that person I cannot say. Of course, you could get equally scared testing out such a system, unless you could test it locally...

This makes the whole system more complex and complexity isn't exactly the friend of security, so if you do it that way I would have to advise you to be careful.
 
Old 04-13-2009, 11:59 AM   #7
Red Squirrel
Senior Member
 
Registered: Dec 2003
Distribution: Mint 20.1 on workstation, Debian 11 on servers
Posts: 1,336

Original Poster
Rep: Reputation: 54
Been playing around and it seems good, as long as I make sure that my ssh port rule is ok. If one rule fails the script still runs as it's just a bash script.

Also I wish I had thought of this before, but all I really need to do is make a bash script like this:

sleep 30; service iptables stop

And call it up with &, then apply my changes. In fact I should just add that to my rules file and have a --production flag that makes it not call that up. That way it's set and forget.

Reason I thought of that is because I just recently locked myself out while checking something, and had to get the data center to open up the firewall.

I should be doing this on a test box anyway lol.
 
Old 04-13-2009, 12:28 PM   #8
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Yeah, lock outs are a pain. Been there, done that.

I recommend you have it fall back to a known good set of rules instead of stopping the firewall, though.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP-MySQL tutorial and Linux Networking tutorial needed Please dlux45 Linux - Software 6 04-19-2010 01:10 PM
what is an iptable? & how to seperate the network on the basis of iptable vinod.wagh Linux - Networking 1 09-11-2008 01:28 AM
LXer: Tutorial: An Easy Tutorial on IP Tables and Port Knocking LXer Syndicated Linux News 0 02-04-2008 04:20 PM
iptable tutorial? alaios Linux - Networking 1 12-09-2004 08:30 AM
A good ftp commands tutorial? NonSumPisces Linux - Networking 2 08-23-2004 05:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 04:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration