如何在 Linux 中创建交换分区

Linux命令 Edge插件网 10个月前 (12-04) 320次浏览 已收录 0个评论

交换分区可以是任何 Linux 系统的重要组件。当系统的 RAM 被充分利用时,它会提供额外的内存。如果没有足够的交换空间,当内存需求很高时,您的系统可能会急剧变慢甚至冻结。

如何在 Linux 中创建交换分区

先决条件

在开始之前,完成本指南有几个要求:

  • Linux的基本知识和终端使用舒适性。我们将使用各种命令行实用程序。
  • 对 Ubuntu Linux 发行版的 root 访问权限。要创建分区和修改系统文件,您需要使用 root 权限进行操作。
  • 可用磁盘空间。我们将创建一个用于交换的新分区,因此您需要在磁盘上留出可用空间。
  • 备份(推荐)。修改磁盘分区时,最好进行备份,以防万一。

只要满足这些要求,就可以开始创建交换分区了。让我们先看一下当前的交换设置。

检查当前掉期信息

首先,我们需要检查系统是否已经配置了任何交换空间。这让我们对当前的交换情况以及我们是否需要添加更多。

使用 swapon 命令查看当前启用的交换区域:

<span class="pln">sudo swapon </span><span class="pun">--</span><span class="pln">show</span>

这将打印出任何活动的交换分区或文件。如果未打印任何内容,则未启用交换。

要查看有关交换区域(包括大小)的更多详细信息,请使用 带有 -s 标志的 swapon

<span class="pln">sudo swapon </span><span class="pun">-</span><span class="pln">s</span>

这提供了更详细的输出,包括交换文件或分区名称、交换类型、大小、已用空间和优先级。

您还可以使用像 grep 这样的实用程序来解析 /proc/swaps 的输出 ,其中包含类似的信息:

<span class="pln">sudo grep </span><span class="pun">--</span><span class="pln">color</span><span class="pun">=</span><span class="kwd">auto</span> <span class="typ">Swap</span> <span class="pun">/</span><span class="pln">proc</span><span class="pun">/</span><span class="pln">swaps</span>

除了当前启用的交换之外,我们还想检查任何可能配置为通过 /etc/fstab 在启动时激活的交换条目

检查此文件中是否有任何以“swap”开头的行:

<span class="pln">sudo cat </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">fstab</span>

这将打印定义永久交换挂载的行。输出将包括交换区域名称和挂载选项。 有了这些信息,您现在可以查看是否已经设置了任何交换空间、任何现有交换区域的大小,以及是否需要额外的交换。

创建交换分区

如果已确定需要额外的交换空间,则下一步是创建新的交换分区。

有几种方法可以解决这个问题,但我们将使用 fdisk it 创建一个新的主分区进行交换。

注意:这将清除分区上的所有现有数据,因此请确保不要覆盖另一个重要分区。 首先, 对要分区的磁盘运行 fdisk

<span class="pln">sudo fdisk </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda</span>

将 /dev/sda 替换为实际的磁盘设备名称。在 fdisk 界面内:

    1. 键入 p 以打印分区表并标识可用空间。
    2. 键入 n 以创建新分区。
    3. 将分区类型指定为 Linux swap
    4. 输入所需大小的起始块和结束块。
    5. 键入 t 为 Linux 交换分区设置十六进制代码 82
    6. 验证分区表和更改是否正确。
    7. 键入 w 写入分区表并退出。

例如:

<span class="typ">Command</span> <span class="pun">(</span><span class="pln">m </span><span class="kwd">for</span><span class="pln"> help</span><span class="pun">):</span><span class="pln"> p

</span><span class="typ">Disk</span> <span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda</span><span class="pun">:</span> <span class="lit">32.2</span><span class="pln"> GB</span><span class="pun">,</span> <span class="lit">32212254720</span><span class="pln"> bytes</span><span class="pun">,</span> <span class="lit">62914560</span><span class="pln"> sectors
</span><span class="typ">Units</span> <span class="pun">=</span><span class="pln"> sectors </span><span class="kwd">of</span> <span class="lit">1</span> <span class="pun">*</span> <span class="lit">512</span> <span class="pun">=</span> <span class="lit">512</span><span class="pln"> bytes

