{"id":41,"date":"2020-01-06T16:55:55","date_gmt":"2020-01-06T16:55:55","guid":{"rendered":"https:\/\/www.it-react.com\/?p=41"},"modified":"2025-01-16T23:00:14","modified_gmt":"2025-01-16T23:00:14","slug":"how-to-setup-reverse-ssh-tunnel-on-linux","status":"publish","type":"post","link":"https:\/\/www.it-react.com\/index.php\/2020\/01\/06\/how-to-setup-reverse-ssh-tunnel-on-linux\/","title":{"rendered":"How to Setup Reverse SSH Tunnel on Raspberry Pi behind a 3G USB Stick Modem"},"content":{"rendered":"\n<p class=\"has-text-align-left\">There are cases when a remote computer could be hard to reach. This computer could be placed behind a firewall or a router (NAT) whose rules or settings cannot be changed.<\/p>\n\n\n\n<p> An easy and practical solution that could help us in this case is to set up a reverse SSH tunnel on Linux. Reverse SSH tunneling allows you to create a connection from the remote computer to a local computer and using this established connection to set up a new connection from your computer back to the remote computer. How can you achieve this, I&#8217;ll explain you in the following lines. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step One &#8211; Solution Design<\/strong><\/h2>\n\n\n\n<p>My concrete case where I need this kind of implementation is a <a class=\"link hover-1\" href=\"https:\/\/www.amazon.com\/gp\/product\/B07NY4WN6W\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B07NY4WN6W&amp;linkCode=as2&amp;tag=itreact-20&amp;linkId=df8458ed3b9d5ac6d56b10caf7f16051\">Raspberry Pi<\/a> connected to Internet using a 3G USB dongle.  The Raspberry Pi is used in a remote house to convert a classic alarm system to a smart alarm (with Internet and SMS functionality). <br><br>I&#8217;ll write more about this project in a different post, for now let&#8217;s focus on ssh tunnel subject, which is quite a challenging part. <br><br>Here is the design of the solution:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"952\" height=\"453\" src=\"https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/Zeichnung1.png\" alt=\"\" class=\"wp-image-323\" srcset=\"https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/Zeichnung1.png 952w, https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/Zeichnung1-300x143.png 300w, https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/Zeichnung1-768x365.png 768w\" sizes=\"auto, (max-width: 952px) 100vw, 952px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>I would like to be able to access the <a href=\"https:\/\/www.amazon.com\/gp\/product\/B07NY4WN6W\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B07NY4WN6W&amp;linkCode=as2&amp;tag=itreact-20&amp;linkId=df8458ed3b9d5ac6d56b10caf7f16051\" target=\"_blank\" rel=\"noreferrer noopener\">Raspberry Pi<\/a> from anywhere, not only from my local computer. For this solution we&#8217;d need a Linux computer that will be anytime available and directly accessible from Internet. Or even better, we can choose a VPS (virtual private server) solution from one of the many providers.<\/p>\n\n\n\n<p> I am living in Germany and I chose a VPS solution from www.ionos.de. At the time I&#8217;m writing this article, the solution is very affordable, only 1Euro\/month and you get 1Core CPU, 0,5GB and 20GB SSD. It is also very easy to set up and it has a very nice and intuitive interface.  <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step two &#8211; Setting up a reverse SSH tunnel<\/strong> <\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Note<\/strong>: We are working with root user, if you are not logged in as root, add sudo in the front of the commands.<\/p>\n\n\n\n<p>We\u2019ll start by setting up the reverse SSH tunnel on the <a href=\"https:\/\/www.amazon.com\/gp\/product\/B07NY4WN6W\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B07NY4WN6W&amp;linkCode=as2&amp;tag=itreact-20&amp;linkId=df8458ed3b9d5ac6d56b10caf7f16051\">Raspberry Pi<\/a> by typing in the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># ssh -R 3333:localhost:22 user@xxx.xxx.xxx.xxx<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>3333 &#8211; the port used for the reverse SSH tunnel (port should be opened on VPS).  <\/li><li> 22 &#8211; default ssh port of Raspberry  <\/li><li>user@xxx.xxx.xxx.xxx &#8211; user and IP address of the VPS, replace with your actual user and IP<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>I&#8217;ve opened port 3333 in the Firewall on the management web-interface of the VPS. <br><\/p>\n\n\n\n<p>My iptables on VPS has the default configuration, so is not dropping any packets. In this case we don&#8217;t have to do any settings in iptables.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># iptables -L -vn\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Chain INPUT (policy ACCEPT 39 packets, 4642 bytes)\n  pkts bytes target     prot opt in     out     source               destination\n Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)\n  pkts bytes target     prot opt in     out     source               destination\n Chain OUTPUT (policy ACCEPT 33 packets, 5915 bytes)\n  pkts bytes target     prot opt in     out     source               destination\n<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step three &#8211; Connecting to the SSH tunnel from VPS<\/strong><\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>After we&#8217;ve created the tunnel with the command at step 1, Raspberry can be accessed by executing the following command on the VPS:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># ssh localhost -p 3333<\/pre>\n\n\n\n<p>In order to access Raspberry from another machine, we have to first ssh to the VPS <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># ssh user@xxx.xxx.xxx.xxx<\/pre>\n\n\n\n<p>And then logging in Raspberry from the VPS:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># ssh localhost -p 3333<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step four &#8211; Connecting to the SSH tunnel from another machine<\/strong><\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>To open the SSH Tunnel directly to Raspberry from another machine, without connecting first to the VPS (actually the connection will also pass through VPS, but we&#8217;ll not see anymore the VPS login), we have to add\/modify one variable in \/etc\/ssh\/sshd-config on the VPS.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># nano \/etc\/ssh\/sshd-config<\/pre>\n\n\n\n<p>Look for GatewayPorts. If you found it and it looks like this  <kbd>#GatewayPorts no<\/kbd>, delete &#8220;#&#8221; (to uncomment it) and change it to  <kbd>GatewayPorts yes<\/kbd> . If the variable doesn&#8217;t exist in the file, simply add  <kbd>GatewayPorts yes<\/kbd>  at the end of the file <\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/sshd-config.jpg\" alt=\"\" class=\"wp-image-368\" width=\"585\" height=\"459\" srcset=\"https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/sshd-config.jpg 616w, https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/sshd-config-300x236.jpg 300w\" sizes=\"auto, (max-width: 585px) 100vw, 585px\" \/><\/figure>\n\n\n\n<p>After you changed the file, we have to reload the sshd service.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># systemctl reload sshd.service<\/pre>\n\n\n\n<p>Now we can start a connection directly to Raspberry from a Windows machine by using this command in CLI, or using Putty:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt; ssh -p 3333 pi@xxx.xxx.xxx.xxx<\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"893\" height=\"306\" src=\"https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/sshfromwindows-1.jpg\" alt=\"\" class=\"wp-image-384\" srcset=\"https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/sshfromwindows-1.jpg 893w, https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/sshfromwindows-1-300x103.jpg 300w, https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/sshfromwindows-1-768x263.jpg 768w\" sizes=\"auto, (max-width: 893px) 100vw, 893px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"599\" height=\"541\" src=\"https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/putty-reverse-ssh.jpg\" alt=\"\" class=\"wp-image-430\" srcset=\"https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/putty-reverse-ssh.jpg 599w, https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/putty-reverse-ssh-300x271.jpg 300w\" sizes=\"auto, (max-width: 599px) 100vw, 599px\" \/><\/figure>\n\n\n\n<p>&#8230;and we&#8217;re in, now we can insert commands in Raspberry<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step five &#8211; Creating a persistent SSH tunnel<\/strong><\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>The tunnel we created so far won\u2019t be persistent and will be dropped  if the connection of Raspberry drops, if we want to make our reverse SSH tunnel persistent we need to install autossh.<br> Here is the command to install autossh on Raspberry:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># apt-get install autossh<\/pre>\n\n\n\n<p>We will use a script that will start automatically at boot and create the ssh tunnel. I&#8217;ve found a script that can do our job pretty good and adapted to my needs. You can find it <a href=\"https:\/\/gist.github.com\/Clement-TS\/48ae8d23f6452cd1a3a071640c1bd07b\">here<\/a> . Thanks to the author. <\/p>\n\n\n\n<p> To make the Reverse SSH Tunnel start at boot without our intervention, we need to set up ssh-key based authentication. This means that the connection will be created without being necessary to introduce the password. <br>How to achieve a ssh-key based authentication you can find <a href=\"https:\/\/www.it-react.com\/index.php\/2020\/01\/12\/create-and-install-ssh-keys-on-linux-and-windows\/\">here<\/a>. <\/p>\n\n\n\n<p>Here is the original script:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#! \/bin\/sh\n# author: Cl\u00e9ment D\u00e9siles\n# date: 01\/08\/2016\n# source: https:\/\/gist.github.com\/suma\/8134207\n# source: http:\/\/stackoverflow.com\/questions\/34094792\/autossh-pid-is-not-equal-to-the-one-in-pidfile-when-using-start-stop-daemon\n\n### BEGIN INIT INFO\n# Provides:          autossh\n# Required-Start:    $remote_fs $syslog\n# Required-Stop:     $remote_fs $syslog\n# Default-Start:     2 3 4 5\n# Default-Stop:      0 1 6\n# Short-Description: autossh initscript\n# Description:       establish a tunnelled connexion for remote access\n### END INIT INFO\n\n. \/etc\/environment\n. \/lib\/init\/vars.sh\n. \/lib\/lsb\/init-functions\n\nTUNNEL_HOST=iam.perdu.com\nTUNNEL_USER=toto\nTUNNEL_PORT=42000\nMONITOR_PORT=42001\nKEY_PATH=\/home\/toto\/.ssh\/id_rsa\n\nNAME=autossh\nDAEMON=\/usr\/lib\/autossh\/autossh\nAUTOSSH_ARGS=\"-M $MONITOR_PORT -f\"\nSSH_ARGS=\"-nNTv -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -o IdentitiesOnly=yes -o StrictHostKeyChecking=no \\\n         -i $KEY_PATH -R $TUNNEL_PORT:localhost:22 $TUNNEL_USER@$TUNNEL_HOST\"\n\nDESC=\"autossh for reverse ssh\"\nSCRIPTNAME=\/etc\/init.d\/$NAME\nDAEMON_ARGS=\" $AUTOSSH_ARGS $SSH_ARGS\"\n\n# Export PID for autossh\nAUTOSSH_PIDFILE=\/var\/run\/$NAME.pid\nexport AUTOSSH_PIDFILE\n\ndo_start() {\n\tstart-stop-daemon --start --background --name $NAME --exec $DAEMON --test &gt; \/dev\/null || return 1\n\tstart-stop-daemon --start --background --name $NAME --exec $DAEMON -- $DAEMON_ARGS    || return 2\n}\n\ndo_stop() {\n\tstart-stop-daemon --stop --name $NAME --retry=TERM\/5\/KILL\/9 --pidfile $AUTOSSH_PIDFILE --remove-pidfile\n\tRETVAL=\"$?\"\n\t&#91; \"$RETVAL\" = 2 ] &amp;&amp; return 2\n\tstart-stop-daemon --stop --oknodo --retry=0\/5\/KILL\/9 --exec $DAEMON\n\t&#91; \"$?\" = 2 ] &amp;&amp; return 2\n\treturn \"$RETVAL\"\n}\n\ncase \"$1\" in\n  start)\n\tlog_daemon_msg \"Starting $DESC\" \"$NAME\"\n\tdo_start\n\tcase \"$?\" in\n\t\t0|1) log_end_msg 0 ;;\n\t\t2) log_end_msg 1 ;;\n\tesac\n\t;;\n  stop)\n\tlog_daemon_msg \"Stopping $DESC\" \"$NAME\"\n\tdo_stop\n\tcase \"$?\" in\n\t\t0|1) log_end_msg 0 ;;\n\t\t2) log_end_msg 1 ;;\n\tesac\n\t;;\n  status)\n\tstatus_of_proc \"$DAEMON\" \"$NAME\" &amp;&amp; exit 0 || exit $?\n\t;;\n  *)\n\techo \"Usage: $SCRIPTNAME {start|stop|status|restart}\" &gt;&amp;2\n\texit 3\n\t;;\nesac<\/code><\/pre>\n\n\n\n<p>Here are the install instructions:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#<code>mv ~\/Downloads\/autossh.init.sh \/etc\/init.d\/autossh <\/code>\n#<code>chmod +x \/etc\/init.d\/autossh <\/code>\n#<code>update-rc.d -f autossh defaults 90 90 &gt; \/dev\/null 2&gt;&amp;1<\/code> <\/pre>\n\n\n\n<p>This is the part I&#8217;ve modified:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"969\" height=\"212\" src=\"https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/autossh.jpg\" alt=\"\" class=\"wp-image-410\" srcset=\"https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/autossh.jpg 969w, https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/autossh-300x66.jpg 300w, https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/autossh-768x168.jpg 768w\" sizes=\"auto, (max-width: 969px) 100vw, 969px\" \/><\/figure>\n\n\n\n<p>The monitor port, which is 9923 in our case should also be opened in VPS.<br>Autossh will send test data on the monitoring port and receive it back on the port above. In our case, autossh will set up forwards so that it can send data on port 9923 and receive it back on 9924.<\/p>\n\n\n\n<p>The autossh script should start at boot and re-connect in case the connection is dropping. <\/p>\n\n\n\n<p><strong>Update1: <\/strong>Please note in the previous screenshot that I&#8217;m opening actually two ports for reverse tunneling, one is for ssh (port 22 ) and the other one is for node-red (port 1880). So if I open a browser and access the IP address of the VPS on port 8080, then the connection will be forwarded to the Raspberry Pi port 1880, and in this way I can access the node-red running on Pi. <\/p>\n\n\n\n<p>If there is any Firewall running on the VPS or the provider of your VPS instance has a suplementary Firewall, you have to open that port (in our case port 8080). <br><br>More than that, if you have more Raspberry Pi boards in the same scenario, you can create a reverse tunelling on every Pi pointing to the same VPS, but using different ports on VPS. So if for the first Pi we used port 8080 of the VPS, for the second one we can use port 8081. When we acess http:\/\/VPS-IP:8080\/ui we&#8217;ll land to the node-red dashboard of the first Pi, when access http:\/\/VPS-IP:8081\/ui we&#8217;ll land to the node-red dashboard of the second Pi.  <\/p>\n\n\n\n<p><strong>Update2<\/strong>: autossh will create the automatic connection with the help of ssh-keys. Please check <a href=\"https:\/\/www.it-react.com\/index.php\/2020\/01\/12\/create-and-install-ssh-keys-on-linux-and-windows\/\">here<\/a> how to generate the ssh-keys. Please see in script the path of the ssh-key: KEY_PATH=\/root\/.ssh\/id_rsa <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step six &#8211; Troubleshooting Reverse SSH Tunnel<\/strong><\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>You can check if the Reverse SSH Tunnel is started by issuing this command on VPS:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># netstat -lt<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Active Internet connections (only servers)<br>\nProto Recv-Q Send-Q Local Address           Foreign Address         State<br>\ntcp        0      0 localhost:domain        0.0.0.0:*               LISTEN<br>\ntcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN<br>\ntcp        0      0 0.0.0.0:9923            0.0.0.0:*               LISTEN<br>\ntcp        0      0 0.0.0.0:3333            0.0.0.0:*               LISTEN<\/pre>\n\n\n\n<p>If you see here port 3333 and 9923 this means that Reverse SSH Tunnel is up and running. <\/p>\n\n\n\n<p>If the ports above are not listening on VPS , you have to further investigate the problem on Raspberry side and check if the autossh service is running with the command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># systemctl status autossh.service<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"984\" height=\"327\" src=\"https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/autossh-notrunning.jpg\" alt=\"\" class=\"wp-image-475\" srcset=\"https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/autossh-notrunning.jpg 984w, https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/autossh-notrunning-300x100.jpg 300w, https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/autossh-notrunning-768x255.jpg 768w\" sizes=\"auto, (max-width: 984px) 100vw, 984px\" \/><\/figure>\n\n\n\n<p>As you can see the service is active, but exited. This means that tunnel is not running. In this situation it helps to restart the service with the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> # systemctl restart autossh.service <\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"295\" src=\"https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/autossh-running-1024x295.jpg\" alt=\"\" class=\"wp-image-478\" srcset=\"https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/autossh-running-1024x295.jpg 1024w, https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/autossh-running-300x87.jpg 300w, https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/autossh-running-768x221.jpg 768w, https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/autossh-running.jpg 1127w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We have suceeded, now the service is running and our tunnel is up. <\/p>\n\n\n\n<br>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>There are cases when a remote computer could be hard to reach. This computer could be placed behind a firewall or a router (NAT) whose rules or settings cannot be changed. An easy and practical solution that could help us in this case is to set up a reverse SSH tunnel on Linux. Reverse SSH [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":20,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_FSMCFIC_featured_image_caption":"","_FSMCFIC_featured_image_nocaption":"","_FSMCFIC_featured_image_hide":"","footnotes":""},"categories":[16,7],"tags":[24,18,6,4,5,19],"class_list":["post-41","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-iot","category-linux","tag-3g-usb","tag-raspberry","tag-reversessh","tag-ssh","tag-tunnel","tag-vps"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Setup Reverse SSH Tunnel on Raspberry Pi behind a 3G USB Stick Modem - IT-REACT<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.it-react.com\/index.php\/2020\/01\/06\/how-to-setup-reverse-ssh-tunnel-on-linux\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Setup Reverse SSH Tunnel on Raspberry Pi behind a 3G USB Stick Modem - IT-REACT\" \/>\n<meta property=\"og:description\" content=\"There are cases when a remote computer could be hard to reach. This computer could be placed behind a firewall or a router (NAT) whose rules or settings cannot be changed. An easy and practical solution that could help us in this case is to set up a reverse SSH tunnel on Linux. Reverse SSH [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.it-react.com\/index.php\/2020\/01\/06\/how-to-setup-reverse-ssh-tunnel-on-linux\/\" \/>\n<meta property=\"og:site_name\" content=\"IT-REACT\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-06T16:55:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-16T23:00:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/yohei-shimomae-NkQR-rlIM44-unsplash-scaled-e1578328334875.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1081\" \/>\n\t<meta property=\"og:image:height\" content=\"299\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Ioan Penu\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ioan Penu\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.it-react.com\\\/index.php\\\/2020\\\/01\\\/06\\\/how-to-setup-reverse-ssh-tunnel-on-linux\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.it-react.com\\\/index.php\\\/2020\\\/01\\\/06\\\/how-to-setup-reverse-ssh-tunnel-on-linux\\\/\"},\"author\":{\"name\":\"Ioan Penu\",\"@id\":\"https:\\\/\\\/www.it-react.com\\\/#\\\/schema\\\/person\\\/bf08cffeb4b02ee6baff5d56ab17c8f0\"},\"headline\":\"How to Setup Reverse SSH Tunnel on Raspberry Pi behind a 3G USB Stick Modem\",\"datePublished\":\"2020-01-06T16:55:55+00:00\",\"dateModified\":\"2025-01-16T23:00:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.it-react.com\\\/index.php\\\/2020\\\/01\\\/06\\\/how-to-setup-reverse-ssh-tunnel-on-linux\\\/\"},\"wordCount\":1194,\"commentCount\":7,\"publisher\":{\"@id\":\"https:\\\/\\\/www.it-react.com\\\/#\\\/schema\\\/person\\\/bf08cffeb4b02ee6baff5d56ab17c8f0\"},\"image\":{\"@id\":\"https:\\\/\\\/www.it-react.com\\\/index.php\\\/2020\\\/01\\\/06\\\/how-to-setup-reverse-ssh-tunnel-on-linux\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.it-react.com\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/yohei-shimomae-NkQR-rlIM44-unsplash-scaled-e1578328334875.jpg\",\"keywords\":[\"3G-USB\",\"raspberry\",\"reversessh\",\"ssh\",\"tunnel\",\"vps\"],\"articleSection\":[\"IoT\",\"Linux\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.it-react.com\\\/index.php\\\/2020\\\/01\\\/06\\\/how-to-setup-reverse-ssh-tunnel-on-linux\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.it-react.com\\\/index.php\\\/2020\\\/01\\\/06\\\/how-to-setup-reverse-ssh-tunnel-on-linux\\\/\",\"url\":\"https:\\\/\\\/www.it-react.com\\\/index.php\\\/2020\\\/01\\\/06\\\/how-to-setup-reverse-ssh-tunnel-on-linux\\\/\",\"name\":\"How to Setup Reverse SSH Tunnel on Raspberry Pi behind a 3G USB Stick Modem - IT-REACT\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.it-react.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.it-react.com\\\/index.php\\\/2020\\\/01\\\/06\\\/how-to-setup-reverse-ssh-tunnel-on-linux\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.it-react.com\\\/index.php\\\/2020\\\/01\\\/06\\\/how-to-setup-reverse-ssh-tunnel-on-linux\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.it-react.com\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/yohei-shimomae-NkQR-rlIM44-unsplash-scaled-e1578328334875.jpg\",\"datePublished\":\"2020-01-06T16:55:55+00:00\",\"dateModified\":\"2025-01-16T23:00:14+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.it-react.com\\\/index.php\\\/2020\\\/01\\\/06\\\/how-to-setup-reverse-ssh-tunnel-on-linux\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.it-react.com\\\/index.php\\\/2020\\\/01\\\/06\\\/how-to-setup-reverse-ssh-tunnel-on-linux\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.it-react.com\\\/index.php\\\/2020\\\/01\\\/06\\\/how-to-setup-reverse-ssh-tunnel-on-linux\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.it-react.com\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/yohei-shimomae-NkQR-rlIM44-unsplash-scaled-e1578328334875.jpg\",\"contentUrl\":\"https:\\\/\\\/www.it-react.com\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/yohei-shimomae-NkQR-rlIM44-unsplash-scaled-e1578328334875.jpg\",\"width\":1081,\"height\":299,\"caption\":\"Photo by Yohei Shimomae on Unsplash\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.it-react.com\\\/index.php\\\/2020\\\/01\\\/06\\\/how-to-setup-reverse-ssh-tunnel-on-linux\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.it-react.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Setup Reverse SSH Tunnel on Raspberry Pi behind a 3G USB Stick Modem\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.it-react.com\\\/#website\",\"url\":\"https:\\\/\\\/www.it-react.com\\\/\",\"name\":\"it-react\",\"description\":\"Ctrl\u2022Alt\u2022Automate\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.it-react.com\\\/#\\\/schema\\\/person\\\/bf08cffeb4b02ee6baff5d56ab17c8f0\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.it-react.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.it-react.com\\\/#\\\/schema\\\/person\\\/bf08cffeb4b02ee6baff5d56ab17c8f0\",\"name\":\"Ioan Penu\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2a2a1b6be0f322a113eea11669895227e284c6091424d65be6c3c706c2822975?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2a2a1b6be0f322a113eea11669895227e284c6091424d65be6c3c706c2822975?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2a2a1b6be0f322a113eea11669895227e284c6091424d65be6c3c706c2822975?s=96&d=mm&r=g\",\"caption\":\"Ioan Penu\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2a2a1b6be0f322a113eea11669895227e284c6091424d65be6c3c706c2822975?s=96&d=mm&r=g\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Setup Reverse SSH Tunnel on Raspberry Pi behind a 3G USB Stick Modem - IT-REACT","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.it-react.com\/index.php\/2020\/01\/06\/how-to-setup-reverse-ssh-tunnel-on-linux\/","og_locale":"en_US","og_type":"article","og_title":"How to Setup Reverse SSH Tunnel on Raspberry Pi behind a 3G USB Stick Modem - IT-REACT","og_description":"There are cases when a remote computer could be hard to reach. This computer could be placed behind a firewall or a router (NAT) whose rules or settings cannot be changed. An easy and practical solution that could help us in this case is to set up a reverse SSH tunnel on Linux. Reverse SSH [&hellip;]","og_url":"https:\/\/www.it-react.com\/index.php\/2020\/01\/06\/how-to-setup-reverse-ssh-tunnel-on-linux\/","og_site_name":"IT-REACT","article_published_time":"2020-01-06T16:55:55+00:00","article_modified_time":"2025-01-16T23:00:14+00:00","og_image":[{"width":1081,"height":299,"url":"https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/yohei-shimomae-NkQR-rlIM44-unsplash-scaled-e1578328334875.jpg","type":"image\/jpeg"}],"author":"Ioan Penu","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Ioan Penu","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.it-react.com\/index.php\/2020\/01\/06\/how-to-setup-reverse-ssh-tunnel-on-linux\/#article","isPartOf":{"@id":"https:\/\/www.it-react.com\/index.php\/2020\/01\/06\/how-to-setup-reverse-ssh-tunnel-on-linux\/"},"author":{"name":"Ioan Penu","@id":"https:\/\/www.it-react.com\/#\/schema\/person\/bf08cffeb4b02ee6baff5d56ab17c8f0"},"headline":"How to Setup Reverse SSH Tunnel on Raspberry Pi behind a 3G USB Stick Modem","datePublished":"2020-01-06T16:55:55+00:00","dateModified":"2025-01-16T23:00:14+00:00","mainEntityOfPage":{"@id":"https:\/\/www.it-react.com\/index.php\/2020\/01\/06\/how-to-setup-reverse-ssh-tunnel-on-linux\/"},"wordCount":1194,"commentCount":7,"publisher":{"@id":"https:\/\/www.it-react.com\/#\/schema\/person\/bf08cffeb4b02ee6baff5d56ab17c8f0"},"image":{"@id":"https:\/\/www.it-react.com\/index.php\/2020\/01\/06\/how-to-setup-reverse-ssh-tunnel-on-linux\/#primaryimage"},"thumbnailUrl":"https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/yohei-shimomae-NkQR-rlIM44-unsplash-scaled-e1578328334875.jpg","keywords":["3G-USB","raspberry","reversessh","ssh","tunnel","vps"],"articleSection":["IoT","Linux"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.it-react.com\/index.php\/2020\/01\/06\/how-to-setup-reverse-ssh-tunnel-on-linux\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.it-react.com\/index.php\/2020\/01\/06\/how-to-setup-reverse-ssh-tunnel-on-linux\/","url":"https:\/\/www.it-react.com\/index.php\/2020\/01\/06\/how-to-setup-reverse-ssh-tunnel-on-linux\/","name":"How to Setup Reverse SSH Tunnel on Raspberry Pi behind a 3G USB Stick Modem - IT-REACT","isPartOf":{"@id":"https:\/\/www.it-react.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.it-react.com\/index.php\/2020\/01\/06\/how-to-setup-reverse-ssh-tunnel-on-linux\/#primaryimage"},"image":{"@id":"https:\/\/www.it-react.com\/index.php\/2020\/01\/06\/how-to-setup-reverse-ssh-tunnel-on-linux\/#primaryimage"},"thumbnailUrl":"https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/yohei-shimomae-NkQR-rlIM44-unsplash-scaled-e1578328334875.jpg","datePublished":"2020-01-06T16:55:55+00:00","dateModified":"2025-01-16T23:00:14+00:00","breadcrumb":{"@id":"https:\/\/www.it-react.com\/index.php\/2020\/01\/06\/how-to-setup-reverse-ssh-tunnel-on-linux\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.it-react.com\/index.php\/2020\/01\/06\/how-to-setup-reverse-ssh-tunnel-on-linux\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.it-react.com\/index.php\/2020\/01\/06\/how-to-setup-reverse-ssh-tunnel-on-linux\/#primaryimage","url":"https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/yohei-shimomae-NkQR-rlIM44-unsplash-scaled-e1578328334875.jpg","contentUrl":"https:\/\/www.it-react.com\/wp-content\/uploads\/2020\/01\/yohei-shimomae-NkQR-rlIM44-unsplash-scaled-e1578328334875.jpg","width":1081,"height":299,"caption":"Photo by Yohei Shimomae on Unsplash"},{"@type":"BreadcrumbList","@id":"https:\/\/www.it-react.com\/index.php\/2020\/01\/06\/how-to-setup-reverse-ssh-tunnel-on-linux\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.it-react.com\/"},{"@type":"ListItem","position":2,"name":"How to Setup Reverse SSH Tunnel on Raspberry Pi behind a 3G USB Stick Modem"}]},{"@type":"WebSite","@id":"https:\/\/www.it-react.com\/#website","url":"https:\/\/www.it-react.com\/","name":"it-react","description":"Ctrl\u2022Alt\u2022Automate","publisher":{"@id":"https:\/\/www.it-react.com\/#\/schema\/person\/bf08cffeb4b02ee6baff5d56ab17c8f0"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.it-react.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.it-react.com\/#\/schema\/person\/bf08cffeb4b02ee6baff5d56ab17c8f0","name":"Ioan Penu","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2a2a1b6be0f322a113eea11669895227e284c6091424d65be6c3c706c2822975?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2a2a1b6be0f322a113eea11669895227e284c6091424d65be6c3c706c2822975?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2a2a1b6be0f322a113eea11669895227e284c6091424d65be6c3c706c2822975?s=96&d=mm&r=g","caption":"Ioan Penu"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/2a2a1b6be0f322a113eea11669895227e284c6091424d65be6c3c706c2822975?s=96&d=mm&r=g"}}]}},"_links":{"self":[{"href":"https:\/\/www.it-react.com\/index.php\/wp-json\/wp\/v2\/posts\/41","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.it-react.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.it-react.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.it-react.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.it-react.com\/index.php\/wp-json\/wp\/v2\/comments?post=41"}],"version-history":[{"count":214,"href":"https:\/\/www.it-react.com\/index.php\/wp-json\/wp\/v2\/posts\/41\/revisions"}],"predecessor-version":[{"id":3407,"href":"https:\/\/www.it-react.com\/index.php\/wp-json\/wp\/v2\/posts\/41\/revisions\/3407"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.it-react.com\/index.php\/wp-json\/wp\/v2\/media\/20"}],"wp:attachment":[{"href":"https:\/\/www.it-react.com\/index.php\/wp-json\/wp\/v2\/media?parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.it-react.com\/index.php\/wp-json\/wp\/v2\/categories?post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.it-react.com\/index.php\/wp-json\/wp\/v2\/tags?post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}