mirror of
https://github.com/jaywcjlove/reference
synced 2026-02-01 22:48:19 +00:00
doc: update docs/ssh.md #980 ad034b476d
This commit is contained in:
@@ -40,7 +40,7 @@
|
||||
<p>此快速参考备忘单提供了使用 SSH 的各种方法。</p>
|
||||
</div></header><div class="menu-tocs"><div class="menu-btn"><svg aria-hidden="true" fill="currentColor" height="1em" width="1em" viewBox="0 0 16 16" version="1.1" data-view-component="true">
|
||||
<path fill-rule="evenodd" d="M2 4a1 1 0 100-2 1 1 0 000 2zm3.75-1.5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zM3 8a1 1 0 11-2 0 1 1 0 012 0zm-1 6a1 1 0 100-2 1 1 0 000 2z"></path>
|
||||
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#入门">入门</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#连接">连接</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#执行">执行</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#scp">SCP</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#配置位置">配置位置</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#scp-选项">SCP 选项</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#配置示例">配置示例</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#proxyjump">ProxyJump</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#ssh-copy-id">ssh-copy-id</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#ssh-keygen">SSH keygen</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#ssh-keygen-1">ssh-keygen</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#产生">产生</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#钥匙类型">钥匙类型</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#known_hosts">known_hosts</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#密钥格式">密钥格式</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#另见">另见</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="入门"><a aria-hidden="true" tabindex="-1" href="#入门"><span class="icon icon-link"></span></a>入门</h2><div class="wrap-body">
|
||||
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#入门">入门</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#连接">连接</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#执行">执行</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#scp">SCP</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#配置位置">配置位置</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#scp-选项">SCP 选项</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#配置示例">配置示例</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#proxyjump">ProxyJump</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#ssh-copy-id">ssh-copy-id</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#ssh-keygen">SSH keygen</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#ssh-keygen-1">ssh-keygen</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#产生">产生</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#钥匙类型">钥匙类型</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#known_hosts">known_hosts</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#密钥格式">密钥格式</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#端口转发">端口转发</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#动态端口转发语法">动态端口转发语法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#验证-socks5-代理">验证 SOCKS5 代理</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#本地端口转发">本地端口转发</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#远程端口转发">远程端口转发</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#另见">另见</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="入门"><a aria-hidden="true" tabindex="-1" href="#入门"><span class="icon icon-link"></span></a>入门</h2><div class="wrap-body">
|
||||
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="连接"><a aria-hidden="true" tabindex="-1" href="#连接"><span class="icon icon-link"></span></a>连接</h3><div class="wrap-body">
|
||||
<p>连接到服务器(默认端口 22)</p>
|
||||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">ssh</span> root@192.168.1.5
|
||||
@@ -255,6 +255,42 @@
|
||||
<li>PEM</li>
|
||||
<li>PKCS8</li>
|
||||
</ul>
|
||||
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="端口转发"><a aria-hidden="true" tabindex="-1" href="#端口转发"><span class="icon icon-link"></span></a>端口转发</h2><div class="wrap-body">
|
||||
<!--rehype:body-class=cols-6-->
|
||||
</div></div><div class="h2wrap-body cols-6"><div class="wrap h3body-not-exist col-span-3"><div class="wrap-header h3wrap"><h3 id="动态端口转发语法"><a aria-hidden="true" tabindex="-1" href="#动态端口转发语法"><span class="icon icon-link"></span></a>动态端口转发语法</h3><div class="wrap-body">
|
||||
<!--rehype:wrap-class=col-span-3-->
|
||||
<p>将本地端口变成一个 SOCKS 代理,自动转发任意目标地址的请求(适用于代理上网)。</p>
|
||||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">ssh</span> <span class="token parameter variable">-D</span> 本地SOCKS端口 用户名@SSH服务器 <span class="token parameter variable">-N</span>
|
||||
</span></code></pre>
|
||||
<p>示例:开启本地 1080 端口作为 SOCKS5 代理:</p>
|
||||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">ssh</span> <span class="token parameter variable">-D</span> <span class="token number">1080</span> <span class="token parameter variable">-N</span> user@example.com
|
||||
</span></code></pre>
|
||||
</div></div></div><div class="wrap h3body-not-exist col-span-3"><div class="wrap-header h3wrap"><h3 id="验证-socks5-代理"><a aria-hidden="true" tabindex="-1" href="#验证-socks5-代理"><span class="icon icon-link"></span></a>验证 SOCKS5 代理</h3><div class="wrap-body">
|
||||
<!--rehype:wrap-class=col-span-3-->
|
||||
<p>使用 curl 命令验证 SOCKS5 代理是否工作正常:</p>
|
||||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token comment"># 设置</span>
|
||||
</span><span class="code-line">$ <span class="token function">ssh</span> <span class="token parameter variable">-D</span> <span class="token number">1080</span> <span class="token parameter variable">-N</span> user@example.com
|
||||
</span><span class="code-line"><span class="token comment"># 验证</span>
|
||||
</span><span class="code-line">$ <span class="token function">curl</span> <span class="token parameter variable">--socks5</span> <span class="token number">127.0</span>.0.1:1080 https://ifconfig.me
|
||||
</span></code></pre>
|
||||
<p>如果返回的是 <code>example.com</code> 服务器的公网 IP,说明 SOCKS5 代理正常工作。</p>
|
||||
</div></div></div><div class="wrap h3body-not-exist col-span-3"><div class="wrap-header h3wrap"><h3 id="本地端口转发"><a aria-hidden="true" tabindex="-1" href="#本地端口转发"><span class="icon icon-link"></span></a>本地端口转发</h3><div class="wrap-body">
|
||||
<!--rehype:wrap-class=col-span-3-->
|
||||
<p>将本地端口的流量通过 SSH 隧道转发到目标服务器(适用于访问远程/内网服务)。</p>
|
||||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">ssh</span> <span class="token parameter variable">-L</span> <span class="token punctuation">[</span>本地IP:<span class="token punctuation">]</span>本地端口:目标IP:目标端口 用户名@SSH服务器 <span class="token parameter variable">-N</span>
|
||||
</span></code></pre>
|
||||
<p>示例:将本地的 <code>4000</code> 端口转发到远程内网服务器 192.168.1.10:80:</p>
|
||||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">ssh</span> <span class="token parameter variable">-L</span> <span class="token number">4000</span>:192.168.1.10:80 user@example.com <span class="token parameter variable">-N</span>
|
||||
</span></code></pre>
|
||||
<p>其中 <code>-N</code> 表示不执行远程命令,只是建立隧道。</p>
|
||||
</div></div></div><div class="wrap h3body-not-exist col-span-3"><div class="wrap-header h3wrap"><h3 id="远程端口转发"><a aria-hidden="true" tabindex="-1" href="#远程端口转发"><span class="icon icon-link"></span></a>远程端口转发</h3><div class="wrap-body">
|
||||
<!--rehype:wrap-class=col-span-3-->
|
||||
<p>让远程 SSH 服务器上的端口转发到本地的某个服务(适用于让外部访问你本地服务)。</p>
|
||||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">ssh</span> <span class="token parameter variable">-R</span> <span class="token punctuation">[</span>SSH服务器IP:<span class="token punctuation">]</span>远程端口:本地IP:本地端口 用户名@SSH服务器 <span class="token parameter variable">-N</span>
|
||||
</span></code></pre>
|
||||
<p>示例:将远程服务器的 5000 端口映射到你本地的 localhost:3306(MySQL):</p>
|
||||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">ssh</span> <span class="token parameter variable">-R</span> <span class="token number">5000</span>:localhost:3306 user@example.com <span class="token parameter variable">-N</span>
|
||||
</span></code></pre>
|
||||
</div></div></div></div></div><div class="wrap h2body-not-exist"><div class="wrap-header h2wrap"><h2 id="另见"><a aria-hidden="true" tabindex="-1" href="#另见"><span class="icon icon-link"></span></a>另见</h2><div class="wrap-body">
|
||||
<ul>
|
||||
<li><a href="https://www.cyberciti.biz/faq/create-ssh-config-file-on-linux-unix/">OpenSSH 配置文件示例</a> <em>(cyberciti.biz)</em></li>
|
||||
|
||||
@@ -1801,7 +1801,7 @@
|
||||
|
||||
gtag('config', 'G-9MWEWXSDQK');
|
||||
</script>
|
||||
</div></div><div class="h2wrap-body"></div></div></div></div><footer class="footer-wrap"><footer class="max-container">© 2022 <a href="https://wangchujiang.com/#/app" target="_blank">Kenny Wang</a>. Updated on 2025/07/18 01:11:41</footer></footer><script async src="https://www.googletagmanager.com/gtag/js?id=G-9MWEWXSDQK"></script><script>window.dataLayer = window.dataLayer || [];
|
||||
</div></div><div class="h2wrap-body"></div></div></div></div><footer class="footer-wrap"><footer class="max-container">© 2022 <a href="https://wangchujiang.com/#/app" target="_blank">Kenny Wang</a>. Updated on 2025/07/18 19:16:05</footer></footer><script async src="https://www.googletagmanager.com/gtag/js?id=G-9MWEWXSDQK"></script><script>window.dataLayer = window.dataLayer || [];
|
||||
function gtag(){dataLayer.push(arguments);}
|
||||
gtag('js', new Date());
|
||||
gtag('config', 'G-9MWEWXSDQK');</script><script src="data.js?v=1.9.0" defer></script><script src="js/fuse.min.js?v=1.9.0" defer></script><script src="js/main.js?v=1.9.0" defer></script><div id="mysearch"><div class="mysearch-box"><div class="mysearch-input"><div><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
|
||||
|
||||
Reference in New Issue
Block a user