<!-- begin _includes/seo.html --><title>SSH into Machine That Is Behind a Private Network - Şahin Akkaya’s Personal Page</title>
<metaname="description"content="Story I believe there is always a “tech support person” in every home. Everyone knows that when there is a problem with any electronic device, they should ask this person. I am the tech support in our house. Today, I had to fix a problem in our desktop. Since I was not at home, I had to fix the problem remotely.">
<metaproperty="og:description"content="Story I believe there is always a “tech support person” in every home. Everyone knows that when there is a problem with any electronic device, they should ask this person. I am the tech support in our house. Today, I had to fix a problem in our desktop. Since I was not at home, I had to fix the problem remotely.">
<!-- insert favicons. use https://realfavicongenerator.net/ -->
<!-- end custom head snippets -->
</head>
<bodyclass="layout--single">
<navclass="skip-links">
<ul>
<li><ahref="#site-nav"class="screen-reader-shortcut">Skip to primary navigation</a></li>
<li><ahref="#main"class="screen-reader-shortcut">Skip to content</a></li>
<li><ahref="#footer"class="screen-reader-shortcut">Skip to footer</a></li>
</ul>
</nav>
<!--[if lt IE 9]>
<divclass="notice--danger align-center"style="margin: 0;">You are using an <strong>outdated</strong> browser. Please <ahref="https://browsehappy.com/">upgrade your browser</a> to improve your experience.</div>
<metaitemprop="headline"content="SSH into Machine That Is Behind a Private Network">
<metaitemprop="description"content="StoryI believe there is always a “tech support person” in every home. Everyone knows that when there is a problem with any electronic device, they should ask this person. I am the tech support in our house. Today, I had to fix a problem in our desktop. Since I was not at home, I had to fix the problem remotely.">
<p>I believe there is always a “tech support person” in every home. Everyone knows that when there is a problem with any electronic device, they should ask this person. I am the tech support in our house. Today, I had to fix a problem in our desktop. Since I was not at home, I had to fix the problem remotely.</p>
<p>Just tell the non-tech people at home to configure the router to forward ssh traffic to desktop, right? Well, this is not an option for me, not because people are non-tech, but there is no router! The desktop is connected to internet via hotspot from mobile phone. There is no root access in the phone and even if there was, it is a really big pain to forward the packets manually. Trust me. Been there, done that!</p>
</li>
<li>
<p>There are tools like <ahref="https://www.ngrok.com">ngrok</a>, <ahref="localtunnel.me">localtunnel</a> which exposes your localhost to the internet and gives you a URL to access it but I did not want to use them.</p>
<ul>
<li>I did not want to use <codeclass="language-plaintext highlighter-rouge">ngrok</code> because it is not open source and it might have security issues. They are also charging you.</li>
<li><codeclass="language-plaintext highlighter-rouge">localtunnel</code> seemed perfect. The code of both client and server is open. That is great news! But it did not last long because it is just forwarding http/https traffic :(</li>
</ul>
</li>
</ul>
<h2id="solution">Solution</h2>
<p>I was thinking of extending the functionality of <codeclass="language-plaintext highlighter-rouge">localtunnel</code>, but I learned a very simple way. You don’t need any external program to overcome this issue. The good old <codeclass="language-plaintext highlighter-rouge">ssh</code> can do that! All you need is another machine (a remote server) that both computers can access via ssh.</p>
<divclass="language-bash highlighter-rouge"><divclass="highlight"><preclass="highlight"><code><spanclass="c"># local machine (my home computer)</span>
<p>This command forwards all the incoming connections to port 7777 of remote machine to port 22 of our current machine. In order for this to work, you need to make sure <codeclass="language-plaintext highlighter-rouge">GatewayPorts</code> is set to <codeclass="language-plaintext highlighter-rouge">yes</code> in the remote server ssh configuration. It also assumes our current machine accepts ssh connections via port 22.</p>
<hr/>
<p>Now, go to any machine and connect to the remote server first. When we are connected, we will create another ssh connection to port 7777 to connect our home computer.</p>
<divclass="language-bash highlighter-rouge"><divclass="highlight"><preclass="highlight"><code><spanclass="c"># another local machine (my laptop)</span>
<p>As a result, it only took us 2 simple ssh commands to do this. This is just unbelievable! Now, I need to find a way to make non-tech people at home run this command when there is a problem. Too bad Linux can’t help me there :D</p>
<pclass="archive__item-excerpt"itemprop="description">Last week, I received an email from LetsEncrypt reminding me to renew my certificates. I forgot to renew it and the certificate expired. Now I can’t send or ...</p>
<pclass="archive__item-excerpt"itemprop="description">Anytime I install a new system on my machine, I pray God for nothing bad happens. But it usually happens. When I reboot, I find myself in the “Grub rescue” m...</p>
In my previous post, I explained how to do port forwarding to access some machine behind private network. I will use this method to fix some issues in ...</p>
Today, I have recorded a video for one of my classes and I was required to upload it till midnight. The video was perfect except for a few seconds wher...</p>