<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>內網 &#8211; 記下來</title>
	<atom:link href="https://noter.tw/tag/%E5%85%A7%E7%B6%B2/feed/" rel="self" type="application/rss+xml" />
	<link>https://noter.tw</link>
	<description>一路上踩到的坑、遇到的問題，一點一滴記下來，希望能幫助到需要的人~</description>
	<lastBuildDate>Thu, 27 Apr 2023 15:07:29 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.3</generator>

<image>
	<url>https://noter.tw/wp-content/uploads/cropped-old-1130742_1920-1-32x32.jpg</url>
	<title>內網 &#8211; 記下來</title>
	<link>https://noter.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>MySQL 在內網不能連？開 1 個洞吧！autossh Reverse Tunnel 教學</title>
		<link>https://noter.tw/6742/mysql-autossh-reverse-tunnel/</link>
					<comments>https://noter.tw/6742/mysql-autossh-reverse-tunnel/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Wed, 15 Jan 2020 15:03:10 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[資料庫]]></category>
		<category><![CDATA[反向通道]]></category>
		<category><![CDATA[防火牆]]></category>
		<category><![CDATA[穿透]]></category>
		<category><![CDATA[autossh]]></category>
		<category><![CDATA[內網]]></category>
		<category><![CDATA[SSH Reverse Tunnel 教學]]></category>
		<category><![CDATA[SSH Tunnel]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[SSH 穿牆]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[無法連線]]></category>
		<category><![CDATA[Reverse Tunnel]]></category>
		<guid isPermaLink="false">https://noter.tw/?p=6742</guid>

					<description><![CDATA[<p>這篇躺在草稿區好久了，小蛙一直偷懶沒有寫，今天朋友遇到這個問題，小蛙幫忙處理了一下，趁著還有印象，趕快來把 SSH Reverse Tunnel 教學記下來。</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/6742/mysql-autossh-reverse-tunnel/" data-wpel-link="internal">MySQL 在內網不能連？開 1 個洞吧！autossh Reverse Tunnel 教學</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>這篇躺在草稿區好久了，小蛙一直偷懶沒有寫，今天朋友遇到這個問題，小蛙幫忙處理了一下，趁著還有印象，趕快來把 SSH Reverse Tunnel 教學記下來。</p>



<span id="more-6742"></span>



<p>有接觸過 Linux 的人都知道 SSH，但應該也有很多人跟小蛙一樣不知道有一個東西叫做 ssh reverse tunnel，可以透過 SSH 打出一個外部可以連進來的「洞」。這樣講很難懂，小蛙每次看完、做完就忘記了 &#8230; >&lt; 這篇記錄一下流程。</p>



<h2 class="wp-block-heading para" id="p1">環境 &amp; 目的</h2>



<p>小蛙有以下兩台機器，兩台都是 Ubuntu</p>



<ul class="my-li bg-darkblue wp-block-list">
<li>A：必須要透過 FortiClient VPN 才能經由內網 IP 進入的主機</li>



<li>B：一台外部機器</li>
</ul>



<p>小蛙的需求是要讓 B 可以連線到 A 的 MySQL，小蛙平常要連上 VPN 之後透過內部 IP 才連到的 A，因此從外部機器 B 沒辦法直接連到 A，小蛙接著要透過 SSH 打出一個反向的通道，這樣就可以讓外部機器 B 經由這個打出的通道，連進去 A 的 MySQL 裡。這就是這篇最主要的目的～</p>



<h2 class="wp-block-heading para" id="p2">前處理</h2>



<p>打洞之前需要做一些前處理</p>



<ul class="my-li bg-darkblue wp-block-list">
<li>設定連線免輸入密碼（可參考<a href="https://noter.tw/278/%e7%99%bb%e5%85%a5-ssh-%e5%85%8d%e8%bc%b8%e5%85%a5%e5%b8%b3%e8%99%9f%e5%af%86%e7%a2%bc/" data-wpel-link="internal">這篇</a>，最下方「設置公鑰」章節）</li>



<li>安裝 autossh：apt-get install autossh </li>
</ul>



<p>前處理這樣就算結束了～</p>



<h2 class="wp-block-heading para" id="p3">開洞囉！SSH Reverse Tunnel 教學</h2>



<p>其實用 ssh 加一些設定就可以做到了，但是小蛙 survey 了一些文章後發現 <a href="https://linux.die.net/man/1/autossh" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">autossh<span class="wpel-icon wpel-image wpel-icon-6"></span></a> 可以很輕鬆做到這件事情，因此選擇了使用 autossh 來協助。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">autossh -M 12345 -NfR 33306:localhost:3306 account@server -p112233

參數介紹
-M 12345 : 用來監聽 ssh 有沒有斷線的 port
-NfR : N 不運行遠端指令、f 連線後背景執行、R 將遠端 port 對應到本地端 port
33306:localhost:3306 : 把遠端的 33306 對應到本機的 3306
account@server : 用來登入遠端 SSH 的帳號
-p112233 : 如果遠端 SSH 不是 22 port，可透過 -p 來指定 port</pre>



<p>這樣看完還是霧沙沙，簡單的說因為 B 沒辦法連到 A，小蛙要從 A 執行上述指令，這樣就會連到 B，並且讓 B 可以透過 33306 port 連過來，而且連過來的時候對應到 A 的 3306 port。</p>



<p>前處理的時候已經設定登入免密碼，讓 A 在打 reverse tunnel 的時候不用再輸入密碼，而 autossh 又會在 ssh 斷線後自動重新連接。</p>



<p>小蛙很容易弄錯的點是 -R 後面參數的設定，也就是上面 33306:localhost:3306 的部份（這裡常常記反），但其實只要記</p>



<ul class="my-li bg-darkblue wp-block-list">
<li>從內網主機 or 沒辦法透過外部連線、要打洞的主機下指令</li>



<li>-R 的參數，前面的是對方 port，後面才是 localhost 本機自己的</li>
</ul>



<h2 class="wp-block-heading para" id="p4">測試</h2>



<p>從 A 打反向通道到 B 之後，接著我們就要到 B 去測試到底能不能連上 A 啦～連上 B 後，小蛙要測試的是 MySQL，這邊可以直接用 mysql 的指令來做測試</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">mysql -umysql_account -p -h127.0.0.1 -P33306</pre>



<p>噹啷～順利連上啦！</p>



<p>在 A 上面下 <code>netstat -utln</code> 可以看到多聆聽了 12345，在 B 上面下 <code>netstat -utln</code> 則會多聆聽 12345 及上面設定的 33306。</p>



<h2 class="wp-block-heading para" id="p5">注意事項</h2>



<p>ssh reverse tunnel 畢竟是在系統上面開洞，安全性上面多少會有一些影響，使用時要特別小心。</p>



<p>如果要關閉通道的話，只要下 ps -Al</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1 S     0 23025     1  0  80   0 -  1092 pause  ?        00:00:00 autossh</pre>



<p>接著 kill 23025 就可以刪掉 ssh reverse tunnel 囉！</p>


<h2 class="para">與 Ubuntu 相關的文章</h2><ul><li><span style="font-size: 1em;"><a href="https://noter.tw/10572/ubuntu-%e5%ae%89%e8%a3%9d%e5%8f%8a%e8%a8%ad%e5%ae%9a-vsftp/" data-wpel-link="internal">Ubuntu 安裝及設定 vsftp</a></span></li><li><span style="font-size: 1em;"><a href="https://noter.tw/10525/w-gpg-error-%e7%94%b1%e6%96%bc%e7%84%a1%e6%b3%95%e5%8f%96%e5%be%97%e5%ae%83%e5%80%91%e7%9a%84%e5%85%ac%e9%91%b0%ef%bc%8c%e4%bb%a5%e4%b8%8b%e7%b0%bd%e7%ab%a0%e7%84%a1%e6%b3%95%e9%80%b2%e8%a1%8c/" data-wpel-link="internal">W: GPG error: 由於無法取得它們的公鑰，以下簽章無法進行驗證： NO_PUBKEY 4EB27DB2A3B88B8B</a></span></li><li><span style="font-size: 1em;"><a href="https://noter.tw/9107/certbot-error-acmev1-is-deprecated/" data-wpel-link="internal">Certbot error: ACMEv1 is deprecated and you can &#8230;</a></span></li><li><span style="font-size: 1em;"><a href="https://noter.tw/4524/softether-vpn-%e6%9e%b6%e8%a8%ad%e8%a8%98%e9%8c%84/" data-wpel-link="internal">SoftEther VPN 架設記錄</a></span></li><li><span style="font-size: 1em;"><a href="https://noter.tw/4508/mariadb-replication-%e8%a8%ad%e5%ae%9a/" data-wpel-link="internal">MariaDB Replication 設定</a></span></li><li><span style="font-size: 1em;"><a href="https://noter.tw/50/ubuntu-%e4%bd%bf%e7%94%a8-ssmtp-%e9%80%8f%e9%81%8e-gmail-%e7%99%bc%e9%80%81-email/" data-wpel-link="internal">Ubuntu 使用 ssmtp 透過 Gmail 發送 email</a></span></li><li><span style="font-size: 1em;"><a href="https://noter.tw/115/curl-%e7%9a%84%e8%a9%ad%e7%95%b0%e5%95%8f%e9%a1%8c-vultr-dns-%e8%a8%ad%e5%ae%9a/" data-wpel-link="internal">curl 的詭異問題 &#8211; Vultr DNS 設定</a></span></li><li><span style="font-size: 1em;"><a href="https://noter.tw/3980/ubuntu-16-04-%e8%a8%ad%e5%ae%9a-apache-virtualhost-2/" data-wpel-link="internal">Ubuntu 16.04 設定 Apache VirtualHost</a></span></li><li><span style="font-size: 1em;"><a href="https://noter.tw/3946/ssh-%e9%81%a0%e7%ab%af%e7%99%bb%e5%87%ba%e5%be%8c%e7%b9%bc%e7%ba%8c%e5%9f%b7%e8%a1%8c/" data-wpel-link="internal">SSH 遠端登出後繼續執行</a></span></li><li><span style="font-size: 1em;"><a href="https://noter.tw/3913/ubuntu-16-04-phpmyadmin-deprecation-notice-with-php-7/" data-wpel-link="internal">Ubuntu 16.04 &#8211; phpmyadmin Deprecation Notice with PHP 7</a></span></li></ul><p>這篇文章 <a rel="nofollow" href="https://noter.tw/6742/mysql-autossh-reverse-tunnel/" data-wpel-link="internal">MySQL 在內網不能連？開 1 個洞吧！autossh Reverse Tunnel 教學</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/6742/mysql-autossh-reverse-tunnel/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
