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-24-2011, 12:44 AM   #1
dbudbu
LQ Newbie
 
Registered: Nov 2011
Posts: 7

Rep: Reputation: Disabled
basic port forward help


As an exercise, trying to make a trivial port forward work. Eg. forward requests to 12345 to standard ssh. Starting as small a possible...

# iptables -t nat -A PREROUTING -p tcp --dport 12345 -j DNAT --to 22
# telnet localhost 12345
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused

# telnet localhost 22
Trying 127.0.0.1...
Connected to localhost.
^]

Forwarding on, all policies wide open, accept. Can someone throw me a bone?
 
Old 11-24-2011, 01:09 AM   #2
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 193Reputation: 193
Code:
 -j DNAT --to-port 22
 
Old 11-24-2011, 01:17 AM   #3
dbudbu
LQ Newbie
 
Registered: Nov 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thanks, but I had typo; I tried

--to 127.0.0.1:22
--to-port 22
--to-destination 127.0.0.1:22

Same result.
 
Old 11-24-2011, 01:18 AM   #4
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 193Reputation: 193
Hows about?

Code:
 -j REDIRECT --to-port 22

Last edited by fukawi1; 11-24-2011 at 01:27 AM.
 
Old 11-24-2011, 01:25 AM   #5
dbudbu
LQ Newbie
 
Registered: Nov 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
So.. you would have me say:

Code:
 # iptables -t nat -A PREROUTING -p tcp --dport 22 -j DNAT --to-destination 127.0.0.1:22
.. so I'd be forwarding _from_ *:22 -> localhost:22. But I want to forward from 12345->22.
 
Old 11-24-2011, 01:28 AM   #6
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 193Reputation: 193
Yeah, i brain farted, I just edited that post...
Dont mind me, long day
 
Old 11-24-2011, 08:46 AM   #7
dbudbu
LQ Newbie
 
Registered: Nov 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
only from outside.

I cannot claim to understand it, not even entirely believe it, but the failure seems to be limited to attempts to navigate the iptables DNAT/SNAT rules on a host from within that host.

So if I have an interface with address 192.168.1.100 and make a rule to send hiport traffic to the ssh port:

Code:
# iptables -t nat -A PREROUTING -p tcp -d 192.168.1.100 --dport 12345 -j DNAT --to 192.168.1.100:22
That rule works great if you come in from the outside world with the command
Code:
 $ telnet 192.168.1.100 12345
  ..connected...
but that indentical incantation from within the host with DNAT running on it fails. So there is something about locally sourced socket traffic that causes iptables to not do the expected. I guess we live with it..
 
Old 11-28-2011, 02:37 AM   #8
TimothyEBaldwin
Member
 
Registered: Mar 2009
Posts: 249

Rep: Reputation: 27
Quote:
Originally Posted by dbudbu View Post
# iptables -t nat -A PREROUTING -p tcp --dport 12345 -j DNAT --to 22
# telnet localhost 12345
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused
To apply to locally source traffic you need to use the OUTPUT chain, not PREROUTING.
 
  


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
Iptables can't port forward (PAT Port address translation) sfrederiksen Linux - Networking 7 12-20-2011 10:47 AM
If I forward a port in iptables, does the port have to be open on the firewall? qwertyjjj Linux - Server 4 08-06-2009 09:22 AM
A way to forward an IP/Port to another system's IP/Port astroboy5714 Linux - Networking 6 01-26-2004 05:45 PM
Port forward help please hanxuerui Linux - Networking 6 05-05-2003 01:20 PM
Forward port port 80 to lan web server dulaus Linux - Networking 9 10-04-2002 03:45 AM

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

All times are GMT -5. The time now is 08:00 PM.

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