LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 10-13-2005, 06:51 PM   #1
patcito
LQ Newbie
 
Registered: Oct 2005
Posts: 10

Rep: Reputation: 0
redirecting port on my PC


Hey all,
I use my PC as my router at home cause I have 2 network cards and no money to buy a special router box but I do have a switch though.

I get the net on my eth1 (which is connected to my aDSL ethernet modem) and I redirect it on eth0 at boot with this command:

Quote:
#!/bin/sh
#
iptables=`which iptables`
echo 1 > /proc/sys/net/ipv4/ip_forward
$iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
this is what it looks like:

rkaljord.free.fr/mynetwork.png (this is my first post so I can't post url)

The net is working great everywhere.

What I wanna do is redirect port 22 (tcp) from eth1 to one of my LAN PC 192.168.0.2
I tried several things such as this one but it didn't work:

Quote:
iptables -A FORWARD -p tcp -m tcp -d eth1 -i eth0 --dport 4662 -j ACCEPT
Thanx in advance for your help.

Patcito

PS: I use kernel 2.6.13 with iptables, ipforward and ipchain compiled
 
Old 10-14-2005, 03:28 PM   #2
imitheos
Member
 
Registered: May 2005
Location: Greece
Posts: 441

Rep: Reputation: 141Reputation: 141
Re: redirecting port on my PC

Quote:
Originally posted by patcito
Hey all,
I use my PC as my router at home cause I have 2 network cards and no money to buy a special router box but I do have a switch though.

I get the net on my eth1 (which is connected to my aDSL ethernet modem) and I redirect it on eth0 at boot with this command:

The net is working great everywhere.

What I wanna do is redirect port 22 (tcp) from eth1 to one of my LAN PC 192.168.0.2
I tried several things such as this one but it didn't work:

iptables -A FORWARD -p tcp -m tcp -d eth1 -i eth0 --dport 4662 -j ACCEPT

Thanx in advance for your help.

Patcito

PS: I use kernel 2.6.13 with iptables, ipforward and ipchain compiled
You need two rules.

iptables -t nat -A PREROUTING -p tcp --dport 22 -j DNAT --to-destination 192.168.0.2:22
iptables -A FORWARD -p tcp --dport 22 -j ACCEPT

I made them as simple as they get. You can put whatever you want from here (for example -i, -o for interface matching)
The second rule is like the one you mentioned. It accepts traffic that comes at your router to port 22 and is not destined for it.
Why this rule alone doesn't work ? Because when the packet come the destination address is the router's ip, not the NATed
box's ip. So you need to rewrite the traffic to its real destination which is the 192.168.0.2.

this is the opposite of the "-j MASQUERADE" you have. ($iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE)
This rule rewrites every packet that goes off the router so that it has the source ip is the router's one (or else the source ip
would be 192.168.0.X)

The rule i wrote does the opposite stuff. It changes the destination address so that it is the one of the NATed box.

I try to explain it as simple as i can.
If you want more information you can read the tutorial in http://iptables-tutorial.frozentux.net
It has a great deal of information about the way a packet goes and several chains.
 
Old 10-20-2005, 04:19 PM   #3
patcito
LQ Newbie
 
Registered: Oct 2005
Posts: 10

Original Poster
Rep: Reputation: 0
Thank you so much! it's working great now! I'm gonna look at your tutorials right now. By the way how can I delete the ipforward rules I setup?
 
Old 10-20-2005, 04:27 PM   #4
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

Rep: Reputation: 80
#iptables -t nat -F
deletes all rules in the nat table.

#iptables -t nat -D INPUT 2
deletes rule number 2 in chain POSTROUTING in the nat table

Last edited by Agrouf; 10-20-2005 at 04:30 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
port redirecting st3reo Linux - Networking 3 11-12-2004 05:35 PM
Port Redirecting Problem with Transparent Proxy landuchi Linux - Networking 15 11-08-2004 07:01 AM
Port redirecting English_Man Linux - Networking 3 03-09-2004 07:40 AM
Redirecting Half_Elf Linux - Networking 2 09-04-2002 10:51 PM
about port redirecting cmardhekar Linux - Newbie 1 09-13-2001 07:29 AM

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

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