<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Pavilion_Cat&#39;s Blog</title>
  
  <subtitle>欲买桂花同载酒 终不似 少年游</subtitle>
  <link href="http://blog.pavilioncat.com/atom.xml" rel="self"/>
  
  <link href="http://blog.pavilioncat.com/"/>
  <updated>2025-12-26T18:08:25.000Z</updated>
  <id>http://blog.pavilioncat.com/</id>
  
  <author>
    <name>Pavilion_Cat</name>
    
  </author>
  
  <generator uri="https://hexo.io/">Hexo</generator>
  
  <entry>
    <title>《半导体物理学》学习笔记：第一章 半导体中的电子状态</title>
    <link href="http://blog.pavilioncat.com/semiconductor-physics-study-notes-electron-states-in-semiconductors/"/>
    <id>http://blog.pavilioncat.com/semiconductor-physics-study-notes-electron-states-in-semiconductors/</id>
    <published>2025-12-26T18:08:25.000Z</published>
    <updated>2025-12-26T18:08:25.000Z</updated>
    
    <content type="html"><![CDATA[<div class="note pink"><p>教材：刘恩科 朱秉升 罗晋生 等编著《半导体物理学》（第 8 版）電子工業出版社</p></div><p><em>本文乃阿猫学余笔录，拙作难免疏漏，恳请不吝指正</em></p><h2 id="1-半导体的晶格结构和结合性质"><a href="#1-半导体的晶格结构和结合性质" class="headerlink" title="1.半导体的晶格结构和结合性质"></a>1.半导体的晶格结构和结合性质</h2><details class="fold blue" open><summary>为什么要学半导体的晶格结构</summary><div class='fold-content'><p>因为材料的电学特性与两个有关，一个是化学组份，一个是原子的排列。原子的排列其实就是晶格结构，他影响着电子的运动，而电子的运动他就间接的反映了材料的电学特性。</p></div></details><h3 id="1-固体的三种类型"><a href="#1-固体的三种类型" class="headerlink" title="1.*.固体的三种类型"></a>1.*.固体的三种类型</h3><div class="note yellow"><p><strong>按照原子排列的有序程度</strong></p><ul><li>非晶：无序</li><li>多晶：晶粒和晶界</li><li>单晶：长程有序</li></ul></div><h3 id="1-1-金刚石型结构（Si、Ge）"><a href="#1-1-金刚石型结构（Si、Ge）" class="headerlink" title="1.1.金刚石型结构（Si、Ge）"></a>1.1.金刚石型结构（Si、Ge）</h3><p><img   lazyload src="/assets/images/Meow-Loading.webp" data-lazy-src="/./semiconductor-physics-study-notes-electron-states-in-semiconductors/1.png"  alt="金刚石型结构的晶胞" title="金刚石型结构的晶胞"><br><em>以硅和锗为主的重要半导体材料在化学元素周期表中都属于Ⅳ族元素，原子最外层都具有 4 个价电子</em></p><div class="note yellow"><p><strong>正四面体结构</strong></p><ul><li>原子与原子之间为<strong>共价键</strong>。</li><li>配位数是 4 </li><li>键角109°28’</li></ul></div><p>配位数：晶体中任一原子（离子）周围最近邻的原子（离子）数。例：NaCl的配位数为6。配位数反映了晶体中原子（离子）排列的紧密程度。</p><p>晶胞：晶体结构的基本单元，既反映了周期性，又反映了各种对称性。整个晶体是由晶胞周期排列而成的。</p><div class="note yellow"><p><img   lazyload src="/assets/images/Meow-Loading.webp" data-lazy-src="/./semiconductor-physics-study-notes-electron-states-in-semiconductors/2.png"  alt="两个相互嵌套的面心立方（FCC）子晶格" title="两个相互嵌套的面心立方（FCC）子晶格"><br><strong>结晶学原胞</strong></p><ul><li>由<strong>两个面心立方体（FCC）晶胞</strong>沿立方体的空间对角线位移 <strong>1&#x2F;4</strong> 空间对角线长度套构而成。</li></ul></div><h3 id="1-2-闪锌矿型结构（GaAs）"><a href="#1-2-闪锌矿型结构（GaAs）" class="headerlink" title="1.2.闪锌矿型结构（GaAs）"></a>1.2.闪锌矿型结构（GaAs）</h3><p><img   lazyload src="/assets/images/Meow-Loading.webp" data-lazy-src="/./semiconductor-physics-study-notes-electron-states-in-semiconductors/3.png"  alt="GaAs闪锌矿型结构3D球棍模型" title="GaAs闪锌矿型结构3D球棍模型"><br><em>由化学元素周期表中的Ⅲ族元素铝、镓、铟和Ⅴ族元素磷、砷、锑合成的Ⅲ-Ⅴ族化合物都是半导体材料，他们绝大多数都具有闪锌矿型结构，典型代表材料是砷化镓（GaAs）</em></p><div class="note yellow"><p><strong>四面体结构</strong></p><ul><li>原子与原子之间是混合键：<strong>共价键 + 离子键</strong></li><li>注意是<strong>两种</strong>不同原子</li></ul></div><h3 id="1-例题"><a href="#1-例题" class="headerlink" title="1.例题"></a>1.例题</h3><details class="fold blue" ><summary>简答题 金刚石型结构和闪锌矿型结构的区别 （点击展开查看答案）</summary><div class='fold-content'><ol><li>金刚石型结构原子间是共价键，闪锌矿型结构原子间是共价键和离子键。</li><li>金刚石型结构由一种原子构成，闪锌矿型结构由两种不同原子构成。</li></ol></div></details><h2 id="2-半导体中的电子状态和能带"><a href="#2-半导体中的电子状态和能带" class="headerlink" title="2.半导体中的电子状态和能带"></a>2.半导体中的电子状态和能带</h2><h3 id="2-1-1-电子的共有化运动"><a href="#2-1-1-电子的共有化运动" class="headerlink" title="2.1.1.电子的共有化运动"></a>2.1.1.电子的共有化运动</h3><p><img   lazyload src="/assets/images/Meow-Loading.webp" data-lazy-src="/./semiconductor-physics-study-notes-electron-states-in-semiconductors/4.png"  alt="电子共有化运动示意图" title="电子共有化运动示意图"><br>原子组成晶体后，由于电子壳层交叠，电子不再完全局限在某一个原子上，可以由一个原子转移到相邻的原子上去，因而，电子将可以在整个晶体中运动。</p><h3 id="2-1-2-能带的形成"><a href="#2-1-2-能带的形成" class="headerlink" title="2.1.2.能带的形成"></a>2.1.2.能带的形成</h3><p><img   lazyload src="/assets/images/Meow-Loading.webp" data-lazy-src="/./semiconductor-physics-study-notes-electron-states-in-semiconductors/5.png"  alt="原子能级分裂为能带的情况示意图" title="原子能级分裂为能带的情况示意图"></p><ul><li>当两个原子互相靠近时，原来在某一能级上的电子就分别处在分裂的两个能级上，这时电子不再属于某一个原子，而为两个原子所共有。</li><li>推导至 N 个，在 N 个原子互相靠近结合成晶体后，每个电子都要受到周围原子势场的作用，其结果是每个 N 度简并能级都分裂成 N 个彼此很近的能级，这 N 个能级组成一个能带。（这正是共有化运动导致的）</li></ul><div class="note yellow"><p>分裂的每一个能带都称为<strong>允带</strong>，允带之间没有能级故称为<strong>禁带</strong>。</p></div><h3 id="2-1-例题"><a href="#2-1-例题" class="headerlink" title="2.1.例题"></a>2.1.例题</h3><details class="fold blue" ><summary>简答题 请简述硅晶体能带的形成过程 （点击展开查看答案）</summary><div class='fold-content'><ol><li>轨道交叠 ↓</li><li>电子共有化运动 ↓</li><li>能级分裂 ↓</li><li>实际能带</li></ol><p>这里需要注意的是，一定要是 N 个原子靠近，N 要远远大于 1</p></div></details><h3 id="2-2-导体、半导体、绝缘体的能带"><a href="#2-2-导体、半导体、绝缘体的能带" class="headerlink" title="2.2.导体、半导体、绝缘体的能带"></a>2.2.导体、半导体、绝缘体的能带</h3><p>固体能够导电是固体中的电子在外电场作用下做定向运动的结果。</p><p>对于被电子部分占满的能带，在外电场的作用下，电子可从外电场中吸收能量跃迁到未被电子占据的能级，形成电流并起导电作用，常称这种能带为<strong>导带</strong>。</p><p><img   lazyload src="/assets/images/Meow-Loading.webp" data-lazy-src="/./semiconductor-physics-study-notes-electron-states-in-semiconductors/6.jpg"  alt="绝缘体、导体、半导体的能带示意图" title="绝缘体、导体、半导体的能带示意图"></p><p>绝缘体和半导体的能带类似，如图所示，即下半部分是已被价电子占满的满带（其下面还有被内层电子占满的若干满带未画出），也称为<strong>价带</strong>，中间为<strong>禁带</strong>，上面为<strong>导带</strong>。</p><p><strong>绝缘体和半导体的主要区别在于禁带宽度</strong></p><div class="table-container"><table><thead><tr><th align="center"></th><th align="center">绝缘体</th><th align="center">半导体</th></tr></thead><tbody><tr><td align="center">禁带</td><td align="center">宽</td><td align="center">窄</td></tr><tr><td align="center">电子激发</td><td align="center">不易激发</td><td align="center">易激发</td></tr><tr><td align="center">导电性</td><td align="center">差</td><td align="center">导电性介于绝缘体和导体之间</td></tr></tbody></table></div><p>因为绝缘体禁带宽，电子跃迁到导带需要的能量就越大，所以难激发，导电性差；半导体同理。</p><h2 id="3-半导体中电子的运动——有效质量"><a href="#3-半导体中电子的运动——有效质量" class="headerlink" title="3.半导体中电子的运动——有效质量"></a>3.半导体中电子的运动——有效质量</h2><h3 id="3-1-半导体中-E-k-与-k-的关系"><a href="#3-1-半导体中-E-k-与-k-的关系" class="headerlink" title="3.1.半导体中 E(k) 与 k 的关系"></a>3.1.半导体中 E(k) 与 k 的关系</h3><p><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>E</mi><mo stretchy="false">(</mo><mi>k</mi><mo stretchy="false">)</mo><mo>−</mo><mi>E</mi><mo stretchy="false">(</mo><mn>0</mn><mo stretchy="false">)</mo><mo>&#x3D;</mo><mfrac><mrow><msup><mi data-mjx-alternate="1">ℏ</mi><mn>2</mn></msup><msup><mi>k</mi><mn>2</mn></msup></mrow><mrow><mn>2</mn><msubsup><mi>m</mi><mi>n</mi><mo>∗</mo></msubsup></mrow></mfrac></math></p><ul><li>E(k)：电子在晶体中的能量，是波矢 k 的函数（能带色散关系）。</li><li>E(0)：导带底部（conduction band minimum, CBM）的能量，通常设为参考零点（在导带底附近分析时，常写作 E_c 或简化为 E(0)）。</li><li>ħ：约化普朗克常数（ħ &#x3D; h &#x2F; 2π）。</li><li>k：电子的晶体波矢（Bloch波的波矢），|k| 是相对于能带极值点（通常是Γ点或L点等）的距离。</li></ul><h3 id="3-2-半导体中电子的平均速度"><a href="#3-2-半导体中电子的平均速度" class="headerlink" title="3.2.半导体中电子的平均速度"></a>3.2.半导体中电子的平均速度</h3><p>晶体中电子的平均速度 v 与其波矢 k 的关系由 E(k) 关系决定，具体为：<br><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mover><mi>v</mi><mo>⃗</mo></mover><mo>&#x3D;</mo><mfrac><mn>1</mn><mi>ℏ</mi></mfrac><mi>∇</mi><mi>E</mi><mo stretchy="false">(</mo><mover><mi>k</mi><mo>⃗</mo></mover><mo stretchy="false">)</mo></math></p><p>对于一维情况，可简化为：<br><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>v</mi><mo>&#x3D;</mo><mfrac><mn>1</mn><mi>ℏ</mi></mfrac><mfrac><mrow><mi>d</mi><mi>E</mi><mo stretchy="false">(</mo><mi>k</mi><mo stretchy="false">)</mo></mrow><mrow><mi>d</mi><mi>k</mi></mrow></mfrac></math><br>将 E(k) 与 k 的关系带入上式可得：<br><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>v</mi><mo>⋅</mo><msubsup><mi>m</mi><mi>n</mi><mo>∗</mo></msubsup><mo>&#x3D;</mo><mi>ℏ</mi><mi>k</mi></math></p><h3 id="3-3-电子的准动量"><a href="#3-3-电子的准动量" class="headerlink" title="3.3.电子的准动量"></a>3.3.电子的准动量</h3><p>晶体中电子的动量不同于自由电子动量 <math><mi>p</mi><mo>&#x3D;</mo><mi>m</mi><mi>v</mi></math>，我们引入<strong>准动量</strong>（或晶体动量）的概念，定义为：<br><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mover><mi>p</mi><mo>⃗</mo></mover><mo>&#x3D;</mo><mi>ℏ</mi><mover><mi>k</mi><mo>⃗</mo></mover></math></p><p>准动量是描述晶体中电子运动状态的重要物理量，在外力作用下，电子准动量的变化遵循类似牛顿第二定律的规律。</p><h3 id="3-4-有效质量的引入与物理意义"><a href="#3-4-有效质量的引入与物理意义" class="headerlink" title="3.4.有效质量的引入与物理意义"></a>3.4.有效质量的引入与物理意义</h3><p>在外力 F 作用下，电子的能量发生变化，其准动量也随之改变。通过推导加速度 a 与外力 F 的关系，可以引出<strong>有效质量</strong> 的概念。</p><p>一维情况下：<br><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>a</mi><mo>&#x3D;</mo><mfrac><mrow><mi>d</mi><mi>v</mi></mrow><mrow><mi>d</mi><mi>t</mi></mrow></mfrac><mo>&#x3D;</mo><mfrac><mn>1</mn><mi>ℏ</mi></mfrac><mfrac><mi>d</mi><mrow><mi>d</mi><mi>t</mi></mrow></mfrac><mrow><mo>(</mo><mfrac><mrow><mi>d</mi><mi>E</mi></mrow><mrow><mi>d</mi><mi>k</mi></mrow></mfrac><mo>)</mo></mrow><mo>&#x3D;</mo><mfrac><mn>1</mn><mi>ℏ</mi></mfrac><mfrac><mrow><msup><mi>d</mi><mn>2</mn></msup><mi>E</mi></mrow><mrow><mi>d</mi><msup><mi>k</mi><mn>2</mn></msup></mrow></mfrac><mfrac><mrow><mi>d</mi><mi>k</mi></mrow><mrow><mi>d</mi><mi>t</mi></mrow></mfrac></math><br>又因为 <math><mi>F</mi><mo>&#x3D;</mo><mfrac><mrow><mi>d</mi><mrow><mo>(</mo><mi>ℏ</mi><mi>k</mi><mo>)</mo></mrow></mrow><mrow><mi>d</mi><mi>t</mi></mrow></mfrac><mo>&#x3D;</mo><mi>ℏ</mi><mfrac><mrow><mi>d</mi><mi>k</mi></mrow><mrow><mi>d</mi><mi>t</mi></mrow></mfrac></math>，所以：<br><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>a</mi><mo>&#x3D;</mo><mfrac><mi>F</mi><mrow><msup><mi>ℏ</mi><mn>2</mn></msup><mo>&#x2F;</mo><mrow><mo>(</mo><msup><mi>d</mi><mn>2</mn></msup><mi>E</mi><mo>&#x2F;</mo><mi>d</mi><msup><mi>k</mi><mn>2</mn></msup><mo>)</mo></mrow></mrow></mfrac><mo>&#x3D;</mo><mfrac><mi>F</mi><mrow><msubsup><mi>m</mi><mi>n</mi><mo>∗</mo></mrow></mfrac></math></p><p>由此定义有效质量 <math><msup><mi>m</mi><mo>∗</mo></msup></math> 为：<br><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mn>1</mn><mrow><msubsup><mi>m</mi><mi>n</mi><mo>∗</mo></msubsup></mrow></mfrac><mo>&#x3D;</mo><mfrac><mn>1</mn><mrow><msup><mi>ℏ</mi><mn>2</mn></msup></mrow></mfrac><mfrac><mrow><msup><mi>d</mi><mn>2</mn></msup><mi>E</mi><mo stretchy="false">(</mo><mi>k</mi><mo stretchy="false">)</mo></mrow><mrow><mi>d</mi><msup><mi>k</mi><mn>2</mn></msup></mrow></mfrac></math></p><div class="note info"><p><strong>物理意义</strong>：<br>有效质量 <math><msup><mi>m</mi><mo>∗</mo></msup></math> 并不是电子的惯性质量，它是一个概括了晶体内部<strong>周期性势场</strong>对电子作用力的物理量。它将复杂的内力问题简化为一个简单的“质量”问题，使得我们可以像处理自由电子一样处理晶体中电子的运动，只需把惯性质量 m 替换为有效质量 <math><msup><mi>m</mi><mo>∗</mo></msup></math> 即可。</p><ul><li>在<strong>导带底</strong>，E(k) 曲线是向上开口的抛物线，<math><msup><mi>d</mi><mn>2</mn></msup><mi>E</mi><mo>&#x2F;</mo><mi>d</mi><msup><mi>k</mi><mn>2</mn></msup><mo>&gt;</mo><mn>0</mn></math>，所以电子的有效质量 <math><msubsup><mi>m</mi><mi>n</mi><mo>∗</mo></msubsup></math> 为<strong>正值</strong>。</li><li>在<strong>价带顶</strong>，E(k) 曲线是向下开口的抛物线，<math><msup><mi>d</mi><mi>d</mi><msup><mi>k</mi><mn>2</mn></msup><mo>&lt;</mo><mn>0</mn></math>，所以电子的有效质量为<strong>负值</strong>。为方便描述，我们引入<strong>空穴</strong>的概念，空穴携带电荷 +q，其有效质量 <math><msubsup><mi>m</mi><mi>p</mi><mo>∗</mo></msubsup></math> 为<strong>正值</strong>。</li></ul></div><div class="table-container"><table><tr bgcolor=#F7F7F7><th colspan="3">能带宽窄对有效质量的影响</th></tr><tr align="center"><td>能带窄 →</td><td>能带“胖” →</td><td>有效质量大</td></tr><tr align="center"><td>能带宽 →</td><td>能带“瘦” →</td><td>有效质量小</td></tr></table></div><h3 id="3-例题"><a href="#3-例题" class="headerlink" title="3.例题"></a>3.例题</h3><details class="fold green" ><summary>判断题 宽带中电子的有效质量比窄带中的小。（点击展开查看答案）</summary><div class='fold-content'><p>&#x2705; 对的</p><p>宽带说明这个能带长得又高又瘦的，窄带说明这个能带又矮又胖的，结合前文的结论，宽带中电子的有效质量小于窄带的。</p></div></details><h2 id="4-本征半导体的导电机构——空穴"><a href="#4-本征半导体的导电机构——空穴" class="headerlink" title="4.本征半导体的导电机构——空穴"></a>4.本征半导体的导电机构——空穴</h2><h3 id="4-1-空穴特征"><a href="#4-1-空穴特征" class="headerlink" title="4.1.空穴特征"></a>4.1.空穴特征</h3><div class="note yellow"><ul><li>假想粒子；</li><li>带一个单位的正电荷 +e；</li><li>有效质量为正，数值大小与价带顶电子相同<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><msubsup><mi>m</mi><mi>p</mi><mo>*</mo></msubsup><mo>&#x3D;</mo><msubsup><mi>m</mi><mi>n</mi><mo>*</mo></msubsup></math>。</li></ul></div><h3 id="4-2-两种导电机构"><a href="#4-2-两种导电机构" class="headerlink" title="4.2.两种导电机构"></a>4.2.两种导电机构</h3><div class="note yellow"><p><strong>半导体中有两种载流子</strong></p><ul><li>空穴</li><li>电子</li></ul><p><strong>金属只有一种载流子</strong></p></div><h3 id="本征激发"><a href="#本征激发" class="headerlink" title="本征激发"></a>本征激发</h3><p><img   lazyload src="/assets/images/Meow-Loading.webp" data-lazy-src="/./semiconductor-physics-study-notes-electron-states-in-semiconductors/7.png"  alt="本征激发示意" title="本征激发示意"></p><div class="note yellow"><p>一定温度下，价带激发电子到导带，价带顶出现空状态。</p><div align="center"><b>↓↓</b></div>本征激发就算价带电子激发成为导带电子的过程。</div><p><strong>几个概念：</strong></p><div class="note purple"><ul><li>导带底 E<sub>c</sub> ：导带电子的最低能量</li><li>价带顶 E<sub>v</sub> ：价带电子的最高能量</li><li>禁带宽度： E<sub>g</sub> &#x3D; E<sub>c</sub> - E<sub>v</sub></li></ul></div><h3 id="4-例题"><a href="#4-例题" class="headerlink" title="4.例题"></a>4.例题</h3><details class="fold blue" ><summary>简答题 什么是本征激发？试定性说明之 （点击展开查看答案）</summary><div class='fold-content'><p>在一定温度下，价带电子获得足够的能量（&gt;&#x3D;E<sub>g</sub>）,被激发到导带成为导电电子；这一过程我们就叫做本征激发。</p><p><img   lazyload src="/assets/images/Meow-Loading.webp" data-lazy-src="/./semiconductor-physics-study-notes-electron-states-in-semiconductors/8.png"  alt="本征激发示意图二" title="本征激发示意图二"></p></div></details><h2 id="5-回旋共振"><a href="#5-回旋共振" class="headerlink" title="5.回旋共振"></a>5.回旋共振</h2><p>一种实验，用于<strong>测出</strong>载流子的<strong>有效质量</strong>并依据此推出半导体的能带结构。</p><h2 id="6-常见半导体的能带结构"><a href="#6-常见半导体的能带结构" class="headerlink" title="6.常见半导体的能带结构"></a>6.常见半导体的能带结构</h2><div class="table-container"><table><thead><tr><th align="center">特性</th><th align="center">重空穴</th><th align="center">轻空穴</th></tr></thead><tbody><tr><td align="center"><strong>能带形状</strong></td><td align="center">价带顶处更“平坦”、曲率小</td><td align="center">价带顶处更“陡峭”、曲率大</td></tr><tr><td align="center"><strong>有效质量</strong></td><td align="center"><strong>大</strong></td><td align="center"><strong>小</strong></td></tr><tr><td align="center"><strong>迁移率</strong></td><td align="center"><strong>低</strong></td><td align="center"><strong>高</strong></td></tr><tr><td align="center"><strong>数量</strong></td><td align="center">绝大多数</td><td align="center">少数</td></tr></tbody></table></div><h3 id="6-1-Si、Ge的能带结构特点"><a href="#6-1-Si、Ge的能带结构特点" class="headerlink" title="6.1.Si、Ge的能带结构特点"></a>6.1.Si、Ge的能带结构特点</h3><p>轻重空穴，间接带隙</p><h3 id="6-2-GaAs的能带结构特点"><a href="#6-2-GaAs的能带结构特点" class="headerlink" title="6.2.GaAs的能带结构特点"></a>6.2.GaAs的能带结构特点</h3><p>轻重空穴，第三能带，直接带隙</p><h3 id="6-3-直接间隙和间接间隙"><a href="#6-3-直接间隙和间接间隙" class="headerlink" title="6.3.直接间隙和间接间隙"></a>6.3.直接间隙和间接间隙</h3><div class="table-container"><table><thead><tr><th align="left">特性</th><th align="left"><strong>间接带隙</strong></th><th align="left"><strong>直接带隙</strong></th></tr></thead><tbody><tr><td align="left"><strong>k空间位置</strong></td><td align="left">价带顶和导带底在不同k点</td><td align="left">价带顶和导带顶在同一k点</td></tr><tr><td align="left"><strong>光跃迁</strong></td><td align="left"><strong>必须有声子参与</strong>（二阶过程）</td><td align="left"><strong>只需光子参与</strong>（二阶过程）</td></tr><tr><td align="left"><strong>发光效率</strong></td><td align="left"><strong>极低</strong></td><td align="left"><strong>非常高</strong></td></tr><tr><td align="left"><strong>典型材料</strong></td><td align="left"><strong>硅、锗</strong></td><td align="left">砷化镓、氮化镓、磷化铟</td></tr></tbody></table></div>]]></content>
    
    
    <summary type="html">本篇笔记整理了《半导体物理学》第一章的核心内容，从半导体的晶格结构（金刚石型、闪锌矿型）出发，阐述了固体能带理论的形成，并初步探讨了半导体中电子的运动状态，为后续学习奠定基础。</summary>
    
    
    
    <category term="笔记" scheme="http://blog.pavilioncat.com/categories/%E7%AC%94%E8%AE%B0/"/>
    
    
    <category term="半导体物理" scheme="http://blog.pavilioncat.com/tags/%E5%8D%8A%E5%AF%BC%E4%BD%93%E7%89%A9%E7%90%86/"/>
    
  </entry>
  
  <entry>
    <title>《模拟电子技术基础》学习笔记——绪论&amp;半导体基础知识</title>
    <link href="http://blog.pavilioncat.com/fundamentals-of-analog-electronic-circuits-study-notes-introduction-and-semiconductor-basics/"/>
    <id>http://blog.pavilioncat.com/fundamentals-of-analog-electronic-circuits-study-notes-introduction-and-semiconductor-basics/</id>
    <published>2025-09-28T13:06:10.000Z</published>
    <updated>2025-12-22T15:44:12.000Z</updated>
    
    <content type="html"><![CDATA[<div class="note pink"><p>教材：童诗白、华成英《模拟电子技术基础》（第六版）高等教育出版社</p></div><p><em>本文乃阿猫学余笔录，拙作难免疏漏，恳请不吝指正</em></p><div class="title-tag">绪论</div><h3 id="一、模拟信号与模拟电路"><a href="#一、模拟信号与模拟电路" class="headerlink" title="一、模拟信号与模拟电路"></a>一、模拟信号与模拟电路</h3><h4 id="1-电子电路中信号的分类"><a href="#1-电子电路中信号的分类" class="headerlink" title="1.电子电路中信号的分类"></a>1.电子电路中信号的分类</h4><details class="fold blue" open><summary>信号类型对比</summary><div class='fold-content'><ul><li>数字信号：在时间和数值上有离散性，u和i的变化在时间上不连续，总是发生在离散的瞬间，且它们的数值是一个最小量值的整数倍，并以此倍数作为数字信号的数值。</li><li>模拟信号：在时间和数值上有连续性，对于任何时间值t都有确定的函数值u和i，并且u和i的幅值是连续取值的。 （大多数物理量为模拟信号，非电物理量可以通过各种传感器变换成电信号）</li></ul></div></details><h4 id="2-模拟电路"><a href="#2-模拟电路" class="headerlink" title="2.模拟电路"></a>2.模拟电路</h4><div class="note light"><p>模拟电路：对模拟信号进行处理的电路。最基本的处理是对信号的放大，有功能和性能各异的放大电路，其他模拟电路多以放大电路为基础</p></div><div class="tabs" id="模拟电路相关概念"><ul class="tabs-nav"><button type="button" class="tab  active" data-href="模拟电路相关概念-1">数字化处理</button><button type="button" class="tab " data-href="模拟电路相关概念-2">放大本质</button><button type="button" class="tab " data-href="模拟电路相关概念-3">有源元件</button></ul><div class="tabs-content"><div class="tabs-item-content active" id="模拟电路相关概念-1"><p>只有将模拟信号放大到足够大，才能够进行数字化处理；而只有将处理好的数字信号转换为模拟信号并进行功率放大，才能驱动某些负载。</p></div><div class="tabs-item-content" id="模拟电路相关概念-2"><p>放大的本质：能量的控制。</p></div><div class="tabs-item-content" id="模拟电路相关概念-3"><p>有源元件：能够控制能量的元件。如晶体管、场效应管。</p></div><div class="tabs-to-top"><button type="button" aria-label="scroll to top"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 15l6 -6l6 6" /></svg></button></div></div></div><h3 id="二、电子系统的组成"><a href="#二、电子系统的组成" class="headerlink" title="二、电子系统的组成"></a>二、电子系统的组成</h3><p><img   lazyload src="/assets/images/Meow-Loading.webp" data-lazy-src="/./fundamentals-of-analog-electronic-circuits-study-notes-introduction-and-semiconductor-basics/1.png"  alt="电子系统的组成" title="电子系统的组成"></p><h3 id="三、模拟电子技术基础课程的特点"><a href="#三、模拟电子技术基础课程的特点" class="headerlink" title="三、模拟电子技术基础课程的特点"></a>三、模拟电子技术基础课程的特点</h3><h4 id="1-工程性"><a href="#1-工程性" class="headerlink" title="1.工程性"></a>1.工程性</h4><details class="fold purple" open><summary>工程性特点</summary><div class='fold-content'><ul><li>实际工程需要证明其可行性。强调定性分析；实际工程满足需求的方案是多”解”的。方案比较、利弊分析。</li><li>实际工程在满足基本性能指标的前提下总是容许存在一定的误差范围的。定量分析为”估算”。</li><li>近似分析要”合理”。抓主要矛盾和矛盾的主要方面，特别要注意近似条件。</li><li>电子电路归根结底是电路。不同条件下，构造半导体器件不同的模型，将它代入电子电路，电子电路便成为一般电路。</li></ul></div></details><h4 id="2-实践性"><a href="#2-实践性" class="headerlink" title="2.实践性"></a>2.实践性</h4><div class="note green"><ul><li>EDA（Electronic Design Automation）软件的应用方法</li><li>实验报告的写作方法</li></ul></div><div class="title-tag">半导体基础知识</div><h3 id="一、本征半导体"><a href="#一、本征半导体" class="headerlink" title="一、本征半导体"></a>一、本征半导体</h3><h4 id="什么是半导体？什么是本征半导体？"><a href="#什么是半导体？什么是本征半导体？" class="headerlink" title="什么是半导体？什么是本征半导体？"></a>什么是半导体？什么是本征半导体？</h4><div class="tabs" id="半导体分类"><ul class="tabs-nav"><button type="button" class="tab  active" data-href="半导体分类-1">导体</button><button type="button" class="tab " data-href="半导体分类-2">绝缘体</button><button type="button" class="tab " data-href="半导体分类-3">半导体</button></ul><div class="tabs-content"><div class="tabs-item-content active" id="半导体分类-1"><p>导体——铁、铝、铜等金届元素等低价元素，其最外层电子在外电场作用下很容易产生定向移动，形成电流。</p></div><div class="tabs-item-content" id="半导体分类-2"><p>绝缘体——情性气体、橡胶等，其原子的最外层电子受原子核的束缚力很强，只有在外电场强到一定程度时才可能导电。</p></div><div class="tabs-item-content" id="半导体分类-3"><p>半导体——硅（Si）、锗（Ge），均为四价元素，它们原子的最外层电子受原子核的束缚力介于导体与绝缘体之间。</p></div><div class="tabs-to-top"><button type="button" aria-label="scroll to top"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 15l6 -6l6 6" /></svg></button></div></div></div><div class="note yellow"><p><strong>本征半导体是纯净（无杂质）的晶体结构的半导体。</strong></p></div><h4 id="本征半导体的结构"><a href="#本征半导体的结构" class="headerlink" title="本征半导体的结构"></a>本征半导体的结构</h4><p><img   lazyload src="/assets/images/Meow-Loading.webp" data-lazy-src="/./fundamentals-of-analog-electronic-circuits-study-notes-introduction-and-semiconductor-basics/2.jpg"  alt="本征半导体的结构" title="本征半导体的结构"></p><details class="fold blue" open><summary>导电特性</summary><div class='fold-content'><ul><li>自由电子与空穴发生碰撞同时消失，称为<strong>复合</strong>。</li><li>在一定温度下，自由电子与空穴对的浓度一定（动态平衡）；温度升高，热运动加剧，挣脱共价键的电子增多，自由电子与空穴对的浓度加大。</li><li><strong>运载电荷的粒子称为载流子。</strong></li><li>外加电场时，带负电的自由电子和带正电的空穴均参与导电，且运动方向相反。由于载流子数目很少，故导电性很差。</li><li>温度升高，热运动加剧，载流子浓度增大，导电性增强。<strong>热力学温度0K时不导电。</strong></li></ul></div></details><div class="note pink"><p><strong>为什么要制造导电性很弱的本征半导体：因为要使得导电性可控。</strong></p></div><h3 id="二、杂质半导体"><a href="#二、杂质半导体" class="headerlink" title="二、杂质半导体"></a>二、杂质半导体</h3><div class="note light"><p>为了使本征半导体导电性可控，我们就需要向本征半导体掺入一定的杂质。</p></div><h4 id="N型半导体"><a href="#N型半导体" class="headerlink" title="N型半导体"></a>N型半导体</h4><p>在本征半导体中掺杂五价元素，一般是掺入磷（P）。</p><p><img   lazyload src="/assets/images/Meow-Loading.webp" data-lazy-src="/./fundamentals-of-analog-electronic-circuits-study-notes-introduction-and-semiconductor-basics/3.jpg"  alt="N型半导体" title="N型半导体"></p><div class="note pink"><p>多子：多数载流子；少子：少数载流子。</p></div><h4 id="P型半导体"><a href="#P型半导体" class="headerlink" title="P型半导体"></a>P型半导体</h4><p>在本征半导体中掺杂三价元素，一般是掺入硼（B）。</p><p><img   lazyload src="/assets/images/Meow-Loading.webp" data-lazy-src="/./fundamentals-of-analog-electronic-circuits-study-notes-introduction-and-semiconductor-basics/4.jpg"  alt="P型半导体" title="P型半导体"></p><details class="fold green" open><summary>P型半导体特性</summary><div class='fold-content'><ul><li><strong>空位呈电中性</strong></li><li>温度变化时，载流子数目变化，假如温度增加，热运动加剧，就会有更多价电子挣脱共价键的束缚。</li><li>温度变化时，少子与多子变化的数目相同，因为当一个价电子挣脱共价键的束缚，就会出现一个空穴。</li><li>温度变化时，少子与多子浓度的变化不同，假设原来多子有100个，少子有2个，这时倘若有两个价电子挣脱共价键的束缚，则多子变为102个，少子变为4个，多子增加了2%，少子增加了100%，显然是不相同的</li></ul></div></details><h3 id="三、PN结的形成及其单向导电性"><a href="#三、PN结的形成及其单向导电性" class="headerlink" title="三、PN结的形成及其单向导电性"></a>三、PN结的形成及其单向导电性</h3><h4 id="1-PN结的形成"><a href="#1-PN结的形成" class="headerlink" title="1.PN结的形成"></a>1.PN结的形成</h4><div class="note yellow"><p>物质因浓度差而产生的运动称为扩散运动。气体、液体、固态均有之。</p></div><p><img   lazyload src="/assets/images/Meow-Loading.webp" data-lazy-src="/./fundamentals-of-analog-electronic-circuits-study-notes-introduction-and-semiconductor-basics/5.jpg"  alt="PN结的形成" title="PN结的形成"></p><div class="note purple"><ul><li>由于扩散运动使P区与N区的交界面缺少多数载流子，形成内电场（N区显正电，P区显负电），从而阻止扩散运动的进行。内电场使空穴从N区向P区、自由电子从P区向N区运动。</li><li>扩散运动是多子的运动。</li></ul></div><p><img   lazyload src="/assets/images/Meow-Loading.webp" data-lazy-src="/./fundamentals-of-analog-electronic-circuits-study-notes-introduction-and-semiconductor-basics/6.jpg"  alt="PN结的形成图2" title="PN结的形成图2"></p><div class="note purple"><ul><li>空间电荷区又称为耗尽层。</li><li>漂移运动是少子的运动。</li></ul></div><h4 id="2-PN结的单向导电性"><a href="#2-PN结的单向导电性" class="headerlink" title="2.PN结的单向导电性"></a>2.PN结的单向导电性</h4><h5 id="2-1-PN结加正向电压"><a href="#2-1-PN结加正向电压" class="headerlink" title="2.1.PN结加正向电压"></a>2.1.PN结加正向电压</h5><p><img   lazyload src="/assets/images/Meow-Loading.webp" data-lazy-src="/./fundamentals-of-analog-electronic-circuits-study-notes-introduction-and-semiconductor-basics/7.jpg"  alt="PN结加正向电压" title="PN结加正向电压"></p><div class="note red"><p>限流电阻R很重要，如果电流过大，就会使得PN结的温升过高，进而烧坏PN结。</p></div><h5 id="2-2-PN结加反向电压"><a href="#2-2-PN结加反向电压" class="headerlink" title="2.2.PN结加反向电压"></a>2.2.PN结加反向电压</h5><p><img   lazyload src="/assets/images/Meow-Loading.webp" data-lazy-src="/./fundamentals-of-analog-electronic-circuits-study-notes-introduction-and-semiconductor-basics/8.jpg"  alt="PN结加反向电压" title="PN结加反向电压"></p><div class="note light"><p>因为漂移运动是少子的运动，即使全部的少子都参与这个运动，电流仍然很小，故可以近似的认为其截止。</p></div><h4 id="3-PN结的电容效应"><a href="#3-PN结的电容效应" class="headerlink" title="3.PN结的电容效应"></a>3.PN结的电容效应</h4><h5 id="3-1-势垒电容"><a href="#3-1-势垒电容" class="headerlink" title="3.1.势垒电容"></a>3.1.势垒电容</h5><p><img   lazyload src="/assets/images/Meow-Loading.webp" data-lazy-src="/./fundamentals-of-analog-electronic-circuits-study-notes-introduction-and-semiconductor-basics/9.jpg"  alt="势垒电容" title="势垒电容"></p><h5 id="3-2-扩散电容"><a href="#3-2-扩散电容" class="headerlink" title="3.2.扩散电容"></a>3.2.扩散电容</h5><p>PN结外加的正向电压变化时，在扩散路程中载流子的浓度及其梯度均有变化，也有电荷的积累和释放的过程，其等效电容称为扩散电容Cd 。</p><h5 id="3-3-结电容"><a href="#3-3-结电容" class="headerlink" title="3.3.结电容"></a>3.3.结电容</h5><div class="note pink"><p>Cj&#x3D;Cb+Cd</p><p>若PN结外加电压频率高到一定程度，则失去单向导电性！结电容不是常量！</p></div>]]></content>
    
    
    <summary type="html">《模拟电子技术基础》学习笔记：系统梳理绪论与半导体基础知识，涵盖模拟信号、本征/杂质半导体、PN结形成原理及其单向导电性。</summary>
    
    
    
    <category term="笔记" scheme="http://blog.pavilioncat.com/categories/%E7%AC%94%E8%AE%B0/"/>
    
    
    <category term="模拟电子电路基础" scheme="http://blog.pavilioncat.com/tags/%E6%A8%A1%E6%8B%9F%E7%94%B5%E5%AD%90%E7%94%B5%E8%B7%AF%E5%9F%BA%E7%A1%80/"/>
    
  </entry>
  
  <entry>
    <title>Windows实现端口转发</title>
    <link href="http://blog.pavilioncat.com/windows-port-netsh/"/>
    <id>http://blog.pavilioncat.com/windows-port-netsh/</id>
    <published>2025-01-14T18:32:21.000Z</published>
    <updated>2025-12-22T15:44:12.000Z</updated>
    
    <content type="html"><![CDATA[<h2 id="查看已有的转发规则"><a href="#查看已有的转发规则" class="headerlink" title="查看已有的转发规则"></a>查看已有的转发规则</h2><div class="code-container" code-lang="Plaintext"><div class="codebox"><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">netsh interface portproxy show all</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div><h2 id="新增转发规则"><a href="#新增转发规则" class="headerlink" title="新增转发规则"></a>新增转发规则</h2><div class="code-container" code-lang="Plaintext"><div class="codebox"><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">netsh interface portproxy add v4tov4 listenport=2405 listenaddress=192.168.3.6 connectport=10010 connectaddress=112.11.36.130 protocol=tcp</span><br><span class="line"># （本机本地侦听IP192.168.3.6和侦听端口2405）+（远端服务器IP112.11.36.130+端口10010）</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div><h2 id="删除转发规则"><a href="#删除转发规则" class="headerlink" title="删除转发规则"></a>删除转发规则</h2><div class="code-container" code-lang="Plaintext"><div class="codebox"><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">netsh interface portproxy delete v4tov4 listenport=2405 listenaddress=192.168.3.6</span><br><span class="line">#（本机本地侦听IP192.168.3.6和侦听端口2405）+（远端服务器IP112.11.36.130+端口10010）</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div>]]></content>
    
    
    <summary type="html">Windows下使用netsh命令实现端口转发的完整教程。包含查看、添加、删除端口转发规则的详细命令与示例，一学就会。</summary>
    
    
    
    <category term="教程" scheme="http://blog.pavilioncat.com/categories/%E6%95%99%E7%A8%8B/"/>
    
    
    <category term="Windows" scheme="http://blog.pavilioncat.com/tags/Windows/"/>
    
    <category term="网络" scheme="http://blog.pavilioncat.com/tags/%E7%BD%91%E7%BB%9C/"/>
    
  </entry>
  
  <entry>
    <title>基于Sunshine+Moonlight方案实现串流（超级详细）</title>
    <link href="http://blog.pavilioncat.com/ultimate-sunshine-moonlight-streaming-setup-guide/"/>
    <id>http://blog.pavilioncat.com/ultimate-sunshine-moonlight-streaming-setup-guide/</id>
    <published>2025-01-05T11:17:00.000Z</published>
    <updated>2025-12-22T15:44:12.000Z</updated>
    
    <content type="html"><![CDATA[<blockquote><p>本文写了5000余字，大抵是全网最详细的教程之一吧</p></blockquote><p><strong>串流（Streaming）</strong> 是指在不需要先下载整个文件的情况下，通过互联网或局域网实时传输数据，通常是音频、视频或其他多媒体内容。</p><div class="tabs" id="主流串流方案"><ul class="tabs-nav"><button type="button" class="tab  active" data-href="主流串流方案-1">ToDesk等方案</button><button type="button" class="tab " data-href="主流串流方案-2">Steam Link</button><button type="button" class="tab " data-href="主流串流方案-3">Microsoft Remote Desktop</button><button type="button" class="tab " data-href="主流串流方案-4">Sunshine + Moonlight</button><button type="button" class="tab " data-href="主流串流方案-5">Parsec</button></ul><div class="tabs-content"><div class="tabs-item-content active" id="主流串流方案-1"><p>ToDesk，向日葵，Raylink：经过服务商中转的远程桌面，适合外网访问，但免费用户画质和延迟较差</p></div><div class="tabs-item-content" id="主流串流方案-2"><p>Steam Link：Steam 自带软件，配置超级简单但不稳定，适合内网打游戏</p></div><div class="tabs-item-content" id="主流串流方案-3"><p>Microsoft Remote Desktop：Windows 自带应用，配置相对简单，适合内网办公，缺点是对显卡调度有问题</p></div><div class="tabs-item-content" id="主流串流方案-4"><p>Sunshine + Moonlight：网友一致好评的方案，经过多代更新，配置不再那么复杂</p></div><div class="tabs-item-content" id="主流串流方案-5"><p>Parsec：阿猫没用过，但据说键盘鼠标兼容性不好</p></div><div class="tabs-to-top"><button type="button" aria-label="scroll to top"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 15l6 -6l6 6" /></svg></button></div></div></div><h2 id="Sunshine-Moonlight-的优势"><a href="#Sunshine-Moonlight-的优势" class="headerlink" title="Sunshine + Moonlight 的优势"></a>Sunshine + Moonlight 的优势</h2><p>Sunshine 和 Moonlight 开源方案具有以下优势：</p><div class="note blue"><p><strong>超低延迟：</strong> 通过优化编码、传输协议和输入响应，Sunshine + Moonlight 提供低延迟的远程桌面和游戏体验，在内网环境下，延迟只有<strong>几ms</strong>。</p></div><div class="note green"><p><strong>高画质：</strong> 支持高分辨率和高帧率，用户可以享受近乎本地的画质。</p></div><div class="note purple"><p><strong>跨平台支持：</strong> 无论是 Windows、Linux，还是 Android 和 iOS，Moonlight 都能支持多种平台，提供灵活的串流体验。</p></div><p>通过这些技术，Sunshine + Moonlight 实现了高效、低延迟的远程桌面和游戏串流，使用户能够在各种设备上享受图形密集型应用和游戏，而无需本地高性能硬件。</p><h2 id="环境要求"><a href="#环境要求" class="headerlink" title="环境要求"></a>环境要求</h2><ul><li>一台 Windows 或 Linux 设备作为 Sunshine 服务器。</li><li>一台支持 Moonlight 客户端的设备（如 PC、智能手机、平板电脑等）。</li><li>网络连接要求：服务器和客户端需要在同一网络上，或者客户端需要能够访问服务器（如果使用互联网访问，需要配置端口转发或者拥有公网直接访问）。</li></ul><h2 id="一、安装-Sunshine（流媒体服务器）"><a href="#一、安装-Sunshine（流媒体服务器）" class="headerlink" title="一、安装 Sunshine（流媒体服务器）"></a>一、安装 Sunshine（流媒体服务器）</h2><h3 id="1-下载-Sunshine："><a href="#1-下载-Sunshine：" class="headerlink" title="1.下载 Sunshine："></a>1.下载 Sunshine：</h3><div class="button-list">          <div class="button-item">            <a href="https://github.com/LizardByte/Sunshine" class="normal" alt=Sunshine GitHub页面 target="_blank">              <div class="button-item-icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M17 22v-2" /><path d="M9 15l6 -6" /><path d="M11 6l.463 -.536a5 5 0 0 1 7.071 7.072l-.534 .464" /><path d="M13 18l-.397 .534a5.068 5.068 0 0 1 -7.127 0a4.972 4.972 0 0 1 0 -7.071l.524 -.463" /><path d="M20 17h2" /><path d="M2 7h2" /><path d="M7 2v2" /></svg></div>              <div class="button-item-info">                <div class="button-item-name">Sunshine GitHub页面</div>                <div class="button-item-desc">访问 Sunshine 的 GitHub 页面</div>              </div>            </a>          </div></div><p>下载最新的发行版，注意要选择 <code>Latest</code> 版本的（注意区别 <code>Pre-release</code> 的测试版），Windows选择 <code>sunshine-windows-installer.exe</code> 进行下载。</p><div class="note yellow"><p>若无法访问GitHub，可以使用如下链接进行下载适用Windows的最新版本或特定版本：</p><p><a href="https://wwqu.lanzouq.com/b00y9s33nc">蓝奏云盘</a> 密码：519h</p></div><h3 id="2-安装-Sunshine："><a href="#2-安装-Sunshine：" class="headerlink" title="2.安装 Sunshine："></a>2.安装 Sunshine：</h3><div class="tabs" id="安装sunshine"><ul class="tabs-nav"><button type="button" class="tab  active" data-href="安装sunshine-1">Windows</button><button type="button" class="tab " data-href="安装sunshine-2">Linux</button></ul><div class="tabs-content"><div class="tabs-item-content active" id="安装sunshine-1"><ul><li>下载 <code>.exe</code> 安装文件，双击安装（保险起见，请右键使用管理员权限运行）。</li><li>按照安装向导完成安装。</li><li>安装过程中，请注意安装路径不能存在中文，推荐安装所有控件。</li><li>新版本 Sunshine 的 Audio Sink 和 Virtual Sink 选项不用再手动设置</li><li>新版本 Sunshine 在安装时会一并安装手柄驱动( ViGEmBus )与Steam虚拟声卡( Steam Audio Drivers )，因此不用像老版本一样手动安装</li><li>安装完成,可以吃灰了(bushi)</li></ul><details class="fold pink" ><summary>注意事项</summary><div class='fold-content'><p><strong>注意:</strong> 若需在串流时使用其他声卡(如物理声卡)，需禁用 Install Steam Audio Drivers 选项以防止 Sunshine 自动安装并启用Steam虚拟声卡</p></div></details></div><div class="tabs-item-content" id="安装sunshine-2"><ul><li>从源代码编译或下载预编译的二进制文件。</li><li>如果你选择使用源代码安装：</li></ul><div class="code-container" code-lang="Bash"><div class="codebox"><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">clone</span> https://github.com/LizardByte/Sunshine.git</span><br><span class="line"><span class="built_in">cd</span> Sunshine</span><br><span class="line">make</span><br><span class="line"><span class="built_in">sudo</span> make install</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div></div><div class="tabs-to-top"><button type="button" aria-label="scroll to top"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 15l6 -6l6 6" /></svg></button></div></div></div><h3 id="3-启动-Sunshine："><a href="#3-启动-Sunshine：" class="headerlink" title="3.启动 Sunshine："></a>3.启动 Sunshine：</h3><p>在安装完成后，启动 Sunshine。</p><ul><li>在 Windows 上，通常可以在开始菜单中找到 Sunshine 并启动它。</li><li>在 Linux 上，使用终端命令启动：</li></ul><div class="code-container" code-lang="Bash"><div class="codebox"><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sunshine</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div><p>Windows中 Sunshine 会在后台运行并在本机地址上开启一个端口作为配置页面，点击右下角状态栏的 Sunshine 图标，在弹出菜单中选择 <code>Open</code>，随后浏览器会跳转至 <code><a href="https://localhost:47990/">https://localhost:47990</a></code> 进入管理页面。</p><p>由于 Sunshine 强制 HTTPS 访问但缺少证书，浏览器会提示非私密连接或你的连接不是专用连接，点击 <code>详情</code> 或 <code>高级</code> 后，选择 <code>继续前往localhost（不安全）</code>，大多数浏览器以后访问这个页面时会自动跳过这个页面。</p><h3 id="4-配置-Sunshine："><a href="#4-配置-Sunshine：" class="headerlink" title="4.配置 Sunshine："></a>4.配置 Sunshine：</h3><p>第一次进入 Sunshine 页面之后需要注册，然后使用账号密码登录该网页。（一定要记住这个账户密码，不要过分相信edge等浏览器的记住密码，阿猫有被坑过，如果忘记密码，请移步后文）</p><details class="fold light" ><summary>配置步骤</summary><div class='fold-content'><ol><li><strong>选择你要流式传输的应用或游戏：</strong><ul><li>Sunshine 默认会识别并列出本机安装的游戏和应用程序。</li><li>你可以选择某个特定的程序来进行流媒体传输。</li></ul></li><li><strong>启用硬件加速：</strong><ul><li>在 Sunshine 设置中，启用硬件加速以减少延迟并提高性能。大多数现代显卡（如 NVIDIA 和 AMD）都支持硬件编码。</li><li>如果你的显卡支持 NVIDIA 的 NVENC 或 AMD 的 VCE，可以在 Sunshine 的设置中启用它们。</li></ul></li><li><strong>设置流媒体选项：</strong><ul><li>在 Sunshine 设置中，可以设置编码类型（H.264 或 H.265），码率（建议设置为 10Mbps 或更高），以及音频采样率（例如 48 kHz）。</li></ul></li><li><strong>配置网络：</strong><ul><li>确保你的防火墙允许 Sunshine 服务器通过的端口（默认端口为 47984）。</li><li>如果需要实现广域网远程串流，请移步至后文<strong>远程串流</strong>。</li></ul></li></ol></div></details><div class="note light"><p>主机需要连接显示器才能正常串流，如果是笔记本则不能合盖。要在无显示器状态下串流，可以买一个显卡欺骗器接在电脑上，效果相当于外接一台显示器。或者直接使用虚拟显示器。</p></div><p>如果有需求，阿猫更推荐直接使用虚拟显示器，例如Virtual Display Driver，支持模拟 640 x 480 至 7680 x 4320 （8K） 的分辨率，刷新率包括 60hz、75hz、90hz、120hz、144hz、165hz、240hz、480hz 和 500hz，同时支持HDR。</p><div class="button-list">          <div class="button-item">            <a href="https://github.com/itsmikethetech/Virtual-Display-Driver/releases" class="normal" alt=Virtual Display Driver target="_blank">              <div class="button-item-icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M17 22v-2" /><path d="M9 15l6 -6" /><path d="M11 6l.463 -.536a5 5 0 0 1 7.071 7.072l-.534 .464" /><path d="M13 18l-.397 .534a5.068 5.068 0 0 1 -7.127 0a4.972 4.972 0 0 1 0 -7.071l.524 -.463" /><path d="M20 17h2" /><path d="M2 7h2" /><path d="M7 2v2" /></svg></div>              <div class="button-item-info">                <div class="button-item-name">Virtual Display Driver</div>                <div class="button-item-desc">支持HDR的虚拟显示器驱动</div>              </div>            </a>          </div></div><p>还有一个更易于使用的图形化的驱动是ParsecVDisplay：</p><div class="button-list">          <div class="button-item">            <a href="https://github.com/nomi-san/parsec-vdd/releases" class="normal" alt=ParsecVDisplay target="_blank">              <div class="button-item-icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M17 22v-2" /><path d="M9 15l6 -6" /><path d="M11 6l.463 -.536a5 5 0 0 1 7.071 7.072l-.534 .464" /><path d="M13 18l-.397 .534a5.068 5.068 0 0 1 -7.127 0a4.972 4.972 0 0 1 0 -7.071l.524 -.463" /><path d="M20 17h2" /><path d="M2 7h2" /><path d="M7 2v2" /></svg></div>              <div class="button-item-info">                <div class="button-item-name">ParsecVDisplay</div>                <div class="button-item-desc">图形化虚拟显示器驱动，不支持HDR</div>              </div>            </a>          </div></div><h3 id="5-忘记-Sunshine-密码："><a href="#5-忘记-Sunshine-密码：" class="headerlink" title="5.忘记 Sunshine 密码："></a>5.忘记 Sunshine 密码：</h3><details class="fold light" ><summary>重置密码方法</summary><div class='fold-content'><p>打开 <code>C:\Program Files\Sunshine\config</code> 文件夹或你的安装路径 <code>~\Sunshine\config</code> 文件夹，里面有个名为 <code>sunshine_state.json</code> 的文件，将其删除即可恢复默认，重新启动 <code>Sunshine</code> 即可重新配置</p></div></details><h2 id="二、安装-Moonlight（客户端）"><a href="#二、安装-Moonlight（客户端）" class="headerlink" title="二、安装 Moonlight（客户端）"></a>二、安装 Moonlight（客户端）</h2><h3 id="1-下载并安装-Moonlight："><a href="#1-下载并安装-Moonlight：" class="headerlink" title="1.下载并安装 Moonlight："></a>1.下载并安装 Moonlight：</h3><div class="button-list">          <div class="button-item">            <a href="https://github.com/moonlight-stream/moonlight-qt" class="normal" alt=Moonlight GitHub页面 target="_blank">              <div class="button-item-icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M17 22v-2" /><path d="M9 15l6 -6" /><path d="M11 6l.463 -.536a5 5 0 0 1 7.071 7.072l-.534 .464" /><path d="M13 18l-.397 .534a5.068 5.068 0 0 1 -7.127 0a4.972 4.972 0 0 1 0 -7.071l.524 -.463" /><path d="M20 17h2" /><path d="M2 7h2" /><path d="M7 2v2" /></svg></div>              <div class="button-item-info">                <div class="button-item-name">Moonlight GitHub页面</div>                <div class="button-item-desc">访问 Moonlight 的 GitHub 页面</div>              </div>            </a>          </div></div><div class="tabs" id="各平台moonlight安装"><ul class="tabs-nav"><button type="button" class="tab  active" data-href="各平台moonlight安装-1">Android</button><button type="button" class="tab " data-href="各平台moonlight安装-2">Windows</button><button type="button" class="tab " data-href="各平台moonlight安装-3">Linux</button><button type="button" class="tab " data-href="各平台moonlight安装-4">MacOS</button><button type="button" class="tab " data-href="各平台moonlight安装-5">iOS</button></ul><div class="tabs-content"><div class="tabs-item-content active" id="各平台moonlight安装-1"><p><strong>下载链接</strong>：<a href="https://github.com/moonlight-stream/moonlight-android/releases">Moonlight for Android</a></p><ul><li>进入页面后，你可以找到最新的 APK 文件下载链接。</li><li>可以直接通过 Google Play Store 或者下载 APK 文件进行安装。</li><li>手机如果没root就下nonroot版本</li></ul></div><div class="tabs-item-content" id="各平台moonlight安装-2"><p><strong>下载链接</strong>：<a href="https://github.com/moonlight-stream/moonlight-android/releases">Moonlight for Android</a></p><ul><li>进入页面后，找到适用于 Windows 的安装包（通常是 <code>.exe</code> 格式），下载并安装。</li></ul></div><div class="tabs-item-content" id="各平台moonlight安装-3"><p><strong>下载链接</strong>：<a href="https://github.com/moonlight-stream/moonlight-android/releases">Moonlight for Android</a></p><ul><li>进入页面后，你可以找到适用于 Linux 的安装包（通常是 <code>.deb</code> 或 <code>.rpm</code> 格式），选择适合你的发行版进行下载。</li><li>另外，你也可以从源代码自行编译安装。</li></ul></div><div class="tabs-item-content" id="各平台moonlight安装-4"><p><strong>下载链接</strong>：<a href="https://github.com/moonlight-stream/moonlight-qt/releases">Moonlight for macOS</a></p><ul><li>进入页面后，你可以找到 <code>.dmg</code> 格式的安装包下载链接，用于 macOS 系统。</li></ul></div><div class="tabs-item-content" id="各平台moonlight安装-5"><p><strong>下载链接</strong>：<a href="https://github.com/moonlight-stream/moonlight-ios/releases">Moonlight for iOS</a></p><ul><li>你可以通过 <strong>Apple App Store</strong> 下载 Moonlight for iOS，或者从 GitHub 下载源代码进行自定义安装。</li></ul></div><div class="tabs-to-top"><button type="button" aria-label="scroll to top"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 15l6 -6l6 6" /></svg></button></div></div></div><h3 id="2-启动-Moonlight："><a href="#2-启动-Moonlight：" class="headerlink" title="2.启动 Moonlight："></a>2.启动 Moonlight：</h3><p>启动 Moonlight 客户端应用。</p><h3 id="3-连接-Sunshine-服务器："><a href="#3-连接-Sunshine-服务器：" class="headerlink" title="3.连接 Sunshine 服务器："></a>3.连接 Sunshine 服务器：</h3><ul><li>启动 Moonlight 后，客户端会自动扫描你所在网络中的 Sunshine 服务器。</li><li>如果没有自动发现，手动输入 Sunshine 服务器的 IP 地址。</li><li>选择你的 Sunshine 服务器并点击连接。</li></ul><h3 id="4-配对-Moonlight-和-Sunshine："><a href="#4-配对-Moonlight-和-Sunshine：" class="headerlink" title="4.配对 Moonlight 和 Sunshine："></a>4.配对 Moonlight 和 Sunshine：</h3><ul><li>当你首次连接时，Moonlight 会要求进行配对。你需要在 Sunshine 上确认并输入配对码来授权连接。</li><li>这一过程完成后，Moonlight 将能够远程访问你的 Sunshine 服务器。</li></ul><h3 id="关于Moonlight的一些问题"><a href="#关于Moonlight的一些问题" class="headerlink" title="关于Moonlight的一些问题"></a>关于Moonlight的一些问题</h3><details class="fold light" ><summary>常见问题解答</summary><div class='fold-content'><ul><li>默认情况下，触摸屏的输入方式为触控板，要想直接触摸控制，需要在设置-输入设置中去掉勾选”将触控屏作为触控板使用”。</li><li>在客户端设置中，可以调整分辨率、帧数、码率等设置，但要注意分辨率越高，延迟也会增加（主要因为编解码时间增加），而帧数增加可以减小一点显示延迟（不超过主机和客户端刷新率的情况下）。</li><li>默认情况下，串流开始后，只在客户端播放声音。如果想要在客户端和主机同时播放声音，在客户端设置中开启”在电脑上播放声音”即可。</li><li>如果想要在串流时调用原生系统触控键盘，可以三指点击屏幕，就能弹出屏幕键盘（对于Android和IOS系统）。</li><li>如果使用鼠标发现速度过快，可以在设置-输入设置中勾选”适合远程桌面的鼠标模式”，这会让鼠标移动更加自然。</li><li>注意，如果在安卓设备上进行串流，Windows快捷键将和安卓系统快捷键冲突，许多快捷键无法使用。要解决此问题，可以参考B站视频：<strong><a href="https://www.bilibili.com/video/BV1S64y1K7Z7">Moonlight外接物理键盘，快捷键无法使用？</a></strong>，该作者的修改版本通过无障碍模式解决了快捷键冲突问题，同时开启了多指触控功能，可以方便的进行双指缩放等操作，真正实现了原生触控体验。</li></ul></div></details><div class="note pink"><p>如果使用平板设备进行串流，阿猫强烈推荐使用修改版本，开启多指触控功能。默认的触控模式仅仅是模拟鼠标操作，单指拖动模拟鼠标左键拖动，长按模拟鼠标右键，双指拖动模拟滚轮。开启了多指触控后，能直接使用Windows原生触控操作，可以进行单指滑动，双指缩放和三指下拉等操作，解锁更丝滑的串流体验。</p></div><h2 id="三、广域网远程串流"><a href="#三、广域网远程串流" class="headerlink" title="三、广域网远程串流"></a>三、广域网远程串流</h2><p>实现远程连接，延迟最低的方法是公网直连或P2P直连。但由于IPv4地址匮乏，国内大多数家庭网络并不具备公网IPv4地址。</p><div class="tabs" id="广域网串流方案"><ul class="tabs-nav"><button type="button" class="tab  active" data-href="广域网串流方案-1">有公网的情况</button><button type="button" class="tab " data-href="广域网串流方案-2">无公网的情况</button></ul><div class="tabs-content"><div class="tabs-item-content active" id="广域网串流方案-1"><h3 id="公网在运行-Sunshine-的设备上"><a href="#公网在运行-Sunshine-的设备上" class="headerlink" title="公网在运行 Sunshine 的设备上"></a>公网在运行 Sunshine 的设备上</h3><p>这是最简单的方式，只需要在防火墙中放行 Sunshine 使用的端口即可在远端 Moonlight 上使用公网串流。</p><h3 id="公网在网关等路由设备上"><a href="#公网在网关等路由设备上" class="headerlink" title="公网在网关等路由设备上"></a>公网在网关等路由设备上</h3><p>同样也是很简单的方式，只需要在网关中将 Sunshine 使用的端口转发到运行 Sunshine 的设备上即可在远端 Moonlight 上使用公网串流。</p><ol><li><strong>登录路由器管理页面</strong>：<ul><li>访问你的路由器管理界面，在浏览器中输入路由器的 IP 地址（查看路由器背面写的IP地址（形如：<code>192.168.1.1</code> ），或打开cmd，输入 <code>ipconfig</code>，网关一栏的IP便是你的路由器IP）。</li></ul></li><li><strong>配置端口转发</strong>：<ul><li>在路由器管理界面中，找到 “端口转发”（Port Forwarding）或类似的设置项。</li><li>为 Sunshine 服务器配置端口转发，将外部请求的端口（通常是 <code>47984</code>）转发到 Sunshine 服务器的内网 IP 地址和端口上。</li><li>如果你使用了多个端口（如备用端口 <code>47985</code>、视频流端口 <code>47989</code>、音频流端口 <code>47990</code> 等），也需要进行相应的端口转发。<ul><li><strong>端口转发设置</strong>：<ul><li>外部端口：<code>47984</code></li><li>内部 IP：Sunshine 服务器的局域网 IP 地址</li><li>内部端口：<code>47984</code>（或你设置的其他端口）</li><li>协议：TCP&#x2F;UDP（根据需求，通常设置为 TCP）</li></ul></li></ul></li></ul></li></ol></div><div class="tabs-item-content" id="广域网串流方案-2"><p>相信这也是大多数读者的情况</p><h3 id="方案一：内网穿透"><a href="#方案一：内网穿透" class="headerlink" title="方案一：内网穿透"></a>方案一：内网穿透</h3><p>可以任意选择市面上有的内网穿透服务或自行搭建，并将所有使用的端口进行穿透，阿猫在这里不做推荐，以免广告嫌疑。</p><div class="note yellow"><p>注意：此方案的延迟为内网穿透服务器到两设备的延迟和，建议选择离设备所在地区近的节点</p></div><h3 id="方案二：VPN（虚拟专用网络）"><a href="#方案二：VPN（虚拟专用网络）" class="headerlink" title="方案二：VPN（虚拟专用网络）"></a>方案二：VPN（虚拟专用网络）</h3><div class="button-list">          <div class="button-item">            <a href="https://download.wireguard.com/windows-client/" class="normal" alt=WireGuard官网下载 target="_blank">              <div class="button-item-icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M17 22v-2" /><path d="M9 15l6 -6" /><path d="M11 6l.463 -.536a5 5 0 0 1 7.071 7.072l-.534 .464" /><path d="M13 18l-.397 .534a5.068 5.068 0 0 1 -7.127 0a4.972 4.972 0 0 1 0 -7.071l.524 -.463" /><path d="M20 17h2" /><path d="M2 7h2" /><path d="M7 2v2" /></svg></div>              <div class="button-item-info">                <div class="button-item-name">WireGuard官网下载</div>                <div class="button-item-desc">WireGuard安装包官网下载</div>              </div>            </a>          </div></div><div class="note light"><p>这个网站在国内大概率被墙，阿猫在下方附上离线安装包，选择适合系统的版本：</p><p><a href="https://pavilioncat.lanzouq.com/b00y9s3gyb">蓝奏网盘</a> 密码：8fpq</p></div><p>一旦 VPN 成功连接，远程设备就像在同一个局域网内一样，可以直接通过局域网 IP 地址访问 Sunshine。</p><div class="button-list">          <div class="button-item">            <a href="https://blog.pavilioncat.com/install-setup-wireguard-linux.html" class="normal" alt=Linux系统下安装并使用WireGuard target="_blank">              <div class="button-item-icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M17 22v-2" /><path d="M9 15l6 -6" /><path d="M11 6l.463 -.536a5 5 0 0 1 7.071 7.072l-.534 .464" /><path d="M13 18l-.397 .534a5.068 5.068 0 0 1 -7.127 0a4.972 4.972 0 0 1 0 -7.071l.524 -.463" /><path d="M20 17h2" /><path d="M2 7h2" /><path d="M7 2v2" /></svg></div>              <div class="button-item-info">                <div class="button-item-name">Linux系统下安装并使用WireGuard</div>                <div class="button-item-desc">WireGuard服务器搭建和客户端配置文件教程</div>              </div>            </a>          </div></div><div class="note yellow"><p>注意：此方案的延迟为中转服务器到两设备的延迟和，建议选择离设备所在地区近的公网服务器作为 WireGuard 的转发服务器</p></div></div><div class="tabs-to-top"><button type="button" aria-label="scroll to top"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 15l6 -6l6 6" /></svg></button></div></div></div><h2 id="Sunshine-所使用的主要端口及其用途"><a href="#Sunshine-所使用的主要端口及其用途" class="headerlink" title="Sunshine 所使用的主要端口及其用途"></a>Sunshine 所使用的主要端口及其用途</h2><div class="tabs" id="sunshineports"><ul class="tabs-nav"><button type="button" class="tab  active" data-href="sunshineports-1">主端口 (47984)</button><button type="button" class="tab " data-href="sunshineports-2">备用端口 (47985)</button><button type="button" class="tab " data-href="sunshineports-3">视频流端口 (47989)</button><button type="button" class="tab " data-href="sunshineports-4">音频流端口 (47990)</button><button type="button" class="tab " data-href="sunshineports-5">输入流端口 (47991)</button><button type="button" class="tab " data-href="sunshineports-6">NAT穿透端口 (47998)</button><button type="button" class="tab " data-href="sunshineports-7">WebSocket端口 (47992)</button><button type="button" class="tab " data-href="sunshineports-8">自定义端口</button></ul><div class="tabs-content"><div class="tabs-item-content active" id="sunshineports-1"><p>用途：Sunshine 的主端口，主要用于客户端（如 Moonlight）与 Sunshine 服务器之间建立连接。客户端通过此端口进行连接并请求流媒体内容。</p><p>协议：TCP&#x2F;UDP（通常使用 TCP，确保数据传输的可靠性）</p><p>描述：当你在 Moonlight 或其他客户端输入 Sunshine 服务器的 IP 地址时，它会连接到该端口，以建立流媒体传输会话。所有的客户端请求和响应都会通过这个端口进行交互。</p></div><div class="tabs-item-content" id="sunshineports-2"><p>用途：这是一个备用端口，用于客户端和 Sunshine 之间的通信，特别是在主端口出现问题或需要额外的传输通道时。</p><p>协议：TCP&#x2F;UDP</p><p>描述：类似于主端口，这个端口用于进行数据传输和用户交互，确保如果主端口被防火墙或其他网络设备拦截时，客户端仍然可以通过备用端口进行连接。</p></div><div class="tabs-item-content" id="sunshineports-3"><p>用途：此端口用于 Sunshine 将压缩后的屏幕图像（视频流）传输到客户端。通过该端口，Sunshine 发送编码后的图像和视频数据流。</p><p>协议：TCP</p><p>描述：这是视频流传输的主要端口，客户端通过该端口接收 Sunshine 编码后的视频数据流。该端口保证了视频内容的实时传输和显示。</p></div><div class="tabs-item-content" id="sunshineports-4"><p>用途：用于传输音频流，将计算机的音频数据（如游戏声音、系统声音）传输到客户端。</p><p>协议：UDP</p><p>描述：该端口保证音频流的低延迟传输。因为音频通常不需要像视频流那样高带宽，且延迟要求相对较低，因此使用 UDP 协议进行传输，减少延迟。</p></div><div class="tabs-item-content" id="sunshineports-5"><p>用途：该端口用于传输客户端的用户输入（例如鼠标、键盘、控制器等）的数据。</p><p>协议：TCP&#x2F;UDP</p><p>描述：当用户在客户端上进行操作时，这些操作（如鼠标点击、键盘输入、控制器按键）会通过该端口传输回 Sunshine 服务器，使得游戏或应用程序可以响应用户的输入。</p></div><div class="tabs-item-content" id="sunshineports-6"><p>用途：如果 Sunshine 服务器在 NAT 后面（例如使用路由器连接互联网），这个端口用于 NAT 穿透，确保客户端能够通过互联网访问 Sunshine 服务器。</p><p>协议：UDP</p><p>描述：当 Sunshine 和客户端之间存在 NAT 或防火墙时，NAT 穿透协议帮助它们直接建立连接，而不需要手动配置端口转发。</p></div><div class="tabs-item-content" id="sunshineports-7"><p>用途：此端口用于通过 WebSocket 协议与客户端保持通信，主要用于实现实时的用户交互和状态更新。</p><p>协议：TCP</p><p>描述：WebSocket 协议可以实现低延迟、双向通信，确保客户端和服务器之间的实时互动（例如，控制输入和状态更新）。</p></div><div class="tabs-item-content" id="sunshineports-8"><p>用途：Sunshine 还允许用户根据需要修改某些端口。</p><p>描述：如果默认端口在某些环境下发生冲突，用户可以在配置文件中进行自定义配置。</p></div><div class="tabs-to-top"><button type="button" aria-label="scroll to top"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 15l6 -6l6 6" /></svg></button></div></div></div><h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><div class="timeline "><div class='timeline-item timeline-head'><div class='timeline-item-title'><div class='timeline-icon'><p>Sunshine端口说明</p></div></div></div><div class='timeline-item'><div class='timeline-item-title'><div class='timeline-icon'><p>47984</p></div></div><div class='timeline-item-content'><p>主端口，用于 Sunshine 服务器与客户端的连接，通常使用 TCP 协议。</p></div></div><div class='timeline-item'><div class='timeline-item-title'><div class='timeline-icon'><p>47985</p></div></div><div class='timeline-item-content'><p>备用端口，用于提供额外的通信通道，通常使用 TCP&#x2F;UDP 协议。</p></div></div><div class='timeline-item'><div class='timeline-item-title'><div class='timeline-icon'><p>47989</p></div></div><div class='timeline-item-content'><p>视频流端口，用于视频数据传输，使用 TCP 协议。</p></div></div><div class='timeline-item'><div class='timeline-item-title'><div class='timeline-icon'><p>47990</p></div></div><div class='timeline-item-content'><p>音频流端口，用于音频数据传输，使用 UDP 协议。</p></div></div><div class='timeline-item'><div class='timeline-item-title'><div class='timeline-icon'><p>47991</p></div></div><div class='timeline-item-content'><p>输入流端口，用于客户端输入数据的传输，使用 TCP&#x2F;UDP 协议。</p></div></div><div class='timeline-item'><div class='timeline-item-title'><div class='timeline-icon'><p>47998</p></div></div><div class='timeline-item-content'><p>NAT 穿透端口，帮助客户端穿越 NAT，使用 UDP 协议。</p></div></div><div class='timeline-item'><div class='timeline-item-title'><div class='timeline-icon'><p>47992</p></div></div><div class='timeline-item-content'><p>WebSocket 端口，用于实时交互，使用 TCP 协议。</p></div></div></div><h2 id="其他建议和注意事项"><a href="#其他建议和注意事项" class="headerlink" title="其他建议和注意事项"></a>其他建议和注意事项</h2><h3 id="带宽要求："><a href="#带宽要求：" class="headerlink" title="带宽要求："></a>带宽要求：</h3><div class="note green"><p>为了获得流畅的体验，确保你的网络带宽足够高。对于 1080p 60FPS 的流媒体，推荐的上传速度为至少 10Mbps。</p></div><h3 id="低延迟优化："><a href="#低延迟优化：" class="headerlink" title="低延迟优化："></a>低延迟优化：</h3><div class="note blue"><p>若遇到延迟问题，可以尝试调整 Sunshine 和 Moonlight 的设置，特别是帧率、分辨率和码率设置。</p></div><h2 id="最后"><a href="#最后" class="headerlink" title="最后"></a>最后</h2><p>文章大概已经很详细了，如果有什么不明白的可以在评论区提问或者发送邮件至 <code>Pavilion_Cat@outlook.com</code>。</p><div class="button-list">          <div class="button-item">            <a href="https://www.bilibili.com/video/BV1he4y1w7wB/" class="normal" alt=更多支持 target="_blank">              <div class="button-item-icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M17 22v-2" /><path d="M9 15l6 -6" /><path d="M11 6l.463 -.536a5 5 0 0 1 7.071 7.072l-.534 .464" /><path d="M13 18l-.397 .534a5.068 5.068 0 0 1 -7.127 0a4.972 4.972 0 0 1 0 -7.071l.524 -.463" /><path d="M20 17h2" /><path d="M2 7h2" /><path d="M7 2v2" /></svg></div>              <div class="button-item-info">                <div class="button-item-name">更多支持</div>                <div class="button-item-desc">点击查看视频支持</div>              </div>            </a>          </div></div><p>文章如果有错，或者有补充内容，欢迎在评论区指出</p><p>码字不易，喜欢文章可以多多分享</p><details class="fold light" ><summary>待解决问题</summary><div class='fold-content'><p>待解决问题：受 DRM 保护的画面无法被串流，例如腾讯视频播放</p></div></details><hr>]]></content>
    
    
    <summary type="html">Sunshine+Moonlight串流超详细教程：涵盖安装配置、内网穿透、远程访问及常见问题。实现低延迟游戏/办公串流，支持Windows/Linux/Android/iOS</summary>
    
    
    
    <category term="教程" scheme="http://blog.pavilioncat.com/categories/%E6%95%99%E7%A8%8B/"/>
    
    
    <category term="安装教程" scheme="http://blog.pavilioncat.com/tags/%E5%AE%89%E8%A3%85%E6%95%99%E7%A8%8B/"/>
    
    <category term="串流" scheme="http://blog.pavilioncat.com/tags/%E4%B8%B2%E6%B5%81/"/>
    
  </entry>
  
  <entry>
    <title>AndroidStudio远程调试 安卓ADB 远程调试</title>
    <link href="http://blog.pavilioncat.com/remote-debugging-android-studio/"/>
    <id>http://blog.pavilioncat.com/remote-debugging-android-studio/</id>
    <published>2024-12-26T15:46:12.000Z</published>
    <updated>2025-12-22T15:44:12.000Z</updated>
    
    <content type="html"><![CDATA[<blockquote><p>有些开发场景下，需要一台实体安卓手机，但是安卓手机和开发环境不在一个地理位置，无法通过数据线连接，我们该如何远程调试远程手机</p><p>例如本人情况，我开发所用的服务器在南方，我本人在北方，但是我需要进行安装开发，模拟器用不了的情况下，我只能进行 adb 远程调试</p></blockquote><h2 id="所用工具"><a href="#所用工具" class="headerlink" title="所用工具"></a>所用工具</h2><ul><li>Wireguard 服务（自行搭建，参考站内文章，也可以用其他的方式创建虚拟内网）</li><li>安卓手机</li><li>Android Studio</li><li>一个远程控制开发机器的设备</li></ul><p><a href="https://blog.pavilioncat.com/install-setup-wireguard-linux/">https://blog.pavilioncat.com/install-setup-wireguard-linux/</a></p><h2 id="正文"><a href="#正文" class="headerlink" title="正文"></a>正文</h2><h3 id="1-接入-wireguard-内网"><a href="#1-接入-wireguard-内网" class="headerlink" title="1. 接入 wireguard 内网"></a>1. 接入 wireguard 内网</h3><h3 id="2-配对"><a href="#2-配对" class="headerlink" title="2. 配对"></a>2. 配对</h3><h4 id="打开无线调试服务，记录-ip-和-端口号"><a href="#打开无线调试服务，记录-ip-和-端口号" class="headerlink" title="打开无线调试服务，记录 ip 和 端口号"></a>打开无线调试服务，记录 ip 和 端口号</h4><p><img   lazyload src="/assets/images/Meow-Loading.webp" data-lazy-src="https://s21.ax1x.com/2024/12/27/pAvc6Bt.jpg" ></p><p>{“sha1”:”228f8f34da8c8ab9e7476930743afbec7d3835ce”,”ext”:”jpg”}</p><h3 id="3-开发机器运行命令"><a href="#3-开发机器运行命令" class="headerlink" title="3. 开发机器运行命令"></a>3. 开发机器运行命令</h3><div class="code-container" code-lang="Plaintext"><div class="codebox"><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">adb pair IP:端口（IP和端口用图片下面那个，在回车之后他会让你输入配对）</span><br><span class="line">adb connect IP:端口（用图片上面的 IP和端口）</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div><h3 id="4-Android-Studio-内选择设备"><a href="#4-Android-Studio-内选择设备" class="headerlink" title="4. Android Studio 内选择设备"></a>4. Android Studio 内选择设备</h3><blockquote><p>如果你连接成功了，Android Studio 里会自动出现</p></blockquote><p><img   lazyload src="/assets/images/Meow-Loading.webp" data-lazy-src="https://s21.ax1x.com/2024/12/27/pAvccHP.jpg" ></p>]]></content>
    
    
    <summary type="html">解决异地开发难题！本文详细讲解如何通过WireGuard创建VPN内网，并结合ADB实现Android Studio对远程安卓手机的无线调试，操作简单，高效稳定。</summary>
    
    
    
    <category term="教程" scheme="http://blog.pavilioncat.com/categories/%E6%95%99%E7%A8%8B/"/>
    
    
    <category term="VPN" scheme="http://blog.pavilioncat.com/tags/VPN/"/>
    
    <category term="WireGuard" scheme="http://blog.pavilioncat.com/tags/WireGuard/"/>
    
    <category term="ADB" scheme="http://blog.pavilioncat.com/tags/ADB/"/>
    
    <category term="AndroidStudio" scheme="http://blog.pavilioncat.com/tags/AndroidStudio/"/>
    
    <category term="安卓开发" scheme="http://blog.pavilioncat.com/tags/%E5%AE%89%E5%8D%93%E5%BC%80%E5%8F%91/"/>
    
  </entry>
  
  <entry>
    <title>素数计算常用算法</title>
    <link href="http://blog.pavilioncat.com/prime-calculation-algorithms/"/>
    <id>http://blog.pavilioncat.com/prime-calculation-algorithms/</id>
    <published>2024-12-26T05:48:00.000Z</published>
    <updated>2025-12-22T15:44:12.000Z</updated>
    
    <content type="html"><![CDATA[<h2 id="素数的定义"><a href="#素数的定义" class="headerlink" title="素数的定义"></a>素数的定义</h2><blockquote><p><em>“质数又称素数。一个大于1的自然数，除了1和它自身外，不能被其他自然数整除的数叫做质数”</em></p></blockquote><h2 id="1-试除法（Trial-Division）"><a href="#1-试除法（Trial-Division）" class="headerlink" title="1. 试除法（Trial Division）"></a>1. 试除法（Trial Division）</h2><p>这是最基本的素数判断方法。判断一个数是否为素数，就是尝试除以所有小于它的整数，若没有整除的情况，则该数为素数。</p><h3 id="试除法步骤"><a href="#试除法步骤" class="headerlink" title="试除法步骤"></a>试除法步骤</h3><ul><li>如果一个数 <code>n</code> 能被小于 <code>n</code> 的某个整数整除，则 <code>n</code> 不是素数。</li><li>最多只需要尝试除到 <code>sqrt&#123;n&#125;​</code>，因为如果 <code>n=a×b</code>，那么必有 <code>a≤sqrt&#123;n&#125;</code> 且 <code>b≥sqrt&#123;n&#125;​</code>，所以只需要检查小于等于 <code>sqrt&#123;n&#125;</code>​ 的数。</li></ul><div class="tabs" id="试除法特点"><ul class="tabs-nav"><button type="button" class="tab  active" data-href="试除法特点-1">优点</button><button type="button" class="tab " data-href="试除法特点-2">缺点</button></ul><div class="tabs-content"><div class="tabs-item-content active" id="试除法特点-1"><ul><li>简单易懂</li></ul></div><div class="tabs-item-content" id="试除法特点-2"><ul><li>效率较低，特别是当 n 很大时</li></ul></div><div class="tabs-to-top"><button type="button" aria-label="scroll to top"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 15l6 -6l6 6" /></svg></button></div></div></div><h3 id="6k±1法优化的原理"><a href="#6k±1法优化的原理" class="headerlink" title="6k±1法优化的原理"></a>6k±1法优化的原理</h3><p>除了 <code>2</code> 和 <code>3</code> 之外，所有素数都可以表示为 <code>6k±1</code> 的形式，其中k是一个整数。这是因为，任何整数 <code>n</code> 都可以写成以下六种形式之一：</p><p><code>6k</code>，<code>6k+1</code>，<code>6k+2</code>，<code>6k+3</code>，<code>6k+4</code>，<code>6k+5</code></p><p>在这些形式中，只有 <code>6k+1</code> 和 <code>6k−1</code> 可能是素数。其他形式都可以被 <code>2</code> 或 <code>3</code> 整除，因此不需要检查。</p><h3 id="步骤"><a href="#步骤" class="headerlink" title="步骤"></a>步骤</h3><p>在标准的试除法中，通常需要从 <code>2</code> 开始检查每个数是否是素数。但在使用 <code>6k±1</code> 法时，我们可以跳过能被 <code>2</code> 或 <code>3</code> 整除的数，只检查形如 <code>6k±1</code>的数。</p><p><strong>具体如下：</strong></p><ol><li>检查 <code>n</code> 是否能被 <code>2</code> 或 <code>3</code> 整除。如果可以，直接返回 <code>false</code>（不是素数）</li><li>从 <code>5</code> 开始，检查 <code>n</code> 是否能被形如 <code>6k±1</code> 的数整除，直到 <code>sqrt&#123;n&#125;</code> 为止</li></ol><h3 id="C-代码"><a href="#C-代码" class="headerlink" title="C++代码"></a>C++代码</h3><div class="code-container" code-lang="Cpp"><div class="codebox"><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">bool</span> <span class="title">isPrime</span><span class="params">(<span class="type">int</span> n)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (n &lt;= <span class="number">1</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    <span class="keyword">if</span> (n &lt;= <span class="number">3</span>) <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">if</span> (n % <span class="number">2</span> == <span class="number">0</span> || n % <span class="number">3</span> == <span class="number">0</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 检查从 5 开始的奇数因子</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">5</span>; i * i &lt;= n; i += <span class="number">6</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (n % i == <span class="number">0</span> || n % (i + <span class="number">2</span>) == <span class="number">0</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div><div class="note blue"><ul><li><strong>时间复杂度：</strong> <code>O(sqrt{n})</code></li><li><strong>适用场景：</strong> 当 <code>n</code> 较小，或者只需要判断一个单独的数是否为素数时，试除法是最直接的选择</li></ul></div><h2 id="2-埃拉托斯特尼筛法（Sieve-of-Eratosthenes）"><a href="#2-埃拉托斯特尼筛法（Sieve-of-Eratosthenes）" class="headerlink" title="2. 埃拉托斯特尼筛法（Sieve of Eratosthenes）"></a>2. 埃拉托斯特尼筛法（Sieve of Eratosthenes）</h2><p>这是一个高效的生成素数的算法，可以用来找出一定范围内的所有素数。</p><h3 id="筛法步骤"><a href="#筛法步骤" class="headerlink" title="筛法步骤"></a>筛法步骤</h3><ul><li>创建一个从 <code>2</code> 到某个上限 <code>n</code> 的列表，初始时假设所有数都是素数</li><li>从 <code>2</code> 开始，标记所有 <code>2</code> 的倍数为非素数（即删除）</li><li>继续检查下一个未被删除的数，将其倍数删除</li><li>重复上述过程直到所有小于 <code>sqrt&#123;n&#125;</code>​ 的数都处理完毕</li></ul><div class="tabs" id="筛法特点"><ul class="tabs-nav"><button type="button" class="tab  active" data-href="筛法特点-1">优点</button><button type="button" class="tab " data-href="筛法特点-2">缺点</button></ul><div class="tabs-content"><div class="tabs-item-content active" id="筛法特点-1"><ul><li>可以高效地找出一段区间内所有的素数</li><li>时间复杂度为 <code>O(n log ⁡log⁡ n)</code>，比试除法要高效得多</li></ul></div><div class="tabs-item-content" id="筛法特点-2"><ul><li>对于大范围的素数计算，内存占用较大</li></ul></div><div class="tabs-to-top"><button type="button" aria-label="scroll to top"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 15l6 -6l6 6" /></svg></button></div></div></div><h3 id="C-代码-1"><a href="#C-代码-1" class="headerlink" title="C++代码"></a>C++代码</h3><div class="code-container" code-lang="Cpp"><div class="codebox"><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;vector&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function">std::vector&lt;<span class="type">bool</span>&gt; <span class="title">sieveOfEratosthenes</span><span class="params">(<span class="type">int</span> n)</span> </span>&#123;   <span class="comment">//一个布尔向量，标记从0到n每个数是否为素数。</span></span><br><span class="line">    <span class="function">std::vector&lt;<span class="type">bool</span>&gt; <span class="title">isPrime</span><span class="params">(n + <span class="number">1</span>, <span class="literal">true</span>)</span></span>;</span><br><span class="line">    isPrime[<span class="number">0</span>] = isPrime[<span class="number">1</span>] = <span class="literal">false</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> p = <span class="number">2</span>; p * p &lt;= n; p++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (isPrime[p]) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="type">int</span> i = p * p; i &lt;= n; i += p) &#123;</span><br><span class="line">                isPrime[i] = <span class="literal">false</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> isPrime;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div><div class="note green"><ul><li><strong>时间复杂度：</strong> <code>O(n log log n)</code></li><li><strong>适用场景：</strong> 生成小于某个数 <code>n</code> 的所有素数时，埃拉托斯特尼筛法非常高效。对于处理大量素数查找任务，它的效率远高于试除法。</li></ul></div><h2 id="3-米勒-拉宾素性测试（Miller-Rabin-Primality-Test）"><a href="#3-米勒-拉宾素性测试（Miller-Rabin-Primality-Test）" class="headerlink" title="3. 米勒-拉宾素性测试（Miller-Rabin Primality Test）"></a>3. 米勒-拉宾素性测试（Miller-Rabin Primality Test）</h2><p>这是一种概率性的素性测试算法。它判断一个数是否为素数，虽然它不能保证绝对正确，但通过多次测试可以以极高的概率确定一个数是否为素数。</p><h3 id="步骤-1"><a href="#步骤-1" class="headerlink" title="步骤"></a>步骤</h3><ul><li>基于费马小定理：对于一个大素数 <code>p</code>，若随机选择的数 <code>a</code> 满足 <code>ap−1≡1 mod p</code>，则 <code>p</code> 是素数</li><li>通过随机选择多个不同的 <code>a</code>，每次执行费马测试，若结果不成立，则 <code>n</code> 一定不是素数</li></ul><div class="tabs" id="米勒-拉宾特点"><ul class="tabs-nav"><button type="button" class="tab  active" data-href="米勒-拉宾特点-1">优点</button><button type="button" class="tab " data-href="米勒-拉宾特点-2">缺点</button></ul><div class="tabs-content"><div class="tabs-item-content active" id="米勒-拉宾特点-1"><ul><li>对于大数非常高效</li><li>可以非常快速地进行素数性检验</li></ul></div><div class="tabs-item-content" id="米勒-拉宾特点-2"><ul><li>是概率算法，不能提供确定性的结果（但通过多次实验，可以达到很高的精确度）</li></ul></div><div class="tabs-to-top"><button type="button" aria-label="scroll to top"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 15l6 -6l6 6" /></svg></button></div></div></div><h3 id="C-代码-2"><a href="#C-代码-2" class="headerlink" title="C++代码"></a>C++代码</h3><div class="code-container" code-lang="Cpp"><div class="codebox"><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;cstdlib&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 计算 (a^b) % mod</span></span><br><span class="line"><span class="function"><span class="type">long</span> <span class="type">long</span> <span class="title">modExp</span><span class="params">(<span class="type">long</span> <span class="type">long</span> a, <span class="type">long</span> <span class="type">long</span> b, <span class="type">long</span> <span class="type">long</span> mod)</span> </span>&#123;</span><br><span class="line">    <span class="type">long</span> <span class="type">long</span> result = <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">while</span> (b &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (b % <span class="number">2</span> == <span class="number">1</span>) &#123;</span><br><span class="line">            result = (result * a) % mod;</span><br><span class="line">        &#125;</span><br><span class="line">        a = (a * a) % mod;</span><br><span class="line">        b /= <span class="number">2</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">bool</span> <span class="title">millerRabinTest</span><span class="params">(<span class="type">long</span> <span class="type">long</span> n, <span class="type">int</span> k)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (n &lt;= <span class="number">1</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    <span class="keyword">if</span> (n == <span class="number">2</span> || n == <span class="number">3</span>) <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">if</span> (n % <span class="number">2</span> == <span class="number">0</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 将 n-1 分解成 d * 2^r</span></span><br><span class="line">    <span class="type">long</span> <span class="type">long</span> d = n - <span class="number">1</span>;</span><br><span class="line">    <span class="type">int</span> r = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">while</span> (d % <span class="number">2</span> == <span class="number">0</span>) &#123;</span><br><span class="line">        d /= <span class="number">2</span>;</span><br><span class="line">        r++;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; k; i++) &#123;</span><br><span class="line">        <span class="type">long</span> <span class="type">long</span> a = <span class="number">2</span> + <span class="built_in">rand</span>() % (n - <span class="number">4</span>);  <span class="comment">// 随机选择 a</span></span><br><span class="line">        <span class="type">long</span> <span class="type">long</span> x = <span class="built_in">modExp</span>(a, d, n);</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">if</span> (x == <span class="number">1</span> || x == n - <span class="number">1</span>) <span class="keyword">continue</span>;</span><br><span class="line">        </span><br><span class="line">        <span class="type">bool</span> continueLoop = <span class="literal">false</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j &lt; r - <span class="number">1</span>; j++) &#123;</span><br><span class="line">            x = (x * x) % n;</span><br><span class="line">            <span class="keyword">if</span> (x == n - <span class="number">1</span>) &#123;</span><br><span class="line">                continueLoop = <span class="literal">true</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (!continueLoop) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div><div class="note purple"><ul><li><strong>时间复杂度：</strong> 每次测试的时间复杂度为 <code>O(log n)</code>,进行 <code>k</code> 次测试，整体复杂度为 <code>O(k log n)</code>，其中 <code>k</code> 通常设置为 <code>5</code> 到 <code>10</code>，保证很高的精确度。</li><li><strong>适用场景：</strong> 对于大数素性检测，尤其是在确定性测试需求不高的情况下，米勒-拉宾测试非常高效。通过增加 kkk 次测试可以获得非常高的精度，适用于大数素性判断。</li></ul></div><h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><div class="timeline "><div class='timeline-item timeline-head'><div class='timeline-item-title'><div class='timeline-icon'><p>算法对比</p></div></div></div><div class='timeline-item'><div class='timeline-item-title'><div class='timeline-icon'><p>试除法</p></div></div><div class='timeline-item-content'><p>适用于小规模的素数判断</p></div></div><div class='timeline-item'><div class='timeline-item-title'><div class='timeline-icon'><p>埃拉托斯特尼筛法</p></div></div><div class='timeline-item-content'><p>适合生成一段范围内的所有素数</p></div></div><div class='timeline-item'><div class='timeline-item-title'><div class='timeline-icon'><p>米勒-拉宾素性测试</p></div></div><div class='timeline-item-content'><p>适用于大数的素性测试，具有较高的效率和较高的准确性</p></div></div></div><div class="note pink"><p><strong>最低时间复杂度</strong>：<strong>埃拉托斯特尼筛法</strong></p><p><strong>适合大数素性检测</strong>：<strong>米勒-拉宾素性测试</strong></p></div><p>因此，如果你需要找出一个区间内的所有素数，<strong>埃拉托斯特尼筛法</strong> 是最佳选择。如果是对大数的单独素性检测，<strong>米勒-拉宾素性测试</strong> 会更有效。</p>]]></content>
    
    
    <summary type="html">深入解析素数计算的常用算法：试除法、埃拉托斯特尼筛法与米勒-拉宾素性测试。附C++代码实现，复杂度分析与使用场景对比。</summary>
    
    
    
    <category term="算法" scheme="http://blog.pavilioncat.com/categories/%E7%AE%97%E6%B3%95/"/>
    
    
    <category term="素数" scheme="http://blog.pavilioncat.com/tags/%E7%B4%A0%E6%95%B0/"/>
    
  </entry>
  
  <entry>
    <title>Linux系统下安装并使用WireGuard</title>
    <link href="http://blog.pavilioncat.com/install-setup-wireguard-linux/"/>
    <id>http://blog.pavilioncat.com/install-setup-wireguard-linux/</id>
    <published>2024-12-11T09:03:39.000Z</published>
    <updated>2025-12-22T15:44:12.000Z</updated>
    
    <content type="html"><![CDATA[<h2 id="环境要求"><a href="#环境要求" class="headerlink" title="环境要求"></a>环境要求</h2><div class="note light"><ul><li>一台 Windows 或 Linux 设备充当服务器</li><li>多台 Windows 或 Linux 或 Mac 或 Android 或 iOS 设备充当客户端</li><li>作为 WireGuard 服务器的设备需要有公网 IP 或者能被客户端访问</li></ul></div><h2 id="1-安装-WireGuard"><a href="#1-安装-WireGuard" class="headerlink" title="1. 安装 WireGuard"></a>1. 安装 WireGuard</h2><p>首先，在你的 Linux 系统上安装 WireGuard。你可以通过 apt、dnf、yum 或其他包管理器进行安装，具体取决于你的发行版。</p><div class="tabs" id="安装方法"><ul class="tabs-nav"><button type="button" class="tab  active" data-href="安装方法-1">Debian/Ubuntu 系统</button><button type="button" class="tab " data-href="安装方法-2">CentOS/RHEL 系统</button><button type="button" class="tab " data-href="安装方法-3">Fedora 系统</button><button type="button" class="tab " data-href="安装方法-4">Arch Linux</button></ul><div class="tabs-content"><div class="tabs-item-content active" id="安装方法-1"><div class="code-container" code-lang="Bash"><div class="codebox"><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sudo</span> apt update</span><br><span class="line"><span class="built_in">sudo</span> apt install wireguard</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div></div><div class="tabs-item-content" id="安装方法-2"><div class="note yellow"><p>启用 EPEL 仓库并安装 WireGuard：</p></div><div class="code-container" code-lang="Bash"><div class="codebox"><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sudo</span> yum install epel-release</span><br><span class="line"><span class="built_in">sudo</span> yum install wireguard-tools</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div><div class="note blue"><p>如果你的 CentOS&#x2F;RHEL 系统使用的是 8 及以上版本，你可能需要额外的仓库支持：</p></div><div class="code-container" code-lang="Bash"><div class="codebox"><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sudo</span> dnf install wireguard-tools</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div></div><div class="tabs-item-content" id="安装方法-3"><div class="code-container" code-lang="Bash"><div class="codebox"><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sudo</span> dnf install wireguard-tools</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div></div><div class="tabs-item-content" id="安装方法-4"><div class="code-container" code-lang="Bash"><div class="codebox"><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sudo</span> pacman -S wireguard-tools</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div></div><div class="tabs-to-top"><button type="button" aria-label="scroll to top"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 15l6 -6l6 6" /></svg></button></div></div></div><details class="fold blue" ><summary>手动下载Windows安装包</summary><div class='fold-content'><ul><li>官网下载链接：<a href="https://www.wireguard.com/install/">https://www.wireguard.com/install/</a></li><li>这个网站在国内大概率被墙，阿猫在这里附上离线安装包，选择适合系统的版本安装即可 <a href="https://pavilioncat.lanzouq.com/b00y9s3gyb">蓝奏网盘</a> 密码：8fpq</li></ul></div></details><h2 id="2-生成密钥对"><a href="#2-生成密钥对" class="headerlink" title="2. 生成密钥对"></a>2. 生成密钥对</h2><div class="note purple"><p>WireGuard 使用公钥和私钥来进行加密通信。你可以通过以下命令生成密钥对：</p></div><div class="code-container" code-lang="Bash"><div class="codebox"><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wg genkey | <span class="built_in">tee</span> privatekey | wg pubkey &gt; publickey</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div><details class="fold yellow" ><summary>密钥文件说明</summary><div class='fold-content'><p>这将在当前目录下生成 <code>privatekey</code> 和 <code>publickey</code> 文件。</p><ul><li><strong>私钥</strong>：为机密，不可共享给他人</li><li><strong>公钥</strong>：可以共享给其他 WireGuard 节点</li></ul></div></details><h2 id="3-配置-WireGuard"><a href="#3-配置-WireGuard" class="headerlink" title="3. 配置 WireGuard"></a>3. 配置 WireGuard</h2><p>在 <code>/etc/wireguard/</code> 目录下创建一个配置文件（例如 <code>wg0.conf</code>）。</p><div class="code-container" code-lang="Bash"><div class="codebox"><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sudo</span> vim /etc/wireguard/wg0.conf</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div><div class="tabs" id="配置示例"><ul class="tabs-nav"><button type="button" class="tab  active" data-href="配置示例-1">客户端配置</button><button type="button" class="tab " data-href="配置示例-2">服务器配置</button><button type="button" class="tab " data-href="配置示例-3">多客户端服务器配置</button></ul><div class="tabs-content"><div class="tabs-item-content active" id="配置示例-1"><div class="code-container" code-lang="Ini"><div class="codebox"><figure class="highlight ini"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">[Interface]</span></span><br><span class="line"><span class="attr">PrivateKey</span> = &lt;客户端私钥&gt;  <span class="comment"># 修改时请将&lt;&gt;一并删去</span></span><br><span class="line"><span class="attr">Address</span> = *.*.*.*/*        <span class="comment"># 客户端的虚拟IP地址，服务器分配的ip</span></span><br><span class="line"></span><br><span class="line"><span class="section">[Peer]</span></span><br><span class="line"><span class="attr">PublicKey</span> = &lt;服务器公钥&gt;</span><br><span class="line"><span class="attr">Endpoint</span> = &lt;服务器IP地址&gt;:<span class="number">51820</span>  <span class="comment"># 端口为服务器设置的端口，51820为默认 WireGuard 端口</span></span><br><span class="line"><span class="attr">AllowedIPs</span> = *.*.*.*/*          <span class="comment"># 允许通过 VPN 网络的对端地址</span></span><br><span class="line"><span class="attr">PersistentKeepalive</span> = <span class="number">25</span>         <span class="comment"># 保持连接的频率</span></span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div><div class="note pink"><ul><li><strong><code>[Interface]</code></strong> 部分包含本机配置。你需要用你自己的私钥替换 <code>&lt;你的私钥&gt;</code>，并设置适当的 IP 地址。</li><li><strong><code>[Peer]</code></strong> 部分是对端的配置，<code>&lt;对端公钥&gt;</code> 是对方的公钥，<code>AllowedIPs</code> 是允许的 IP 范围。</li></ul></div></div><div class="tabs-item-content" id="配置示例-2"><div class="code-container" code-lang="Ini"><div class="codebox"><figure class="highlight ini"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">[Interface]</span></span><br><span class="line"><span class="attr">Address</span> = *.*.*.*/*       <span class="comment"># 这是服务器的虚拟IP地址</span></span><br><span class="line"><span class="attr">ListenPort</span> = <span class="number">51820</span>        <span class="comment"># WireGuard 默认端口</span></span><br><span class="line"><span class="attr">PrivateKey</span> = &lt;服务器私钥&gt; <span class="comment"># 服务器的私钥</span></span><br><span class="line"> </span><br><span class="line"><span class="section">[Peer]</span></span><br><span class="line"><span class="attr">PublicKey</span> = &lt;客户端公钥&gt;   <span class="comment"># 连接的客户端公钥</span></span><br><span class="line"><span class="attr">AllowedIPs</span> = *.*.*.*/.*   <span class="comment"># 客户端的虚拟IP地址</span></span><br><span class="line"><span class="attr">PersistentKeepalive</span> = <span class="number">25</span>  <span class="comment"># 保持连接的频率</span></span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div><div class="note green"><ul><li><strong>Address</strong>：为服务器分配一个 IP 地址（通常选择 10.0.0.1&#x2F;24）。</li><li><strong>ListenPort</strong>：设置 WireGuard 监听的端口（默认是 51820）。</li><li><strong>PrivateKey</strong>：填入你为服务器生成的私钥。</li><li><strong><code>[Peer]</code></strong> 部分是客户端的配置，公钥需要在客户端生成并提供给服务器。</li></ul></div></div><div class="tabs-item-content" id="配置示例-3"><div class="code-container" code-lang="Ini"><div class="codebox"><figure class="highlight ini"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">[Interface]</span></span><br><span class="line"><span class="attr">Address</span> = *.*.*.*/*       <span class="comment"># 这是服务器的虚拟IP地址</span></span><br><span class="line"><span class="attr">ListenPort</span> = <span class="number">51820</span>        <span class="comment"># WireGuard 默认端口</span></span><br><span class="line"><span class="attr">PrivateKey</span> = &lt;服务器私钥&gt; <span class="comment"># 服务器的私钥</span></span><br><span class="line"> </span><br><span class="line"><span class="section">[Peer]</span></span><br><span class="line"><span class="comment">#D1</span></span><br><span class="line"><span class="attr">PublicKey</span> = &lt;客户端<span class="number">1</span>公钥&gt;   <span class="comment"># 连接的客户端公钥</span></span><br><span class="line"><span class="attr">AllowedIPs</span> = *.*.*.*/*    <span class="comment"># 客户端的虚拟IP地址</span></span><br><span class="line"><span class="attr">PersistentKeepalive</span> = <span class="number">25</span>  <span class="comment"># 保持连接的频率</span></span><br><span class="line"> </span><br><span class="line"><span class="section">[Peer]</span></span><br><span class="line"><span class="comment">#D2</span></span><br><span class="line"><span class="attr">PublicKey</span> = &lt;客户端<span class="number">2</span>公钥&gt;   <span class="comment"># 连接的客户端公钥</span></span><br><span class="line"><span class="attr">AllowedIPs</span> = *.*.*.*/*    <span class="comment"># 客户端的虚拟IP地址</span></span><br><span class="line"><span class="attr">PersistentKeepalive</span> = <span class="number">25</span>  <span class="comment"># 保持连接的频率</span></span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div><p>如果有多个客户端，可以在 <code>[Peer]</code> 部分添加多个 <code>PublicKey</code> 和 <code>AllowedIPs</code> 条目。</p></div><div class="tabs-to-top"><button type="button" aria-label="scroll to top"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 15l6 -6l6 6" /></svg></button></div></div></div><h2 id="4-启动-WireGuard-服务"><a href="#4-启动-WireGuard-服务" class="headerlink" title="4. 启动 WireGuard 服务"></a>4. 启动 WireGuard 服务</h2><p>使用以下命令启动 WireGuard 接口：</p><div class="code-container" code-lang="Bash"><div class="codebox"><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sudo</span> wg-quick up wg0</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div><div class="note blue"><p>这将启用 <code>wg0</code> 配置文件中的设置并启动 VPN 服务。</p></div><h2 id="5-配置自动启动"><a href="#5-配置自动启动" class="headerlink" title="5. 配置自动启动"></a>5. 配置自动启动</h2><p>为了让 WireGuard 在系统启动时自动启动，使用以下命令启用服务：</p><div class="code-container" code-lang="Bash"><div class="codebox"><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sudo</span> systemctl <span class="built_in">enable</span> wg-quick@wg0</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div><h2 id="6-配置防火墙"><a href="#6-配置防火墙" class="headerlink" title="6. 配置防火墙"></a>6. 配置防火墙</h2><p>确保防火墙允许 WireGuard 的 UDP 端口（默认是 51820）</p><div class="tabs" id="防火墙配置"><ul class="tabs-nav"><button type="button" class="tab  active" data-href="防火墙配置-1">使用 UFW</button><button type="button" class="tab " data-href="防火墙配置-2">使用 iptables</button></ul><div class="tabs-content"><div class="tabs-item-content active" id="防火墙配置-1"><div class="code-container" code-lang="Bash"><div class="codebox"><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sudo</span> ufw allow 51820/udp</span><br><span class="line"><span class="built_in">sudo</span> ufw <span class="built_in">enable</span></span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div></div><div class="tabs-item-content" id="防火墙配置-2"><div class="code-container" code-lang="Bash"><div class="codebox"><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sudo</span> iptables -A INPUT -p udp --dport 51820 -j ACCEPT</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div><div class="note yellow"><p>如果你使用 <code>iptables</code>，确保保存规则，否则它们会在重启后丢失。</p></div></div><div class="tabs-to-top"><button type="button" aria-label="scroll to top"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 15l6 -6l6 6" /></svg></button></div></div></div><h2 id="7-启用-IP-转发（仅服务器）"><a href="#7-启用-IP-转发（仅服务器）" class="headerlink" title="7. 启用 IP 转发（仅服务器）"></a>7. 启用 IP 转发（仅服务器）</h2><div class="note purple"><p>如果你希望通过 VPN 连接进行路由，需启用 IP 转发：</p></div><p><strong>打开 <code>/etc/sysctl.conf</code> 文件：</strong></p><div class="code-container" code-lang="Bash"><div class="codebox"><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sudo</span> vim /etc/sysctl.conf</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div><p><strong>添加以下行以启用 IPv4 和 IPv6 转发：</strong></p><div class="code-container" code-lang="Bash"><div class="codebox"><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">net.ipv4.ip_forward=1</span><br><span class="line">net.ipv6.conf.all.forwarding=1</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div><p><strong>使更改生效：</strong></p><div class="code-container" code-lang="Bash"><div class="codebox"><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sudo</span> sysctl -p</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div><h2 id="8-配置-NAT（仅服务器）"><a href="#8-配置-NAT（仅服务器）" class="headerlink" title="8. 配置 NAT（仅服务器）"></a>8. 配置 NAT（仅服务器）</h2><div class="note pink"><p>如果你想通过 VPN 为客户端提供 Internet 访问，还需要配置 NAT。假设你使用的是 <code>eth0</code> 网络接口（根据实际网络接口名称调整）：</p></div><p><strong>使用 <code>iptables</code> 配置 NAT：</strong></p><div class="code-container" code-lang="Bash"><div class="codebox"><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sudo</span> iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div><p><strong>保存 <code>iptables</code> 规则（根据你的 Linux 发行版选择适当方法）：</strong></p><div class="tabs" id="保存iptables规则"><ul class="tabs-nav"><button type="button" class="tab  active" data-href="保存iptables规则-1">Debian/Ubuntu</button><button type="button" class="tab " data-href="保存iptables规则-2">CentOS/RHEL</button></ul><div class="tabs-content"><div class="tabs-item-content active" id="保存iptables规则-1"><div class="code-container" code-lang="Bash"><div class="codebox"><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sudo</span> iptables-save &gt; /etc/iptables/rules.v4</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div></div><div class="tabs-item-content" id="保存iptables规则-2"><div class="code-container" code-lang="Bash"><div class="codebox"><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sudo</span> service iptables save</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div></div><div class="tabs-to-top"><button type="button" aria-label="scroll to top"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 15l6 -6l6 6" /></svg></button></div></div></div><h2 id="9-测试连接"><a href="#9-测试连接" class="headerlink" title="9. 测试连接"></a>9. 测试连接</h2><p>确保客户端可以成功连接到服务器</p><div class="note green"><p>使用 <code>ping</code> 命令测试：(从客户端 ping 服务器的虚拟 IP)</p></div><div class="code-container" code-lang="Bash"><div class="codebox"><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ping *.*.*.*</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div><h2 id="文中未提及的常用命令"><a href="#文中未提及的常用命令" class="headerlink" title="文中未提及的常用命令"></a>文中未提及的常用命令</h2><ul><li><strong>查看 WireGuard 接口状态：</strong></li></ul><div class="code-container" code-lang="Bash"><div class="codebox"><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sudo</span> wg</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div><ul><li><strong>启动 WireGuard 接口：</strong></li></ul><div class="code-container" code-lang="Bash"><div class="codebox"><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sudo</span> wg-quick up wg0</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div><ul><li><strong>停止 WireGuard 接口：</strong></li></ul><div class="code-container" code-lang="Bash"><div class="codebox"><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sudo</span> wg-quick down wg0</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div><ul><li><strong>查看接口的传输数据：</strong></li></ul><div class="code-container" code-lang="Bash"><div class="codebox"><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sudo</span> wg show wg0</span><br></pre></td></tr></table></figure><div class="code-fold" title="Fold/Open"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 9l6 6l6 -6" /></svg></div><div class="code-copy" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"  stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /><path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /></svg></div></div></div>]]></content>
    
    
    <summary type="html">Linux系统下WireGuard VPN服务器搭建与客户端配置全指南。涵盖多平台安装、密钥生成、服务端与多客户端配置、防火墙及NAT设置。</summary>
    
    
    
    <category term="教程" scheme="http://blog.pavilioncat.com/categories/%E6%95%99%E7%A8%8B/"/>
    
    
    <category term="Linux" scheme="http://blog.pavilioncat.com/tags/Linux/"/>
    
    <category term="VPN" scheme="http://blog.pavilioncat.com/tags/VPN/"/>
    
    <category term="WireGuard" scheme="http://blog.pavilioncat.com/tags/WireGuard/"/>
    
    <category term="安装教程" scheme="http://blog.pavilioncat.com/tags/%E5%AE%89%E8%A3%85%E6%95%99%E7%A8%8B/"/>
    
  </entry>
  
</feed>
