Re: iptables forwarding question

From: Jeroen Geilman (nospam_at_notadaptr.nl)
Date: 09/20/03


Date: Sat, 20 Sep 2003 12:28:42 +0000

Ryan Barnard wrote:

> Hi all,
>
> I currently have a home network which has a Slackware server as a
> firewall for my cable internet. The computers behind the firewall
> have a 192.168.0.# IP.
>
> I am trying to set the network up so somebody could go to
> "computerX.mydomain.com", where computerX is a computer on the private
> network. The reason being mainly so I can ssh into my home machine
> from off-site and do whatever.

Not possible, but see below...

> So, my big question is, where should I be looking to do this? I
> currently have a small iptables setup going to forward all the machine
> out to the internet, and I assume it is some iptables setting I need
> done, but I'm not too sure.

Let me see if I understand:

- You have multiple internal machines, named computer*
- You have one (1) public IP, NATted to all of these machines
- You want to be able to use regular SSH to connect to any one of these
machines.

What I said - not possible.

See, SSH uses port 22 - of which you have only one.

So how is the firewall to decide which of those machines gets the use of
port 22 at any time ?

It doesn't - you have to tell it which one to forward SSH to.

A far better alternative would be, to assign your internal machine a static
IP address (you do not say whether it's static or dhcp) and forward any and
all SSH traffic to that IP - this is quite simple in iptables:

- forward from * to IP on port 22 all NEW connections
- reply and forward from * to IP all ESTABLISHED connections

This is all you need to forward SSH from the outside to one machine.

If you want to be able to SSH to *any* machine, it is a far better idea to
use different port numbers for each machine, say 1022 for #1, 1122 for #2,
1222 for #3 etc.

Then map those ports to the correct internal IP's port 22 with iptables, or
alternatively just run SSH on those ports - SSH doesn't care what port you
use.

The latter makes for a simpler iptables config.

It is simply not possible to use DNS names on a single public IP to map to
different machines, since SSH doesn't use hostnames - it uses the IP
address, and you have only one public IP.

One of the few protocols that DO allow this is http 1.1; you can use it to
run multiple virtual servers off of one IP.

If you're feeling adventurous, you might investigate the DNS "srv"
functionality; I know that this allows you to map a given service on a
domain to any port on any machine, but not many DNS servers implement this
yet, and it only works for entire domains, not hosts.

(M$ DS protocols use this technique)

-- 
Jeroen Geilman
Gentoo 1.4 rc4


Relevant Pages

  • Re: SSH safety
    ... SSH safety (J.L. ... FC3 missing KDE menu items ... I was wondering how safe it is to open the ssh port up to the internet. ...
    (Fedora)
  • Re: OpenSSH 3.4p1 Trouble on SCO 5.0.5?
    ... and I mean *NO* business having any direct exposure to the Internet. ... If you have to run services like SSH to it, it should be through an external firewall with some sort of logging, and preferably not run popular services like SSH on port 22. ... It looks like normal port scanning by crackers. ...
    (comp.unix.sco.misc)
  • Re: Tunnneling?
    ... >> might be able to do something temporarily using ssh and port forwarding. ... > I don't have a machine with a real IP on the internet on my network. ... > That could theoretically be set-up for a tunnel or something like that... ...
    (comp.os.linux.networking)
  • Re: portknocking question
    ... This is nice but still requires closing the port as a step when done. ... you can use a time out with the relevant iptables command ... You can easily close the connection automatically. ... In that example, any existing ssh connection, for example, will continue ...
    (Ubuntu)
  • Re: iptables rules forweb server
    ... wants to open a socket on port 9050 on my router. ... the applet wants to open a connection to a socket on port 9050. ... I give the IP address over a parameter, it's the internet IP address at the ... iptables is hard enough to read in English! ...
    (comp.os.linux.networking)