<p>I have a small local network. Only one of the machines is available to the outside world (this is not easily changeable). I'd like to be able to set it up such that ssh requests that don't come in on the standard port go to another machine. Is this possible? If so, how?</p>

<p>Oh and all of these machines are running either Ubuntu or OS X. </p>

[{'answer_id': 45231, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 0, 'selected': False, 'text': '<p>You can use Port Fowarding to do this. Take a look here:</p>\n\n<p><a href="http://portforward.com/help/portforwarding.htm" rel="nofollow noreferrer">http://portforward.com/help/portforwarding.htm</a></p>\n\n<p>There are instructions on how to set up your router to port forward request on this page:</p>\n\n<p><a href="http://www.portforward.com/english/routers/port_forwarding/routerindex.htm" rel="nofollow noreferrer">http://www.portforward.com/english/routers/port_forwarding/routerindex.htm</a></p>\n'}, {'answer_id': 45237, 'author': 'Mark Biek', 'author_id': 305, 'author_profile': 'https://Stackoverflow.com/users/305', 'pm_score': 5, 'selected': True, 'text': '<p>Another way to go would be to use ssh tunneling (which happens on the client side).</p>\n\n<p>You\'d do an ssh command like this:</p>\n\n<pre><code>ssh -L 8022:myinsideserver:22 paul@myoutsideserver\n</code></pre>\n\n<p>That connects you to the machine that\'s accessible from the outside (myoutsideserver) and creates a tunnel through that ssh connection to port 22 (the standard ssh port) on the server that\'s only accessible from the inside.</p>\n\n<p>Then you\'d do another ssh command like this (leaving the first one still connected):</p>\n\n<pre><code>ssh -p 8022 paul@localhost\n</code></pre>\n\n<p>That connection to port 8022 on your localhost will then get tunneled through the first ssh connection taking you over myinsideserver.\n</p>\n\n<p>There may be something you have to do on myoutsideserver to allow forwarding of the ssh port.  I\'m double-checking that now.</p>\n\n<p><strong>Edit</strong></p>\n\n<p>Hmmm.  The ssh manpage says this:  **Only the superuser can forward privileged ports.  **</p>\n\n<p><strike>That sort of implies to me that the first ssh connection has to be as root.  Maybe somebody else can clarify that.</strike></p>\n\n<p>It looks like superuser privileges aren\'t required as long as the forwarded port <strong><em>(in this case, 8022)</em></strong> isn\'t a privileged port (like 22).  Thanks for the clarification <a href="https://stackoverflow.com/questions/45230/is-it-possible-to-forward-ssh-requests-that-come-in-over-a-certain-port-to-anot#45254">Mike Stone</a>.</p>\n'}, {'answer_id': 45254, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 2, 'selected': False, 'text': '<p><a href="https://stackoverflow.com/questions/45230/is-it-possible-to-forward-ssh-requests-that-come-in-over-a-certain-port-to-anot#45237">@Mark Biek</a></p>\n\n<p>I was going to say that, but you beat me to it!  Anyways, I just wanted to add that there is also the -R option:</p>\n\n<pre><code>ssh -R 8022:myinsideserver:22 paul@myoutsideserver\n</code></pre>\n\n<p>The difference is what machine you are connecting to/from.  My boss showed me this trick not too long ago, and it is definitely really nice to know... we were behind a firewall and needed to give external access to a machine... he got around it by ssh -R to another machine that was accessible... then connections to that machine were forwarded into the machine behind the firewall, so you need to use -R or -L based on which machine you are on and which you are ssh-ing to.</p>\n\n<p>Also, I\'m pretty sure you are fine to use a regular user as long as the port you are forwarding (in this case the 8022 port) is not below the restricted range (which I think is 1024, but I could be mistaken), because those are the "reserved" ports.  It doesn\'t matter that you are forwarding it to a "restricted" port because that port is not being opened (the machine is just having traffic sent to it through the tunnel, it has no knowledge of the tunnel), the 8022 port IS being open and so is restricted as such.</p>\n\n<p>EDIT: Just remember, the tunnel is only open so long as the initial ssh remains open, so if it times out or you exit it, the tunnel will be closed.</p>\n'}, {'answer_id': 45278, 'author': 'JasonSmith', 'author_id': 2938, 'author_profile': 'https://Stackoverflow.com/users/2938', 'pm_score': 2, 'selected': False, 'text': '<p>(In this example, I am assuming port 2222 will go to your internal host.  $externalip and $internalip are the ip addresses or hostnames of the visible and internal machine, respectively.)</p>\n\n<p>You have a couple of options, depending on how permanent you want the proxying to be:</p>\n\n<ul>\n<li><p>Some sort of TCP proxy.  On Linux, the basic idea is that <i>before</i> the incoming packet is processed, you want to <i>change its destination</i>&mdash;i.e. prerouting destination NAT:</p>\n\n<p><code>iptables -t nat -A PREROUTING -p tcp -i eth0 -d $externalip --dport 2222 --sport \n1024:65535 -j DNAT --to $internalip:22</code></p></li>\n<li><p>Using SSH to establish temporary port forwarding.  From here, you have two options again:</p>\n\n<ul>\n<li><p>Transparent proxy, where the client thinks that your visible host (on port 2222) is just a normal SSH server and doesn\'t realize that it is passing through.  While you lose some fine-grained control, you get convenience (especially if you want to use SSH to forward VNC or X11 all the way to the inner host).</p>\n\n<ul>\n<li>From the internal machine: <code>ssh -g -R 2222:localhost:22 $externalip</code></li>\n<li>Then from the outside world: <code>ssh -p 2222 $externalip</code></li>\n</ul>\n\n<p>Notice that the "internal" and "external" machines do not have to be on the same LAN.  You can port forward all the way around the world this way.</p></li>\n<li><p>Forcing login to the external machine first.  This is true "forwarding," not "proxying"; but the basic idea is this: You force people to log in to the external machine (so you control on who can log in and when, and you get logs of the activity), and from there they can SSH through to the inside.  It sounds like a chore, but if you <i>set up simple shell scripts on the external machine with the names of your internal hosts, coupled with password-less SSH keypairs</i> then it is very straightforward for a user to log in.  So:</p>\n\n<ul>\n<li>On the external machine, you make a simple script, <code>/usr/local/bin/internalhost</code> which simply runs <code>ssh $internalip</code></li>\n<li>From the outside world, users do: <code>ssh $externalip internalhost</code> and once they log in to the first machine, they are immediately forwarded through to the internal one.</li>\n</ul>\n\n<p>Another advantage to this approach is that people don\'t get key management problems, since running two SSH services on one IP address will make the SSH client angry.</p></li>\n</ul></li>\n</ul>\n\n<p>FYI, if you want to SSH to a server and you do not want to worry about keys, do this</p>\n\n<pre><code>ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no\n</code></pre>\n\n<p>I have an alias in my shell called "nossh", so I can just do <code>nossh somehost</code> and it will ignore all key errors.  Just understand that you are ignoring security information when you do this, so there is a theoretical risk.</p>\n\n<p>Much of this information is from a talk I gave at Barcamp Bangkok all about fancy SSH tricks.  You can see <a href="http://www.proven-corporation.com/static/ssh/" rel="nofollow noreferrer">my slides</a>, but I recommend the <a href="http://www.proven-corporation.com/static/ssh/ssh.txt" rel="nofollow noreferrer">text version</a> as the S5 slides are kind of buggy.  Check out the section called "Forward Anything: Simple Port Forwarding" for info.  There is also information on creating a SOCKS5 proxy with OpenSSH.  Yes, you can do that.  OpenSSH is awesome like that.</p>\n\n<p>(Finally, if you are doing a lot of traversing into the internal network, consider setting up a VPN.  It sounds scary, but OpenVPN is quite simple and runs on all OSes.  I would say it\'s overkill just for SSH; but once you start port-forwarding through your port-forwards to get VNC, HTTP, or other stuff happening; or if you have lots of internal hosts to worry about, it can be simpler and more maintainable.)</p>\n'}, {'answer_id': 45299, 'author': 'Jay Hofacker', 'author_id': 535, 'author_profile': 'https://Stackoverflow.com/users/535', 'pm_score': 0, 'selected': False, 'text': '<p>In Ubuntu, you can install <a href="https://help.ubuntu.com/8.04/keeping-safe/C/firewall.html" rel="nofollow noreferrer" title="Set up a firewall">Firestarter</a> and then use it\'s <a href="http://www.fs-security.com/docs/policy-page.php" rel="nofollow noreferrer" title="The policy page">Forward Service</a> feature to forward the SSH traffic from a non standard port on your machine with external access to port 22 on the machine inside your network.</p>\n\n<p>On OS X you can edit the <a href="http://docs.info.apple.com/article.html?path=ServerAdmin/10.5/en/c5ns4.html" rel="nofollow noreferrer" title="Server Admin 10.5 Help: Configuring Port Forwarding">/etc/nat/natd.plist</a> file to enable port fowarding.</p>\n'}, {'answer_id': 75570, 'author': 'jtimberman', 'author_id': 7672, 'author_profile': 'https://Stackoverflow.com/users/7672', 'pm_score': 0, 'selected': False, 'text': "<p>Without messing around with firewall rules, you can set up a ~/.ssh/config file.</p>\n\n<p>Assume 10.1.1.1 is the 'gateway' system and 10.1.1.2 is the 'client' system.</p>\n\n<pre><code>Host gateway\n  Hostname 10.1.1.1 \n  LocalForward 8022 10.1.1.2:22 \n\nHost client\n  Hostname localhost\n  Port 8022\n</code></pre>\n\n<p>You can open an ssh connection to 'gateway' via:</p>\n\n<pre><code>ssh gateway\n</code></pre>\n\n<p>In another terminal, open a connection to the client.</p>\n\n<pre><code>ssh client\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/45230', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/85/']