LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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
 
LinkBack Search this Thread
Old 11-24-2009, 11:24 AM   #1
_TeRmInEt_
LQ Newbie
 
Registered: Nov 2009
Posts: 6

Rep: Reputation: 0
Iptables and DNAT


Server A: x.x.x.x and it is eth0:1
Server B: y.y.y.y

Code:
-A PREROUTING -d x.x.x.x/32 -p tcp -m tcp --dport zzz -j DNAT --to-destination y.y.y.y:www
-A POSTROUTING -o eth0 -j MASQUERADE
Whit that rules I can connect to x.x.x.x:zzz and it reponse y.y.y.y:www

The problem is that x.x.x.x is exit with another ip (eth0)... I would like that x.x.x.x connect to y.y.y.y, is there a way?

Thank you
 
Old 11-24-2009, 11:32 AM   #2
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 115Reputation: 115
Quote:
Originally Posted by _TeRmInEt_ View Post
Server A: x.x.x.x and it is eth0:1
Server B: y.y.y.y

Code:
-A PREROUTING -d x.x.x.x/32 -p tcp -m tcp --dport zzz -j DNAT --to-destination y.y.y.y:www
-A POSTROUTING -o eth0 -j MASQUERADE
Whit that rules I can connect to x.x.x.x:zzz and it reponse y.y.y.y:www

The problem is that x.x.x.x is exit with another ip (eth0)... I would like that x.x.x.x connect to y.y.y.y, is there a way?

Thank you
yes.
assuming you have forwarding turned on -

turn it on in /etc/sysctl.conf (net.ipv4.ip_forward = 1) then run sysctl -p

something like

Code:
iptables  -I PREROUTING -t nat  -p tcp -d x.x.x.x/32 --dport zzz  -j DNAT --to x.x.x.x:www
would work
 
Old 11-24-2009, 11:55 AM   #3
_TeRmInEt_
LQ Newbie
 
Registered: Nov 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by centosboy View Post
yes.
assuming you have forwarding turned on -

turn it on in /etc/sysctl.conf (net.ipv4.ip_forward = 1) then run sysctl -p

something like

Code:
iptables  -I PREROUTING -t nat  -p tcp -d x.x.x.x/32 --dport zzz  -j DNAT --to x.x.x.x:www
would work
Uhm...

Code:
peng:/home/terminet# sysctl -p
net.ipv4.ip_forward = 1
Tried but doesn't work, it still connecting using eth0 ip
 
Old 11-24-2009, 12:16 PM   #4
Cybrax
LQ Newbie
 
Registered: Oct 2009
Posts: 11

Rep: Reputation: 0
Quote:
Originally Posted by _TeRmInEt_ View Post
Server A: x.x.x.x and it is eth0:1
Server B: y.y.y.y

Code:
-A PREROUTING -d x.x.x.x/32 -p tcp -m tcp --dport zzz -j DNAT --to-destination y.y.y.y:www
-A POSTROUTING -o eth0 -j MASQUERADE
Whit that rules I can connect to x.x.x.x:zzz and it reponse y.y.y.y:www

The problem is that x.x.x.x is exit with another ip (eth0)... I would like that x.x.x.x connect to y.y.y.y, is there a way?

Thank you
this seems to work for my script
Code:
iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 40568 -j DNAT --to-destination 192.168.0.10:40568
so it should be right what you are doing

i dont know what your scripts is like
but if you are making or also having a firewall
maybe thisline will help

Code:
iptables -A FORWARD -i eth0 -p tcp --dport 40568 -j ACCEPT

Last edited by Cybrax; 11-24-2009 at 12:27 PM.
 
Old 11-24-2009, 01:59 PM   #5
_TeRmInEt_
LQ Newbie
 
Registered: Nov 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Cybrax View Post
this seems to work for my script
Code:
iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 40568 -j DNAT --to-destination 192.168.0.10:40568
so it should be right what you are doing

i dont know what your scripts is like
but if you are making or also having a firewall
maybe thisline will help

Code:
iptables -A FORWARD -i eth0 -p tcp --dport 40568 -j ACCEPT
You're are right it works BUT no how I would like.

Under eth0, I've many public ips:

eth0 = x.x.x.x
eth0:1 = y.y.y.y
eth0:2 = z.z.z.z
eth0:3 = w.w.w.w

The point is that DNAT routing with x.x.x.x (look the logs), instead of y.y.y.y

This is the point.

Tnx
 
Old 11-24-2009, 02:31 PM   #6
Cybrax
LQ Newbie
 
Registered: Oct 2009
Posts: 11

Rep: Reputation: 0
Quote:
Originally Posted by _TeRmInEt_ View Post
You're are right it works BUT no how I would like.

Under eth0, I've many public ips:

eth0 = x.x.x.x
eth0:1 = y.y.y.y
eth0:2 = z.z.z.z
eth0:3 = w.w.w.w

The point is that DNAT routing with x.x.x.x (look the logs), instead of y.y.y.y

This is the point.

Tnx
Ok i see what you mean maybe the source flag -s y.y.y.y
but i duobt that

thats a bit out of my leage yet

edit

or how about try this

instead of eth0 eth0:1

Last edited by Cybrax; 11-24-2009 at 02:36 PM.
 
Old 11-24-2009, 02:49 PM   #7
_TeRmInEt_
LQ Newbie
 
Registered: Nov 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Cybrax View Post
Ok i see what you mean maybe the source flag -s y.y.y.y
but i duobt that

thats a bit out of my leage yet

edit

or how about try this

instead of eth0 eth0:1
With -s you filter the source (only source can connect to).

eth0:1 synatax isn't allowed by iptables
 
Old 11-24-2009, 03:00 PM   #8
Cybrax
LQ Newbie
 
Registered: Oct 2009
Posts: 11

Rep: Reputation: 0
Quote:
Originally Posted by _TeRmInEt_ View Post
With -s you filter the source (only source can connect to).

eth0:1 synatax isn't allowed by iptables
lol trying to put in my 2 cents but i learn more from you then vice versa :P

thats all i can do hope you find a solution
 
Old 11-24-2009, 04:10 PM   #9
_TeRmInEt_
LQ Newbie
 
Registered: Nov 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Np tnx
 
Old 11-24-2009, 06:56 PM   #10
_TeRmInEt_
LQ Newbie
 
Registered: Nov 2009
Posts: 6

Original Poster
Rep: Reputation: 0
The problem was the masquerate that takes the default gateway. It was enough to set a SNAT to resolve
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
iptables with dnat mhm Linux - Networking 3 12-31-2007 08:10 AM
Iptables DNAT ! Please help ! thomaspsimon Linux - Networking 18 08-27-2007 11:03 AM
iptables DNAT problem eantoranz Linux - Networking 2 09-12-2006 02:00 PM
iptables DNAT pshepperd Linux - Security 1 05-22-2004 04:56 PM
iptables DNAT bentz Linux - Networking 15 05-19-2003 02:17 PM


All times are GMT -5. The time now is 05:22 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration