1 <!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">
13 <meta http-equiv=
"content-type" content=
"text/html; charset=ISO-8859-1">
24 <title>ssh tunnelling
</title></head>
26 <body bgcolor=
"#ffffff">
32 <script type=
"text/javascript"><!--
33 google_ad_client
= "pub-7705594074093958";
34 google_ad_width
= 728;
35 google_ad_height
= 90;
36 google_ad_format
= "728x90_as";
37 google_ad_channel
="";
38 google_color_border
= "336699";
39 google_color_bg
= "FFFFFF";
40 google_color_link
= "0000FF";
41 google_color_url
= "008000";
42 google_color_text
= "000000";
44 <script type
="text/javascript" src
="http://pagead2.googlesyndication.com/pagead/show_ads.js">
46 <h2>ssh tunnelling
</h2>
52 <p>ssh tunnelling is an excellent way to tunnel insecure protocols
53 through a secure communication channel. In this example, I'll tunnel
54 POP3 traffic using ssh. Traditional POP3 traffic, including username
55 and password information, travels clear-text across the network.
</p>
61 <p><a href=
"http://www.openssh.com/">OpenSSH
</a> is used in the
62 following examples.
</p>
68 <p> To tunnel POP3 traffic using ssh:
<br>
80 1. Make sure an ssh client is installed on your machine and an ssh
81 server is installed on the POP3 server.
<br>
93 2. Create a local ssh tunnel on your machine (port
1234 for this
94 example) to the POP3 server's port
110. You will need to be the root
95 user to bind to
"privileged" ports (
< 1024).
<br>
101 <span style=
"font-weight: bold;"># ssh -f -N -L
1234:localhost:
110 user@
<span style=
"font-style: italic;">POP3_server
</span></span><br>
113 3. Test the tunnel.
<br>
119 <span style=
"font-weight: bold;">$ telnet localhost
1234</span><br>
125 You should see the POP3 server's banner information.
<br>
137 4. Configure your mail client to access your mail via POP3 using mail
138 server
<span style=
"font-style: italic;">localhost
</span> and port
157 "Reverse" ssh tunnel
</h3>
163 It is possible to create a
"reverse" ssh tunnel. The reverse tunnel
164 will allow you to create an ssh tunnel from your work computer to your
165 home computer, for example, and then login to your work machine from
166 your home machine
<span style=
"font-style: italic;">even if your work
167 firewall does not permit ssh traffic initiated from your home machine!
</span><br>
179 For this to work, an ssh server must be installed on your work and home
180 computer, and ssh (TCP port
22) must be allowed outbound from your work
181 computer to your home computer.
<br>
191 $ ssh -R
<span style=
"font-style: italic;">remote_port
</span>:localhost:
22
192 <span style=
"font-style: italic;">your_home_computer
</span><br>
204 ex.
<span style=
"font-weight: bold;">$
</span> <span style=
"font-weight: bold;">ssh -R
2048:localhost:
22
205 home.computer.com
</span><br>
217 At home, you would then run
<span style=
"font-weight: bold;">ssh -p
218 2048 localhost
</span> to log into your work computer via ssh.
<br>
230 Here is a script I run every
5 minutes through the
<span style=
"font-style: italic;">cron
</span> facility on my work system to
231 make sure the reverse ssh tunnel to my home system is up and running.
232 It is useful in case
<span style=
"font-style: italic;">my_home_system
</span>
239 2006-
11-
15 update:
<br>
245 <span style=
"font-family: monospace;">#!/bin/sh
</span><br style=
"font-family: monospace;">
248 <br style=
"font-family: monospace;">
251 <span style=
"font-family: monospace;"># $REMOTE_HOST is the name of the remote system
</span><br style=
"font-family: monospace;">
254 <span style=
"font-family: monospace;">REMOTE_HOST=my.home.system
</span><br style=
"font-family: monospace;">
257 <br style=
"font-family: monospace;">
260 <span style=
"font-family: monospace;"># $REMOTE_PORT is the remote port number that will be used to tunnel
</span><br style=
"font-family: monospace;">
263 <span style=
"font-family: monospace;"># back to this system
</span><br style=
"font-family: monospace;">
266 <span style=
"font-family: monospace;">REMOTE_PORT=
5000</span><br style=
"font-family: monospace;">
269 <br style=
"font-family: monospace;">
272 <span style=
"font-family: monospace;"># $COMMAND is the command used to create the reverse ssh tunnel
</span><br style=
"font-family: monospace;">
275 <span style=
"font-family: monospace;">COMMAND=
"ssh -q -N -R $REMOTE_PORT:localhost:22 $REMOTE_HOST"</span><br style=
"font-family: monospace;">
278 <br style=
"font-family: monospace;">
281 <span style=
"font-family: monospace;"># Is the tunnel up? Perform two tests:
</span><br style=
"font-family: monospace;">
284 <br style=
"font-family: monospace;">
287 <span style=
"font-family: monospace;">#
1. Check for relevant process ($COMMAND)
</span><br style=
"font-family: monospace;">
290 <span style=
"font-family: monospace;">pgrep -f -x
"$COMMAND" > /dev/null
2>&1 || $COMMAND
</span><br style=
"font-family: monospace;">
293 <br style=
"font-family: monospace;">
296 <span style=
"font-family: monospace;">#
2. Test tunnel by looking at
"netstat" output on $REMOTE_HOST
</span><br style=
"font-family: monospace;">
299 <span style=
"font-family: monospace;">ssh $REMOTE_HOST netstat -an | egrep
"tcp.*:$REMOTE_PORT.*LISTEN" \
</span><br style=
"font-family: monospace;">
302 <span style=
"font-family: monospace;"> > /dev/null
2>&1</span><br style=
"font-family: monospace;">
305 <span style=
"font-family: monospace;">if [ $? -ne
0 ] ; then
</span><br style=
"font-family: monospace;">
308 <span style=
"font-family: monospace;"> pkill -f -x
"$COMMAND"</span><br style=
"font-family: monospace;">
311 <span style=
"font-family: monospace;"> $COMMAND
</span><br style=
"font-family: monospace;">
314 <span style=
"font-family: monospace;">fi
</span><br>
324 2006-
09-
20 update using
<span style=
"font-family: monospace;">pgrep
</span>:
<br>
332 <span style=
"font-family: monospace;">#!/bin/sh
</span><br style=
"font-family: monospace;">
336 <br style=
"font-family: monospace;">
340 <span style=
"font-family: monospace;"># REMOTE_HOST is the name of the remote system
</span><br style=
"font-family: monospace;">
344 <span style=
"font-family: monospace;">REMOTE_HOST=my.home.system
</span><br style=
"font-family: monospace;">
348 <br style=
"font-family: monospace;">
352 <span style=
"font-family: monospace;"># $COMMAND is the command used to create the reverse ssh tunnel
</span><br style=
"font-family: monospace;">
356 <span style=
"font-family: monospace;">COMMAND=
"ssh -N -R 7437:localhost:22 $REMOTE_HOST"</span><br style=
"font-family: monospace;">
360 <br style=
"font-family: monospace;">
364 <span style=
"font-family: monospace;"># Is the tunnel up?
</span><br style=
"font-family: monospace;">
368 <span style=
"font-family: monospace;">pgrep -f -x
"$COMMAND" > /dev/null
2>&1 || $COMMAND
</span><br>
380 <br style=
"font-family: monospace;">
386 <span style=
"font-family: monospace;">#!/bin/sh
</span><br style=
"font-family: monospace;">
392 <br style=
"font-family: monospace;">
398 <span style=
"font-family: monospace;"># $COMMAND is the command used to
399 create the reverse ssh tunnel
</span><br style=
"font-family: monospace;">
405 <span style=
"font-family: monospace;">COMMAND='ssh -N -R
406 31337:localhost:
22 <span style=
"font-style: italic;">my_home_system
</span>'
</span><br style=
"font-family: monospace;">
412 <br style=
"font-family: monospace;">
418 <span style=
"font-family: monospace;"># Is the tunnel up?
</span><br style=
"font-family: monospace;">
424 <span style=
"font-family: monospace;">CHECK_TUNNEL=`ps -eo args | grep
425 "$COMMAND" | grep -v grep`
</span><br style=
"font-family: monospace;">
431 <br style=
"font-family: monospace;">
437 <span style=
"font-family: monospace;"># If the tunnel is not up, create
438 the tunnel
</span><br style=
"font-family: monospace;">
444 <span style=
"font-family: monospace;">if [ -z
"$CHECK_TUNNEL" ] ; then
</span><br style=
"font-family: monospace;">
450 <span style=
"font-family: monospace;"> $COMMAND
</span><br style=
"font-family: monospace;">
456 <span style=
"font-family: monospace;">fi
</span><br>
474 <a href=
"http://www.akadia.com/services/ssh_port_forwarding.html">http://www.akadia.com/services/ssh_port_forwarding.html
</a><a href=
"http://www.hackorama.com/pages/stunnell.shtml"><br>
480 http://www.hackorama.com/pages/stunnell.shtml
</a><br>
486 <a href=
"http://proxytunnel.sourceforge.net/">http://proxytunnel.sourceforge.net/
</a><br>
492 <a href=
"http://proxytunnel.sourceforge.net/papers/muppet-200204.html">http://proxytunnel.sourceforge.net/papers/muppet-
200204.html
</a><br>
499 to
<a href=
"http://www.brandonhutchinson.com/">brandonhutchinson.com
</a>.
506 <h6>Last modified:
2006/
10/
23</h6>