LinuxQuestions.org
Visit Jeremy's Blog.
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 11-04-2006, 08:44 AM   #1
seraph-seph
Member
 
Registered: Jul 2005
Location: /dev/ATX3
Distribution: Slackware 11(kernel 2.6.17.13), SuSE 10, Caldera, CentOS 4.3
Posts: 38

Rep: Reputation: 15
Prerouting ports with iptables on a firewall


Hi everyone.

I've been trying to redirect petitions made to certainly ports (ex. 80) to another linux boxes using one public ip address.

But it haven't work.
here's the code, where eth0 it's the interface connected to internet using a public ip, and eth1 it's the one connected to the LAN. The ip point address for all incoming data throught the port 80 will be 192.168.1.12 and it host an apache webserver

Code:
#!/bin/sh

iptables -F
iptables -X
iptables -Z
iptables -t nat -F

iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -t nat -P PREROUTING ACCEPT
iptables -t nat -P POSTROUTING ACCEPT

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to 192.168.1.12:80

iptables -A INPUT -i lo -j ACCEPT

iptables -A INPUT -s 192.168.1.0/24 -i eth1 -j ACCEPT

#Access to internet 
iptables -A FORWARD -s 192.168.1.0/24 -i eth1 -p tcp --dport 80 -j ACCEPT
iptables -A FORWARD -s 192.168.1.0/24 -i eth1 -p tcp --dport 443 -j ACCEPT

## Access to DNS 
iptables -A FORWARD -s 192.168.1.0/24 -i eth1 -p tcp --dport 53 -j ACCEPT
iptables -A FORWARD -s 192.168.1.0/24 -i eth1 -p udp --dport 53 -j ACCEPT

iptables -A FORWARD -s 192.168.1.0/24 -i eth1 -j DROP

iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE

echo 1 > /proc/sys/net/ipv4/ip_forward

iptables -A INPUT -s 0.0.0.0/0 -i eth0 -p tcp -dport 1:1024 -j DROP
iptables -A INPUT -s 0.0.0.0/0 -i eth0 -p udp -dport 1:1024 -j DROP
But I can't get it work .. when I type the public address on a web browser it shows the "Could not connect to remote server" message, I don't know what else to do ... for the record .. I type the ip address of the webserver and it shows the welcome website of the server, also I can go outside to internet using the same firewall.

Any idea ... kernel option missing or something?
 
Old 11-04-2006, 09:30 AM   #2
amitsharma_26
Member
 
Registered: Sep 2005
Location: New delhi
Distribution: RHEL 3.0/4.0
Posts: 777

Rep: Reputation: 31
Few things :
1. Please make sure that 192.168.1.12(web-server) should have your firewall-box lan-ip as its gateway or we got to write another POSTROUTING rule for this respect as well.

--
Quote:
Originally Posted by seraph-seph
But I can't get it work .. when I type the public address on a web browser it shows the "Could not connect to remote server" message, I don't know what else to do ... for the record .. I type the ip address of the webserver and it shows the welcome website of the server, also I can go outside to internet using the same firewall.
2. You got to use any public-proxy to check whether it is working or not; you cannot check it while sitting on your localbox. During this way; these packets got traverse through OUTPUT chain.

--
Quote:
Originally Posted by seraph-seph
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE
3. As you are having a static public ip; i would suggest you to use SNATing instead of MASQUERADING.

And you should also check iptables port forwarding basics : http://amitsharma.linuxbloggers.com/portforwarding.htm
 
Old 11-04-2006, 09:31 AM   #3
Brian1
LQ Guru
 
Registered: Jan 2003
Location: Seymour, Indiana
Distribution: Distribution: RHEL 5 with Pieces of this and that. Kernel 2.6.23.1, KDE 3.5.8 and KDE 4.0 beta, Plu
Posts: 5,700

Rep: Reputation: 65
Are you trying to access the webserver from within your lan network using the external IP? If so this is probably your issue. Check out this link.
http://iptables-tutorial.frozentux.n...tml#DNATTARGET

There are a few post related to it here using the search feature.

Things to check goto a port checking site like this to see if it open to the outside.
http://www.hackerwatch.org/probe/

Goto an outside site or have a friend or family member from the outside see what they see.

Brian
 
Old 11-04-2006, 12:51 PM   #4
seraph-seph
Member
 
Registered: Jul 2005
Location: /dev/ATX3
Distribution: Slackware 11(kernel 2.6.17.13), SuSE 10, Caldera, CentOS 4.3
Posts: 38

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by amitsharma_26
Few things :
1. Please make sure that 192.168.1.12(web-server) should have your firewall-box lan-ip as its gateway or we got to write another POSTROUTING rule for this respect as well.
Thanks .. I didn't know that I should set as gateway the LAN IP of the firewall .. The webserver is using another one so I'll use this code since it worked.

Code:
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to 192.168.1.12:80
iptables -t nat -A POSTROUTING -p tcp -d 192.168.1.12 --dport 80 -j SNAT --to-source ${PUBLIC_IP}
The other thing is that I must comment the line
Code:
iptables -A FORWARD -s 192.168.1.0/24 -i eth1 -j DROP
to get it work. It might be requesting something else...
Thanks a lot

Mario.

Last edited by seraph-seph; 11-04-2006 at 01:40 PM.
 
  


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
All UDP ports of my firewall are closed even without iptables rules, any clue? mfeoli Linux - Networking 2 01-05-2006 10:07 AM
Iptables+prerouting niranjan_mr Linux - Networking 1 04-19-2005 12:23 PM
Adding ports to firewall/iptables? acidblue Fedora 1 02-09-2005 10:08 PM
Iptables firewall: is it working? Showing open ports Gates1026 Linux - Security 5 12-20-2004 08:16 PM
iptables PREROUTING and blocking question bakuretsu Linux - Security 3 09-12-2002 10:49 AM

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

All times are GMT -5. The time now is 07:58 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