IPTables and Port Forwarding?
$hy_guy asks: "I have
been totally striking out finding some info on how to do port forwarding in
Linux. I am currently running Mandrake 8.1 as my router and i would like
to forward a particular port to another machine on my LAN. I'm pretty
sure I have to use iptables but I have been very unsuccesful at the proper
syntax. I have scoured through Google and I have not really found any
useful info. I would appreciate just a link or something to point me the
correct direction. Thanks for the help" I know many of you may think this
is an FAQ, but it seems that IPTables confuses many people as this is not
the first time this question has hit the bin. If someone has a good general
reference on the use of IPTables, please share.
I use MonMotha's IPTables script to build my firewall. You tell it which ports to leave open and it closes the rest. It also has stuff in there for rate limiting and stuff, I think. According to that page, the beta does port forwarding.
rooooar
Netfilter is extremely well documented... this poster must not have tried very hard.
t er-faq.html
# HOWTO
Home page: http://www.netfilter.org/
FAQs: http://www.netfilter.org/documentation/FAQ/netfil
Excellent HOWTOs: http://www.netfilter.org/documentation/index.html
No-one EVER tries very hard.
"Ask Slashdot: Has anyone found my car keys?"
"Why did they cancel my favorite Sci-Fi show? I downloaded ALL the episodes!"
Why is that so hard?
gus
.. if only.
This one's a bit easy:
Step one: go to http://www.netfilter.org
step two: find the HOWTO section
step three: fifth line of the HTML version of the NAT-HOWTO reads like this: This document describes how to do masquerading, transparent proxying, port forwarding, and other forms of Network Address Translations with the 2.4 Linux Kernels.
step four:Wait, there's no step four... there's no step four!
Quentin
Heh... by coincidence, I just finished a project for the local hospital... I was coding a full-featured firewall based on Linux, and it had to integrate seamlessly with a WinNT network, including limiting 'net access by user name, and it had to work totally transparently for the users. Since a number of people in the hospital use Remotely Anywhere to connect from home, port forwarding became an issue for us.
The syntax for port forwarding is:
iptables -t nat -I PREROUTING -p <protocol> --dport <destination port> -j DNAT --to-destination <destination IP>:<destination port>
Note that you can remap port numbers, too, if need be (ie. traffic coming in on port 80 is redirected internally to port 5000).
Make sure you have the destination NAT target compiled in (I think it might be, by default), and make sure you enable all the NAT stuff you need.
iptables -t nat -A PREROUTING -i eth0 -p TCP --dport 80 -j DNAT --to 192.168.1.2
(To forward port 80 to 192.168.1.2 on LAN. eth0 is your external interface)
if you want to get into the kernel's routing abilities, check http://ds9a.nl/lartc/HOWTO//cvs/2.4routing/output/ 2.4routing.html. this site is _the_ place to go for info on the subject. But if you want to keep it simple, stay with the suggested netfilter sites.
Yeah it's not as obvious as first, but it's actually pretty simple.
OK here's an example: our gateway is 192.168.0.1 with lan interface eth0 and internet interface eth1. We want to redirect port 21 (FTP) to the machine 192.168.0.10
First of all, we need to add a rule matching incoming data to port 21. We use the PREROUTING chain in the NAT table:
iptables -t nat -A PREROUTING -i eth1 -p tcp --dport 21 -j DNAT --to-destination 192.168.0.10
This says: in the network address translation table and the chain that deals with incoming data prior to routing, and if the data is coming in from the internet and wants to go to TCP port 21 (ftp), DNAT (destination network address translate) it to transparently make it go to 192.168.0.10
Here's a generic template:
iptables -t nat -A PREROUTING -i [net interface] [selection rules - proto, port] -j DNAT --to-destination [ip on lan]
You can also redirect to a different port number, in the above example to redirect to 192.168.0.10 port 321 it would be:
--to-destination 192.168.0.10:321
As for this being an FAQ, I am aware of no such references on IPTables, and it doesn't matter. I think the manual page provides more than sufficient information to get you started. If you don't understand it, then you should not be administering a gateway of any kind!
I spent a while fooling with various IPTables scripts, but finnally settled on the gpl'd shorewall package.
It handles all my iptables configuration, including NAT with port forwarding.
___
The way to see by faith is to shut the eye of reason. --Ben Franklin
There was a good article in September 2001 issue of Linux Journal. Scripts are available at ftp.ssc.com/pub/lj/listings/issue89/
samba.org has the howto
why dont you guys post something new? something informative?
-- botsex is {grep;touch;strip;unzip;head;mount}
It's a totally object based graphical tool for building a firewall. You can just drag and drop "services" (ports) to create port mappings, drap and drop machines, other firewalls, networks, etc to determin who gets to do what.
Has a nice little druid in it to get you a working setup that you can modify to better suit your needs.
Really. Check it out.
NZ Electronics Enthusiasts: Check out my Trade Me Listings
Out of curiousity, (since I may be running into a similar scenario myself), how did you go about tying the access to the login?
:)
Thanks!
(Posting anonymously as I'm sure this'll get modded off-topic.
Besides standard iptables functions, you can easily patch your kernel and add extra features. /usr/src/linux. Then you can choose wich patches to apply. The ones I'm using are:
Just download iptables, uncompress it, and run 'make patch-o-matic', provided you have a source tree in
The NETMAP patch:
Author: Svenning Soerensen
Status: Experimental
This adds CONFIG_IP_NF_TARGET_NETMAP option, which provides a target for the nat table. It creates a static 1:1 mapping of the network address, while keeping host addresses intact. It can be applied to the PREROUTING chain to alter the destination of incoming connections, to the POSTROUTING chain to alter the source of outgoing connections, or both (with separate rules).
Examples:
iptables -t nat -A PREROUTING -d 1.2.3.0/24 -j NETMAP --to 5.6.7.0/24
iptables -t nat -A POSTROUTING -s 5.6.7.0/24 -j NETMAP --to 1.2.3.0/24
---
The TTL patch:
Author: Harald Welte
Status: Stable, needs new checksum handling
This adds CONFIG_IP_NF_TARGET_TTL option, which enables the user to set the TTL value of an IP packet or to increment / decrement it by a given value.
---
The iplimit patch:
Author: Gerd Knorr
Status: ItWorksForMe[tm]
This adds CONFIG_IP_NF_MATCH_IPLIMIT match allows you to restrict the number of parallel TCP connections to a server per client IP address (or address block).
Examples:
# allow 2 telnet connections per client host iptables -p tcp --syn --dport 23 -m iplimit --iplimit-above 2 -j REJECT
# you can also match the other way around: iptables -p tcp --syn --dport 23 -m iplimit ! --iplimit-above 2 -j ACCEPT
# limit the nr of parallel http requests to 16 per class C sized # network (24 bit netmask) iptables -p tcp --syn --dport 80 -m iplimit --iplimit-above 16 --iplimit-mask 24 -j REJECT
---
The random patch:
Author: Fabrice MARIE
Status: Works For Me.
This option adds CONFIG_IP_NF_MATCH_RANDOM, which allow you to match packets randomly following a given probability.
Suppported options are:
[--average] percent will match randomly packets with a probability of 'percent' default is 50%
---
The string patch:
Author: Emmanuel Roger
Status: Working, not with kernel 2.4.9
This patch adds CONFIG_IP_NF_MATCH_STRING which allows you to match a string in a whole packet.
---
and iptables 1.2.5 , wich I haven't compiled yet, so cannot tell for sure, has something that seems to be awesome... New quota match to have fixed IP quotas
Pleasee see my page with detailed instructions on how I did port forwarding on my Mandrake 8.1 box, which uses Bastille scripts to generate the Iptable rules.
Anyone tried using a transparent SOCKS proxy for this task? I know there is one in Debian, but does anyone know how well it works? I'd basically like to be able to get Netmeeting and P2P to work from behind my firewall when I'm trying to connect to someone else who's behind a firewall. Thanks,
David
All very nice, but what real-world capabilities does this give a user, besides bragging rights?
What there aren't enough of is plan old examples. Here is my configuration. xxx's replace personal info. :)
/sbin/iptables -t nat -F
/sbin/iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
/sbin/iptables -t nat -A PREROUTING -p tcp --dport 2021 -j DNAT --to 10.10.10.2:21
/sbin/iptables -t nat -A PREROUTING -p tcp --dport 2022 -j DNAT --to 10.10.10.2:22
/sbin/iptables -t nat -A PREROUTING -p tcp --dport 2080 -j DNAT --to 10.10.10.2:80
/sbin/iptables -t nat -A PREROUTING -p tcp --dport 33022 -j DNAT --to 10.10.10.33:22
/sbin/iptables -t nat -A PREROUTING -p tcp --dport 33021 -j DNAT --to 10.10.10.33:21
/sbin/iptables -t nat -A PREROUTING -p tcp --dport 25022 -j DNAT --to 10.10.10.25:22
/sbin/iptables -t nat -A PREROUTING -p tcp --dport 5800 -j DNAT --to 10.10.10.3:5800
/sbin/iptables -t nat -A PREROUTING -p tcp --dport 5900 -j DNAT --to 10.10.10.3:5900
/sbin/iptables -t nat -A PREROUTING -p tcp --dport 5801 -j DNAT --to 10.10.10.2:5801
/sbin/iptables -t nat -A PREROUTING -p tcp --dport 5901 -j DNAT --to 10.10.10.2:5901
iface eth1 inet static
address 209.195.xxx.xxx
netmask 255.255.255.224
gateway 209.195.xxx.xxx
iface eth0 inet static
address 10.10.10.1
netmask 255.255.255.0
network 10.10.10.0
broadcast 10.10.10.255
up
up
up
up
up
up
up
up
up
up
up
up
What most peeps need is a combined gateway+firewall solution, for their home lan and hook up a CM/xDSL for uplink to internet. Question is has anyone utilized the new STATEFULL features of ipchains to allow more apps/games etc to work in this kind of setup ?
gShield is a nice package that uses well-commented config files and scripts to setup an iptables firewall. Quote from page:
/sbin/ntsysv (or whatever SysVinit config tool you are using)will not be able to configure it into runlevels without modification. Personally, I am running it out of rc.local.
support for multiple NATs, configurable public service access, access control lists, routable protection, DMZ support, port-forwarding, MAC-specific filtering, configurable outgoing filtering, blacklists, support for transparent proxy, QoS marking of common transports and more.
I use it at work and at home. One caveat since you are using Mandrake: gShield.rc is not a SysVinit script, so
--
"Outlook not so good." That magic 8-ball knows everything! I'll ask about Exchange Server next.