<?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>ssh &#8211; 記下來</title>
	<atom:link href="https://noter.tw/tag/ssh/feed/" rel="self" type="application/rss+xml" />
	<link>https://noter.tw</link>
	<description>一路上踩到的坑、遇到的問題，一點一滴記下來，希望能幫助到需要的人~</description>
	<lastBuildDate>Wed, 03 Jan 2024 02:35:06 +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>ssh &#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[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>
		<category><![CDATA[反向通道]]></category>
		<category><![CDATA[防火牆]]></category>
		<category><![CDATA[穿透]]></category>
		<category><![CDATA[autossh]]></category>
		<category><![CDATA[內網]]></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>
		<item>
		<title>Ubuntu 使用 SSHFS 掛載遠端空間</title>
		<link>https://noter.tw/30/ubuntu-%e4%bd%bf%e7%94%a8-sshfs-%e6%8e%9b%e8%bc%89%e9%81%a0%e7%ab%af%e7%a9%ba%e9%96%93/</link>
					<comments>https://noter.tw/30/ubuntu-%e4%bd%bf%e7%94%a8-sshfs-%e6%8e%9b%e8%bc%89%e9%81%a0%e7%ab%af%e7%a9%ba%e9%96%93/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Thu, 08 Nov 2018 14:55:06 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[mount]]></category>
		<category><![CDATA[掛載]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[sshfs]]></category>
		<guid isPermaLink="false">https://noter.tw/?p=30</guid>

					<description><![CDATA[<p>趁著有空，慢慢把一些文章補齊，這篇要記錄可以掛載遠端空間的方法，很簡單也很實用，小蛙目前的主機上也都有這些設定。 關於掛載(Mount) 有使用過網路芳鄰的網友應該都知道，可以把網芳空間掛載成像是 Z&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/30/ubuntu-%e4%bd%bf%e7%94%a8-sshfs-%e6%8e%9b%e8%bc%89%e9%81%a0%e7%ab%af%e7%a9%ba%e9%96%93/" data-wpel-link="internal">Ubuntu 使用 SSHFS 掛載遠端空間</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>趁著有空，慢慢把一些文章補齊，這篇要記錄可以掛載遠端空間的方法，很簡單也很實用，小蛙目前的主機上也都有這些設定。 </p>



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



<h2 class="para wp-block-heading">關於掛載(Mount) </h2>



<p>有使用過網路芳鄰的網友應該都知道，可以把網芳空間掛載成像是 Z 槽；或是大家都使用過隨身碟吧！插上隨身碟的時候，OS 把這個外來裝置掛載在系統上(也許是 F 槽)，讓主要的系統可以存取這個裝置中的東西，今天要講的是在 Ubuntu 上透過網路來掛載遠端空間。 </p>



<p>小蛙主機的硬碟只有少得可憐的 20GB ，如果多放幾個檔案跟圖片，加上備份檔案，很快就面臨到硬碟炸裂的狀況，因此小蛙將上述這些東西擺在另外一台主機上，這樣一來主要主機可以使用到這些檔案，但是又不會占用到少少的 20GB SSD，就有點上面提到的，把 USB 隨身碟插進電腦，差別是一個掛載是直接連結本機，另一個則是透過網路掛載。 </p>



<h2 class="para wp-block-heading">使用 sshfs </h2>



<p>在 Ubuntu 要使用 sshfs 相當簡單，只需要 apt-get 就可以直接使用(更多參數看<a rel="noreferrer noopener nofollow external" href="https://linux.die.net/man/1/sshfs" target="_blank" data-wpel-link="external" class="wpel-icon-right">這裡<span class="wpel-icon wpel-image wpel-icon-6"></span></a>)，因為透過 SSH 連到遠端主機，如果沒有設定金鑰的話，總不可能每次連線都要輸入密碼吧！請先參考<a rel="noreferrer noopener" 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/" target="_blank" data-wpel-link="internal">登入 Ssh 免輸入帳號密碼</a>，設定完成並且確認 ssh 到遠端主機不用輸入密碼後，再繼續往下走喔！ </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code"># 安裝 sshfs
apt-get install sshfs
 
# 掛載遠端空間
#sshfs -o nonempty -o allow_other 帳號@主機:要掛載的遠端資料夾 掛載到本機的資料夾
# 以 nginx 帳號透過 ssh 遠端連線到 abc.com.tw 並將 /root/images 掛載到本機的 /root/remote_folder 下使用
sshfs -o nonempty -o allow_other nginx@abc.com.tw:/root/images /root/remote_folder
 
# -o 是增加的屬性，後面接屬性
# nonempty : 如果掛載的本機磁碟非空也沒關係
# allow_other : 預設掛載後只有 root 可以存取，增加這個選項讓其他使用者也可以存取</pre>



<p>小蛙一開始傻傻的，就這樣設定上去，直到有一天要用的時候發現，奇怪，裝置不見了，再掛載一次又好了，才發現還有「斷線」這件事情，當時 Google 出一個方法，可以寫腳本放在 crontab 來做這項檢查(這裡看看就好，先不要照著做) </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">if mount | grep "/root/images" &gt; /dev/null; then
        echo "/root/images already mounted";
else
        echo "mount /root/images now";
        umount nginx@abc.com.tw:/root/images
        sshfs -o nonempty -o allow_other nginx@abc.com.tw:/root/images /root/remote_folder
fi</pre>



<p>首先我們知道在 command line 下<code>mount</code>可以看到目前所有已掛載的裝置(也可以用<code>df</code>來看目前有哪些裝置)，上面這段腳本在做的事情很簡單，就是檢查<code>mount</code>輸出的文字中有沒有包含我們掛載的字串，如果有的話就寫 log 然後什麼事都不做，如果沒有掛載的話先做 umount 解除掛載，做這件事的原因是為了避免掛載兩次發生問題，之後再做 mount。 </p>



<p>小蛙剛剛 google 了一下發現，sshfs 有一個參數就是 reconnect 啊啊啊！！！根據 <a rel="noreferrer noopener nofollow external" href="https://askubuntu.com/questions/716612/sshfs-auto-reconnect" target="_blank" data-wpel-link="external" class="wpel-icon-right">SSHFS &#8211; auto reconnect<span class="wpel-icon wpel-image wpel-icon-6"></span></a> 提到的方法，斷線的時候 sshfs 就會自己重連囉！真的很方便。 </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">sshfs -o reconnect,ServerAliveInterval=15,ServerAliveCountMax=3 server:/path/to/mount /path/to/mount</pre>



<h2 class="para wp-block-heading">解除掛載 sshfs</h2>



<p><a href="https://askubuntu.com/questions/1046816/how-to-umount-non-sudo-sshfs-created-directory" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">這篇文章<span class="wpel-icon wpel-image wpel-icon-6"></span></a> 建議使用 <code>fusermount -u /temp/user/harddrive</code></p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" src="https://lh3.googleusercontent.com/pw/AM-JKLUEaTfCaIk27xsgv9PvkQ-CIg3gs96yzDGTSqf6sge2HqAPDahKkB-gtAm2P4VhKsJTxWKwwU5yIfz7ZFAzFm16PMwQQ7MRf3vIDjtL2kn8tG5RStZxFkf1LKGCqYfG8kSXH6nR410wvCLxm_TZnxqy=w923-h622-no?authuser=1" alt="AM JKLUEaTfCaIk27xsgv9PvkQ CIg3gs96yzDGTSqf6sge2HqAPDahKkB gtAm2P4VhKsJTxWKwwU5yIfz7ZFAzFm16PMwQQ7MRf3vIDjtL2kn8tG5RStZxFkf1LKGCqYfG8kSXH6nR410wvCLxm TZnxqy=w923 h622 no?authuser=1 Ubuntu 使用 SSHFS 掛載遠端空間" title="Ubuntu 使用 SSHFS 掛載遠端空間"></figure></div>


<p></p>



<p></p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/30/ubuntu-%e4%bd%bf%e7%94%a8-sshfs-%e6%8e%9b%e8%bc%89%e9%81%a0%e7%ab%af%e7%a9%ba%e9%96%93/" data-wpel-link="internal">Ubuntu 使用 SSHFS 掛載遠端空間</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/30/ubuntu-%e4%bd%bf%e7%94%a8-sshfs-%e6%8e%9b%e8%bc%89%e9%81%a0%e7%ab%af%e7%a9%ba%e9%96%93/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>登入 ssh 免輸入帳號密碼</title>
		<link>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/</link>
					<comments>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/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Sun, 03 Jun 2018 05:02:50 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[技術相關]]></category>
		<category><![CDATA[ssh key]]></category>
		<category><![CDATA[ssh 免輸入密碼]]></category>
		<category><![CDATA[authorized_keys]]></category>
		<category><![CDATA[ssh 直接登入]]></category>
		<category><![CDATA[id_rsa]]></category>
		<category><![CDATA[SSH 指定金鑰]]></category>
		<category><![CDATA[PieTTYTY]]></category>
		<category><![CDATA[SSH 允許金鑰]]></category>
		<category><![CDATA[PuTTY]]></category>
		<category><![CDATA[PuTTYgen]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[ssh-keygen]]></category>
		<category><![CDATA[免密碼]]></category>
		<guid isPermaLink="false">https://noter.tw/?p=278</guid>

					<description><![CDATA[<p>每次需要產生公私鑰的時候總是忘記，記錄在這邊，同時給有需要的人！ （2019-11-01 補充：這篇寫得很詳細，如果有不清楚的地方可以參考） 從 Windows 登入 Linux 小蛙在家大多使用 W&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" 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">登入 ssh 免輸入帳號密碼</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p> 每次需要產生公私鑰的時候總是忘記，記錄在這邊，同時給有需要的人！ （2019-11-01 補充：<a href="https://devops.ionos.com/tutorials/use-ssh-keys-with-putty-on-windows/" target="_blank" rel="noreferrer noopener nofollow external" aria-label="這篇 (在新分頁中開啟)" data-wpel-link="external" class="wpel-icon-right">這篇<span class="wpel-icon wpel-image wpel-icon-6"></span></a>寫得很詳細，如果有不清楚的地方可以參考）</p>



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



<h2 class="para wp-block-heading">從 Windows 登入 Linux </h2>



<p>

小蛙在家大多使用 Windows 的環境，當需要幫忙管理朋友的伺服器，才去翻東翻西去找出密碼來，這時候就可以在要登入管理的 Linux 系統下設定好相對應的公鑰，當小蛙的 Windows 拿著私鑰來連線的時候，公鑰跟私鑰搭配成一組就可以免輸入密碼登入囉！

</p>



<h3 class="para wp-block-heading">在 Windows 電腦上取得公私鑰步驟</h3>



<p>1. 下載 <a href="http://bcc16.ncu.edu.tw/6/putty/puttygen.html" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">PuTTYgen<span class="wpel-icon wpel-image wpel-icon-6"></span></a></p>



<p>2. 開啟後可基本按照該網頁的操作進行，大概是 (a) 設定 SSH-2 RSA  (b) 長度小蛙設定 2048  (c) 點選 Generate (小蛙後來使用 MobaXterm 裡面有一個類似 PuTTYgen 的工具 MobaKeyGen，使用上大同小異)</p>


<div class="wp-block-image">
<figure class="aligncenter size-large is-resized"><img fetchpriority="high" decoding="async" src="https://lh3.googleusercontent.com/pw/AL9nZEUVEIbskrddj_V3MycO_12NZ_yBjbtSfkYZyDagiBhIP1xpg-ssDDeDEIzVYCPGpctplCous1VBcaIXfOZ4U-gtN5dIxhJkYZYjmACIHWGJ7yYOUYvcHatz7v5cATPIZb-VyWyaBsEsULoHDkOj1tTT=w958-h894-no?authuser=2" alt="AL9nZEUVEIbskrddj V3MycO 12NZ yBjbtSfkYZyDagiBhIP1xpg ssDDeDEIzVYCPGpctplCous1VBcaIXfOZ4U gtN5dIxhJkYZYjmACIHWGJ7yYOUYvcHatz7v5cATPIZb VyWyaBsEsULoHDkOj1tTT=w958 h894 no?authuser=2 登入 ssh 免輸入帳號密碼" width="479" height="447" title="登入 ssh 免輸入帳號密碼"></figure></div>


<p>3. 此時在進度條下方滑鼠動來動去，一開始小蛙不知道，想說怎麼進度條都不會動 &#8230;</p>


<div class="wp-block-image">
<figure class="aligncenter size-large is-resized"><img decoding="async" src="https://lh3.googleusercontent.com/pw/AL9nZEWEl0WsCBdMe-1TdcH4g3rq6WILjUbFhQ5B5nqy-rnfQD7cJbDokMmza7GvIPktJlMxx9JuQL7Cnnkr1x9nY6AN4Xl9pHVIpHywp-PPhAbfP-zQbw7fVJpp5yAAMJUEMvfK9t8prAsW7pvidb0OlUcv=w477-h446-no?authuser=2" alt="AL9nZEWEl0WsCBdMe 1TdcH4g3rq6WILjUbFhQ5B5nqy rnfQD7cJbDokMmza7GvIPktJlMxx9JuQL7Cnnkr1x9nY6AN4Xl9pHVIpHywp PPhAbfP zQbw7fVJpp5yAAMJUEMvfK9t8prAsW7pvidb0OlUcv=w477 h446 no?authuser=2 登入 ssh 免輸入帳號密碼" width="358" height="335" title="登入 ssh 免輸入帳號密碼"></figure></div>


<p>4. 複製產生出來的 Pubkey</p>



<p>5. 如果要登入不輸入密碼，Key passphrase 留空白，這裡也可以把所有自己的 key 都設定成同一個密碼，這樣也可以達到所有機器用同一個密碼搭配 key 登入，不再需要記哪台機器是哪個密碼，小蛙是懶惰，不想輸入密碼 &#8230;</p>



<p>6. key comment 可以隨便打，自己可以認出是什麼並且管理就好</p>



<p>7. Save public key 及 Save private key，務必好好保管，如果私鑰掉了，就沒辦法跟公鑰搭成一對囉</p>


<div class="wp-block-image">
<figure class="aligncenter size-large is-resized"><img decoding="async" src="https://lh3.googleusercontent.com/pw/AL9nZEXUCd7H22scM89sjeWKRBuOTpV5WowL9QOImMSmyUm_3VkC3sqQX5tQXHA6YvvFnofgF80iFTAkgV-u5eMITGrcgxNvTMu3JWuYnlncho2H0Qt4vjvvVymeINnLCMyfe0ab9Nfd_EFsoBsALs6GbGql=w958-h894-no?authuser=2" alt="AL9nZEXUCd7H22scM89sjeWKRBuOTpV5WowL9QOImMSmyUm 3VkC3sqQX5tQXHA6YvvFnofgF80iFTAkgV u5eMITGrcgxNvTMu3JWuYnlncho2H0Qt4vjvvVymeINnLCMyfe0ab9Nfd EFsoBsALs6GbGql=w958 h894 no?authuser=2 登入 ssh 免輸入帳號密碼" width="479" height="447" title="登入 ssh 免輸入帳號密碼"></figure></div>


<h3 class="para wp-block-heading">Linux 設定允許登入的金鑰 </h3>



<ol class="my-li bg-darkblue wp-block-list"><li>先遠端登入 Linux 系統，小蛙的是 Ubuntu，不過應該都差不多</li><li>編輯登入目錄下的設定檔，如果沒有該檔案的話，可直接新建一個 vim ~/.ssh/authorized_keys</li><li>將上面複製的公鑰，貼在檔案中，要注意每一行為一個金鑰，儲存後離開</li></ol>



<h3 class="para wp-block-heading">Windows 設定使用私鑰連線 &#8211; PieTTY</h3>



<ol class="my-li bg-darkblue wp-block-list"><li>開啟 <a href="https://sites.google.com/view/pietty-project/home" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">PieTTY<span class="wpel-icon wpel-image wpel-icon-6"></span></a>&nbsp;或是 <a href="https://www.putty.org/" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">PuTTY<span class="wpel-icon wpel-image wpel-icon-6"></span></a>&nbsp;(以下小蛙以 PieTTY 作為範例)</li><li>點選 PuTTY 模式，先設定好要連線的 IP, Port，接著在 Saved Sessions 填入要儲存的名字，以後可以直接選，不用每次都要設定</li><li>找到 Connection -&gt; Data -&gt; Login details: Auto-login username，填入 Linux 登入的帳號</li><li>找到 Connection -&gt; SSH -&gt; Auth -&gt; 把 Attempt authentication using Pasgeant 及 Attempt &#8220;keyboard-interactive&#8221; auth (SSH-2) 打勾</li><li>接續點選 Browse&#8230; 選取上面產生出來的 Private key</li><li>回到 Session 頁籤，點選右方 Save，把剛剛的設定存好</li><li>點選 Open 就可以直接連線到 Linux 主機囉</li></ol>



<h3 class="para wp-block-heading">Windows 設定使用私鑰連線 &#8211; FileZilla</h3>



<ol class="my-li bg-darkblue wp-block-list"><li>開啟 FileZilla -&gt; 檔案 -&gt; 站台管理員</li><li>新增站台，右邊「一般」，協定「SFTP」，登入形式改成「金鑰檔案」，這裡要選擇剛剛產生的私鑰，其他主機名稱跟使用者照自己的設定輸入</li><li>直接連線就可以囉！</li></ol>



<h2 class="para wp-block-heading">Linux 連線到 Linux</h2>



<p>首先必須在要連線的 Linux client 建立一組金鑰，分別會是公鑰<code>id_rsa.pub</code>及私鑰<code>id_rsa</code>，必須要把這組金鑰保存好。 </p>



<h3 class="para wp-block-heading">產生金鑰 </h3>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 直接按 Enter
Enter passphrase (empty for no passphrase): 直接按 Enter
Enter same passphrase again: 直接按 Enter
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
......
The key's randomart image is:
......</pre>



<h3 class="para wp-block-heading">設置公鑰 </h3>



<p>把公鑰設置到要連線的主機信任清單內，可以使用上面的方法 vim ~/.ssh/authorized_keys，或是從<a rel="noreferrer noopener nofollow external" href="https://gwokae.mewggle.com/wordpress/2010/08/%E8%B6%85%E6%98%93-%E5%85%A9%E8%A1%8C%E6%8C%87%E4%BB%A4%E6%90%9E%E5%AE%9A-linux-ssh%E7%99%BB%E5%85%A5%E5%85%8D%E5%AF%86%E7%A2%BC/#step1-key-pair-gen" target="_blank" data-wpel-link="external" class="wpel-icon-right">這邊<span class="wpel-icon wpel-image wpel-icon-6"></span></a>看到的很方便的方法，直接從要連線的這台設定即可</p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">cat ~/.ssh/id_rsa.pub | ssh root@your_id_address "cat &gt;&gt; ~/.ssh/authorized_keys"
# 將 id_rsa.pub 的內容透過 ssh 傳到遠端並且放進 authorized_keys 裡面</pre>



<h2 class="para wp-block-heading">其他</h2>



<h3 class="para wp-block-heading">使用特定金鑰</h3>



<p>當下 ssh xxx@x.x.x.x 的時候，會預設使用家目錄下的 id_rsa 進行連線，但有時候小蛙必須要幫朋友用別的金鑰測試，這時候就可以透過 -i 這個參數來設定</p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">xx:~ hans$ ssh -i Desktop/private_key_mac xxx@x.x.x.x</pre>



<h3 class="para wp-block-heading">權限錯誤問題</h3>



<p>小蛙在使用朋友給的金鑰時，發生以下錯誤</p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">xx:~ hans$ ssh -i Desktop/private_key_mac xxx@x.x.x.x
Warning: Permanently added 'x.x.x.x' (ECDSA) to the list of known hosts.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for 'Desktop/private_key_mac' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "Desktop/private_key_mac": bad permissions</pre>



<p>查了一下確切需要的權限只要 400 就可以</p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">chmod 400 私鑰</pre>



<h2 class="para wp-block-heading">參考資料 </h2>



<ol class="my-li bg-darkblue wp-block-list"><li><a href="http://bcc16.ncu.edu.tw/6/putty/puttygen.html" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">PuTTYgen<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li><li><a href="https://sites.google.com/view/pietty-project/home" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">PieTTY<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li><li><a href="https://www.putty.org/" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">PuTTY<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li></ol>
<p>這篇文章 <a rel="nofollow" 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">登入 ssh 免輸入帳號密碼</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>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/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>ssh 登入速度緩慢問題</title>
		<link>https://noter.tw/3316/ssh-%e7%99%bb%e5%85%a5%e9%80%9f%e5%ba%a6%e7%b7%a9%e6%85%a2%e5%95%8f%e9%a1%8c/</link>
					<comments>https://noter.tw/3316/ssh-%e7%99%bb%e5%85%a5%e9%80%9f%e5%ba%a6%e7%b7%a9%e6%85%a2%e5%95%8f%e9%a1%8c/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Thu, 04 Dec 2014 06:29:32 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[GSSAPIAuthentication]]></category>
		<category><![CDATA[UseDNS]]></category>
		<guid isPermaLink="false">http://wazai.net/?p=3316</guid>

					<description><![CDATA[<p>之前遇到這問題過，這次遇到又忘記怎麼解決，決定記錄起來下次遇到就不用再去找答案 &#8230; 很久以前剛使用 VPS 的時候遇過這個問題，查了一些方法，到最近要用又忘記了，這個方法參考自&#160;&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/3316/ssh-%e7%99%bb%e5%85%a5%e9%80%9f%e5%ba%a6%e7%b7%a9%e6%85%a2%e5%95%8f%e9%a1%8c/" data-wpel-link="internal">ssh 登入速度緩慢問題</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p>之前遇到這問題過，這次遇到又忘記怎麼解決，決定記錄起來下次遇到就不用再去找答案 &#8230;<br />
<span id="more-3316"></span><br />
很久以前剛使用 VPS 的時候遇過這個問題，查了一些方法，到最近要用又忘記了，這個方法參考自&nbsp;<a href="http://highpoint.logdown.com/posts/2014/03/04/unix-linux-solve-the-problem-of-ssh-for-a-long-time" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">[Unix/Linux] 解決 ssh 卡很久的問題 @ HighPoint&#8217;s Notes<span class="wpel-icon wpel-image wpel-icon-6"></span></a>。印象中也有使用這方法後還是很慢的狀況，如果有什麼後續會繼續更新在這邊。<br />
方法很簡單，以小蛙遇到問題的其中一台 CentOS 為例</p>
<pre>vim /etc/ssh/sshd_config
找到 #UseDNS yes
改成 UseDNS no  (把註解打開，就是不要用 DNS)
#再重新啟動 sshd 即可
/etc/init.d/sshd restart</pre>
<p>文中提到如果這樣還是有卡卡的問題，把 GSSAPIAuthentication 也一樣改成 no，如下</p>
<pre>GSSAPIAuthentication no</pre>
<p>做個筆記留存追查。<br />
<strong>參考資料：</strong></p>
<ol>
<li>[Unix/Linux] 解決 ssh 卡很久的問題 @ HighPoint&#8217;s Notes<br />
<address><a href="http://highpoint.logdown.com/posts/2014/03/04/unix-linux-solve-the-problem-of-ssh-for-a-long-time" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">http://highpoint.logdown.com/posts/2014/03/04/unix-linux-solve-the-problem-of-ssh-for-a-long-time<span class="wpel-icon wpel-image wpel-icon-6"></span></a></address>
</li>
</ol>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/3316/ssh-%e7%99%bb%e5%85%a5%e9%80%9f%e5%ba%a6%e7%b7%a9%e6%85%a2%e5%95%8f%e9%a1%8c/" data-wpel-link="internal">ssh 登入速度緩慢問題</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/3316/ssh-%e7%99%bb%e5%85%a5%e9%80%9f%e5%ba%a6%e7%b7%a9%e6%85%a2%e5%95%8f%e9%a1%8c/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
