Networking / Beginners

Allowing Remote SSH Through a NAT Firewall

You want to open up remote SSH administration to your LAN so you can log in remotely and access various random LAN hosts. You have the OpenSSH server running on the machines you want to remotely administer, but there is a problem-they use nonroutable private IPs, so they are all source NAT-ed to the firewall IP address.

The simplest method uses any of the SSH rules (except, of course, the LAN-only rule) without requiring any changes. SSH into your firewall, then SSH from there into whatever LAN hosts you need to get into. Your sessions will look like this example, which demonstrates logging from a remote host into the firewall named windbag, and then opening an SSH session from windbag to stinkpad:

carla@remotehost:~$ ssh's password:
Linux windbag 2.6.12-10-386 #1 Mon Sep 28 12:13:15 UTC 2007 i686 GNU/Linux
Last login: Mon Aug 21 17:07:24 2007 from
carla@windbag:~$ ssh stinkpad
carla@stinkpad's password:
Last login: Mon Sep 21 17:08:50 2007 from
[carla@stinkpad ~]$

Using this method avoids the problem of having to write additional iptables rules.

What if you have users who need remote SSH access to their PCs, and you deem them worthy enough to have it? To use the two-step SSH login, they will need user accounts on the firewall, which you may not want to allow. To avoid this, you can set up port forwarding directly to LAN hosts. For example, you have host1 at, and host2 at Your remote users are at and You accept remote SSH logins only from those IP addresses:

# allow user@ to ssh directly to work PC
$ipt -t nat -A PREROUTING -i $WAN_IFACE -s --sport 1024:65535 \
-p tcp --dport 10001 -j DNAT--to-destination
$ipt -A FORWARD -p tcp -i $WAN_IFACE -o $LAN_IFACE -d \
--dport 22 -j ACCEPT

# allow user@ to ssh directly to work PC
$ipt -t nat -A PREROUTING -i $WAN_IFACE -s --sport \
1024:65535 -p tcp --dport 10002 -j DNAT --to-destination
$ipt -A FORWARD -p tcp -i $WAN_IFACE -o $LAN_IFACE -d \
--dport 22 -j ACCEPT

Then, your users simply need to specify the port number and the fully qualified domain name or IP address of the firewall to log in:

user@$ ssh


user@$ ssh

Handing out user accounts just for remote SSH access on your firewall is a bad idea. You should also configure the excellent access and authentication controls in OpenSSH to further batten the hatches, and consider using public-key authentication instead of system passwords. Your user's source IP addresses are specified in the rules because you do not want to leave LAN hosts open to the entire Internet, and you especially don't want them logging in from public machines in libraries or Internet cafes.

If your WAN IP address is dynamically assigned, then you're going to collect a lot of host keys because host keys are bound to IP addresses. So, every time the WAN address changes, you'll get a new host key. Dynamic WAN IPs cause all sorts of hassles if you want to do anything other than just have an Internet connection-running services and remote administration is a heck of a lot easier on a static WAN IP address.

[Previous] [Contents] [Next]