Tunneling over an intermediate to overcome demilitarized zone
Often you want to connect to a computer inside a network that is hidden from the outside. This "private" network, aka demilitarized zone (DMZ), can be overcome by having a computer inside the DMZ that opens a connection to an other "open" network or computer. This article describes roughly how to do that using Linux.

Source code: shell script

Tunneling over a intermediar is often used to connect to a server which is e.g. in a DMZ. In the following case we have a server that is the company network and does only allow connections on port 22 from the DMZ. Means the server cannot be connection over the internet. In addition, we have a server in the DMZ which allows connection on port 22 from outside (internet) and can connect to the company network over port 22. Last but not least, every time a server restarts we want that the tunnels automatically opens, means that we need to have an cron job that checks, whether an open connection exists from the server to the intermediary or not, and if the server opens a connection to the intermediary the system does not need to prompt for password. Solution is here to register the public key of the server on the intermediary.


Opening a tunnel from client to intermediary

ssh -L 22000: -N root@

Connecting to server

ssh -P 22000 michel@

Copy files (SCP)

scp -P 22000 michel@ /Users/michel/Public/Drop\ Box/

scp /Users/michel/Public/Drop\ Box/tunelling.sh -P 22000 michel@


Register GPG key from server

apt-key add <file>


Export GPG key

apt-key export <key> > <file>

Open a connection to intermediary from server

Save the following script as e.g. tunneling.sh


cmd="ssh -R 22000: -N root@"

match=$(ps -aef | grep "${cmd}" | grep -v grep)
if [ -z "${match}" ] ; then
$cmd &

Register cron job

Edit crontab

crontab -e

Adding a new job to the crontab. The following entry executes the defined script tunneling.sh every 5 minutes.

5 * * * * /<path>/tunnelilng.sh

Crontab is structured as follow:

* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
comments powered by Disqus