LinuxQuestions.org
Help answer threads with 0 replies.
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 08-15-2007, 11:47 PM   #1
Eric-Mtl
LQ Newbie
 
Registered: Aug 2007
Posts: 1

Rep: Reputation: 0
Question Port redirection with iptables not working as expected.


I need to redirect a port to another one, but also close the original port for external users.

Until now, I've done that by blocking the original port with iptables, and a small utility called portfwd to redirect it (portfwd.sourceforge.net)

Works well, but portfwd takes up memory for each port redirection (1 instance / redirection) and it uses processor resources.

Doing it with iptables works using this command as long as both sourcePort and destPort are open:

/sbin/iptables -t nat -A PREROUTING -p tcp -i eth0 --dport sourcePort -j REDIRECT --to-ports destPort

If the following command is added to prevent direct access to destPort from outside, it also blocks the redirected access.

/sbin/iptables -A INPUT -p tcp --dport destPort -j DROP

I then tried this, but it did not work any better:

/sbin/iptables -A INPUT -p tcp --sport sourcePort --dport destPort -j ACCEPT
/sbin/iptables -A INPUT -p tcp --dport destPort -j DROP

---

So, my question is, how can direct external access to the destination port be blocked while not preventing it from the source port ?


Code:
That works :

sourcePort ----+
               |
destPort   ----+---- service (from both ports)


That's what I would like

sourcePort ----+
               |
destPort   --X +---- service  (from sourcePort only)

but that's what I get :

sourcePort ----+
               |
destPort   ----+--X - service (inaccessible)
Any ideas ?

Last edited by Eric-Mtl; 08-15-2007 at 11:55 PM.
 
Old 08-16-2007, 07:42 AM   #2
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 229Reputation: 229Reputation: 229
I am very confused by the word "original" in your 1st sentence. I took it to mean the 1st port mentioned, "a port", not the 2nd, "another one". This led me to wonder how you could both block & forward a given port.

After re-reading your post a couple of times, I think you are trying to change the port for some service that doesn't have an option to do that. For instance, if ssh didn't have a "-p" option & you wanted to run it on port 2000 instead of the normal port 22.

What happens if you change:
Code:
/sbin/iptables -A INPUT -p tcp --dport destPort -j DROP
to:
Code:
/sbin/iptables -A INPUT -p tcp -i eth0 --dport destPort -j DROP


Leave:
Code:
/sbin/iptables -t nat -A PREROUTING -p tcp -i eth0 --dport sourcePort -j REDIRECT --to-ports destPort
intact.

The key is to block only external, i.e. eth0, not all access.

Last edited by archtoad6; 08-16-2007 at 07:47 AM.
 
  


Reply

Tags
diagram


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
Port redirection, iptables, apache questions? sarajevo Linux - Security 2 09-22-2006 06:02 AM
IPtables port-forwarding not working. Ratclaws Linux - Networking 3 04-12-2005 08:14 AM
iptables port forwarding not working! friendklay Linux - Networking 1 03-23-2005 06:37 AM
Port forwarding with iptables is not working?!! philipina Linux - Networking 1 04-03-2004 03:18 PM
Iptables, Port redirection... and I'm a nimrod finegan Linux - Networking 3 09-14-2003 01:48 PM


All times are GMT -5. The time now is 02:26 PM.

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