</span><span class="typ">Device</span> <span class="typ">Boot</span>      <span class="typ">Start</span>         <span class="typ">End</span>      <span class="typ">Blocks</span>   <span class="typ">Id</span>  <span class="typ">System</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda1   </span><span class="pun">*</span>        <span class="lit">2048</span>    <span class="lit">2099199</span>    <span class="lit">1048576</span>   <span class="lit">83</span>  <span class="typ"><a href="https://www.edge66.com/edgetag/linux" title="查看更多关于Linux的文章" target="_blank">Linux</a></span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda2        </span><span class="lit">2099200</span>    <span class="lit">62914559</span>    <span class="lit">30411280</span>   <span class="lit">83</span>  <span class="typ">Linux</span>

<span class="typ">Command</span> <span class="pun">(</span><span class="pln">m </span><span class="kwd">for</span><span class="pln"> help</span><span class="pun">):</span><span class="pln"> n
</span><span class="typ">Partition</span><span class="pln"> type</span><span class="pun">:</span><span class="pln">
   p   primary </span><span class="pun">(</span><span class="lit">0</span><span class="pln"> primary</span><span class="pun">,</span> <span class="lit">0</span><span class="pln"> extended</span><span class="pun">,</span> <span class="lit">4</span><span class="pln"> free</span><span class="pun">)</span><span class="pln">
   e   extended
</span><span class="typ">Select</span> <span class="pun">(</span><span class="kwd">default</span><span class="pln"> p</span><span class="pun">):</span><span class="pln"> p
</span><span class="typ">Partition</span><span class="pln"> number </span><span class="pun">(</span><span class="lit">1</span><span class="pun">-</span><span class="lit">4</span><span class="pun">,</span> <span class="kwd">default</span> <span class="lit">2</span><span class="pun">):</span> 
<span class="typ">First</span><span class="pln"> sector </span><span class="pun">(</span><span class="lit">20973568</span><span class="pun">-</span><span class="lit">62914559</span><span class="pun">,</span> <span class="kwd">default</span> <span class="lit">20973568</span><span class="pun">):</span>
<span class="typ">Using</span> <span class="kwd">default</span> <span class="kwd">value</span> <span class="lit">20973568</span>
<span class="typ">Last</span><span class="pln"> sector</span><span class="pun">,</span> <span class="pun">+</span><span class="pln">sectors </span><span class="kwd">or</span> <span class="pun">+</span><span class="pln">size</span><span class="pun">{</span><span class="pln">K</span><span class="pun">,</span><span class="pln">M</span><span class="pun">,</span><span class="pln">G</span><span class="pun">}</span> <span class="pun">(</span><span class="lit">20973568</span><span class="pun">-</span><span class="lit">62914559</span><span class="pun">,</span> <span class="kwd">default</span> <span class="lit">62914559</span><span class="pun">):</span> 
<span class="typ">Using</span> <span class="kwd">default</span> <span class="kwd">value</span> <span class="lit">62914559</span>

<span class="typ">Command</span> <span class="pun">(</span><span class="pln">m </span><span class="kwd">for</span><span class="pln"> help</span><span class="pun">):</span><span class="pln"> t
</span><span class="typ">Selected</span><span class="pln"> partition </span><span class="lit">2</span>
<span class="typ">Hex</span><span class="pln"> code </span><span class="pun">(</span><span class="pln">type L to list all codes</span><span class="pun">):</span> <span class="lit">82</span>
<span class="typ">Changed</span><span class="pln"> type </span><span class="kwd">of</span><span class="pln"> partition </span><span class="str">'Linux'</span><span class="pln"> to </span><span class="str">'Linux swap'</span>

<span class="typ">Command</span> <span class="pun">(</span><span class="pln">m </span><span class="kwd">for</span><span class="pln"> help</span><span class="pun">):</span><span class="pln"> p

</span><span class="typ">Disk</span> <span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda</span><span class="pun">:</span> <span class="lit">32.2</span><span class="pln"> GB</span><span class="pun">,</span> <span class="lit">32212254720</span><span class="pln"> bytes</span><span class="pun">,</span> <span class="lit">62914560</span><span class="pln"> sectors
</span><span class="typ">Units</span> <span class="pun">=</span><span class="pln"> sectors </span><span class="kwd">of</span> <span class="lit">1</span> <span class="pun">*</span> <span class="lit">512</span> <span class="pun">=</span> <span class="lit">512</span><span class="pln"> bytes

</span><span class="typ">Device</span> <span class="typ">Boot</span>      <span class="typ">Start</span>         <span class="typ">End</span>      <span class="typ">Blocks</span>   <span class="typ">Id</span>  <span class="typ">System</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda1   </span><span class="pun">*</span>        <span class="lit">2048</span>    <span class="lit">2099199</span>    <span class="lit">1048576</span>   <span class="lit">83</span>  <span class="typ">Linux</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda2        </span><span class="lit">2099200</span>    <span class="lit">62914559</span>    <span class="lit">30411280</span>   <span class="lit">82</span>  <span class="typ">Linux</span><span class="pln"> swap

</span><span class="typ">Command</span> <span class="pun">(</span><span class="pln">m </span><span class="kwd">for</span><span class="pln"> help</span><span class="pun">):</span><span class="pln"> w
</span><span class="typ">The</span><span class="pln"> partition table has been altered</span><span class="pun">!</span>

<span class="typ">Calling</span><span class="pln"> ioctl</span><span class="pun">()</span><span class="pln"> to re</span><span class="pun">-</span><span class="pln">read partition table</span><span class="pun">.</span> 
<span class="typ">Syncing</span><span class="pln"> disks</span><span class="pun">.</span>

这将创建一个所需大小的新交换分区。接下来,我们需要准备它以供使用。

使交换分区可用

现在我们有一个未格式化的交换分区,我们需要将其设置为用作实际的交换区域。 首先,我们使用 mkswap 实用程序格式化分区:

<span class="pln">sudo mkswap </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda2</span>

将 /dev/sda2 替换为实际的交换分区。

这将使用 Linux 交换文件系统格式化分区。

接下来,使用 swapon 命令启用交换区域:

<span class="pln">sudo swapon </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda2</span>

Our new swap space is now active and ready for use!

我们的新交换空间现已激活并可供使用!

我们可以通过再次检查 swapon --show 来验证它是否已启用 ,它现在应该被列出。

使交换永久化

目前,我们的新交换分区已启用,但除非我们将其添加到 /etc/fstab 文件中,否则它不会在重新启动时保留。 首先,获取新交换分区的 UUID:

<span class="pln">sudo blkid </span><span class="pun">|</span><span class="pln"> grep swap</span>

输出将以类似于以下内容的格式提供 UUID:

<span class="str">/dev/</span><span class="pln">sda2</span><span class="pun">:</span><span class="pln"> UUID</span><span class="pun">=</span><span class="str">"5b6e7eb0-1e89-4127-b0b7-26a37be8fce4"</span><span class="pln"> TYPE</span><span class="pun">=</span><span class="str">"swap"</span>

接下来,在 /etc/fstab 中添加一个新行, 其中包含 UUID 和 swap 挂载选项:

<span class="pln">UUID</span><span class="pun">=</span><span class="lit">5b6e7eb0</span><span class="pun">-</span><span class="lit">1e89</span><span class="pun">-</span><span class="lit">4127</span><span class="pun">-</span><span class="pln">b0b7</span><span class="pun">-</span><span class="lit">26a37be8fce4</span><span class="pln"> none swap sw </span><span class="lit">0</span> <span class="lit">0</span>

现在我们的交换分区将在启动时自动挂载。

通过卸载并重新挂载交换来测试 /etc/fstab 条目是否正常工作:

<span class="pln">sudo swapoff </span><span class="pun">-</span><span class="pln">a
sudo swapon </span><span class="pun">-</span><span class="pln">a</span>

检查它是否已使用 swapon --show 重新启用

最后一步是使操作系统能够在需要时开始交换内存:

<span class="pln">sudo sysctl vm</span><span class="pun">.</span><span class="pln">swappiness</span><span class="pun">=</span><span class="lit">10</span>

这会将交换值设置为 10,这是桌面系统的正常级别。 我们现在有一个永久的交换分区,Linux 将使用它来优化性能!

故障 排除

创建和配置新的交换分区时可能会出现一些问题。以下是一些常见问题和解决方案。

  • 启动时未启用交换分区

如果重新启动后未启用新的交换区域,则 /etc/fstab 条目可能不正确。仔细检查 UUID 是否与输出 blkid 匹配,以及挂载选项是否正确。

  • 未使用交换空间

使用 cat /proc/sys/vm/swappiness 检查交换值。如果为 0,则系统根本不会交换。将其设置为较低的值,例如 10。此外,请确保 /etc/sysctl 中没有设置任何限制。会议

  • 内存不足错误

如果在添加更多交换后仍然出现内存不足错误,则交换空间对于您的工作负载来说可能太小。创建额外的交换分区或交换文件以添加更多容量。

  • 性能低下

过度交换会降低性能。确保交换值不要太高,并关闭资源密集型程序以减轻内存压力。添加更多 RAM 也可能有助于减少交换使用量。 请务必使用 top 或 htop 等工具监控交换使用情况 ,以查看新交换是否按预期使用。


Edge插件网 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:如何在 Linux 中创建交换分区
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址