杰力 發表於 2025-6-5 00:00:00

Ubuntu调整swap大小的详细教程

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>1. 找到swap文件位置</li><li>2. 停止swap文件</li><li>3. 删除swap文件</li><li>4. 创建新的swap文件,以创建64G大小为例</li><li>5. 启用新的swap文件</li><li>6. 查看当前swap文件大小</li><li>7. 永久启用swap</li><li>Ubuntu修改Swap交换空间大小<ul class="second_class_ul"><li>一、查看本机Swap交换空间的内存大小</li><li>二、创建Swap文件</li><li>三、激活Swap文件</li><li>四、设置为永久Swap</li><li>五、扩展</li></ul></li></ul></div><p class="maodian"></p><h2>1. 找到swap文件位置</h2>
<div class="dxycode"><pre class="brush:bash;">cat /proc/swaps</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250605/2-2506051G22S48.png" /></p>
<p>说明swap文件名称为swapfile,位于 / 根目录下</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250605/2-2506051G22XJ.png" /></p>
<p class="maodian"></p><h2>2. 停止swap文件</h2>
<div class="dxycode"><pre class="brush:bash;">sudo swapoff /swapfile</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250605/2-2506051G22R30.png" /></p>
<p class="maodian"></p><h2>3. 删除swap文件</h2>
<div class="dxycode"><pre class="brush:bash;">sudo rm /swapfile</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250605/2-2506051G229617.png" /></p>
<p>在根目录下找不到swapfile,说明删除成功</p>
<p class="maodian"></p><h2>4. 创建新的swap文件,以创建64G大小为例</h2>
<div class="dxycode"><pre class="brush:bash;">sudo dd if=/dev/zero of=/swapfile bs=1GB count=64</pre></div>
<p>Linux dd 命令用于读取、转换并输出数据。dd 可从标准输入或文件中读取数据,根据指定的格式来转换数据,再输出到文件、设备或标准输出。</p>
<div class="dxycode"><pre class="brush:bash;">if=文件名:输入文件名,默认为标准输入。即指定源文件。
of=文件名:输出文件名,默认为标准输出。即指定目的文件。
ibs=bytes:一次读入bytes个字节,即指定一个块大小为bytes个字节。
obs=bytes:一次输出bytes个字节,即指定一个块大小为bytes个字节。
bs=bytes:同时设置读入/输出的块大小为bytes个字节。
cbs=bytes:一次转换bytes个字节,即指定转换缓冲区大小。
skip=blocks:从输入文件开头跳过blocks个块后再开始复制。
seek=blocks:从输出文件开头跳过blocks个块后再开始复制。
count=blocks:仅拷贝blocks个块,块大小等于ibs指定的字节数。
conv=&lt;关键字&gt;,关键字可以有以下11种:
conversion:用指定的参数转换文件。
ascii:转换ebcdic为ascii
ebcdic:转换ascii为ebcdic
ibm:转换ascii为alternate ebcdic
block:把每一行转换为长度为cbs,不足部分用空格填充
unblock:使每一行的长度都为cbs,不足部分用空格填充
lcase:把大写字符转换为小写字符
ucase:把小写字符转换为大写字符
swap:交换输入的每对字节
noerror:出错时不停止
notrunc:不截短输出文件
sync:将每个输入块填充到ibs个字节,不足部分用空(NUL)字符补齐。
--help:显示帮助信息
--version:显示版本信息</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250605/2-2506051G2294K.png" /></p>
<p class="maodian"></p><h2>5. 启用新的swap文件</h2>
<div class="dxycode"><pre class="brush:bash;"># 先修改文件权限
sudo chmod 600 /swapfile
# 然后设置为swap文件
sudo mkswap -f /swapfile
# 最后启用swap文件
sudo swapon /swapfile</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250605/2-2506051G230M3.png" /></p>
<p class="maodian"></p><h2>6. 查看当前swap文件大小</h2>
<div class="dxycode"><pre class="brush:bash;">free --giga</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250605/2-2506051G230141.png" /></p>
<div class="dxycode"><pre class="brush:bash;">用法:
free [选项]
选项:
-b, --bytes         以字节显示输出
   --kilo          使用千字节显示输出
   --mega          使用兆字节显示输出
   --giga          使用吉字节显示输出
   --tera          使用太字节显示输出
   --peta          show output in petabytes
-k, --kibi          show output in kibibytes
-m, --mebi          show output in mebibytes
-g, --gibi          show output in gibibytes
   --tebi          show output in tebibytes
   --pebi          show output in pebibytes
-h, --human         显示人类可读的输出
   --si            使用 1000 的倍数而非使用 1024
-l, --lohi          show detailed low and high memory statistics
-t, --total         show total for RAM + swap
-s N, --seconds N   repeat printing every N seconds
-c N, --count N   repeat printing N times, then exit
-w, --wide          宽版输出
   --help   显示此帮助信息并退出
-V, --version显示程序版本然后离开</pre></div>
<p>在根目录下也能看到新建的swapfile</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250605/2-2506051G230564.png" /></p>
<p class="maodian"></p><h2>7. 永久启用swap</h2>
<div class="dxycode"><pre class="brush:bash;">vim /etc/fstab</pre></div>
<p>swapfile所在行内容如下即可</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250605/2-2506051G231225.png" /></p>
<p class="maodian"></p><h2>Ubuntu修改Swap交换空间大小</h2>
<p class="maodian"></p><h3>一、查看本机Swap交换空间的内存大小</h3>
<p>命令如下</p>
<div class="dxycode"><pre class="brush:bash;">free -m</pre></div>
<p>执行结果如下:</p>
<div class="dxycode"><pre class="brush:bash;">longzhiye@longzhiye-laptop:~$ free -m
            total      used      free      sharedbuff/cache   available
Mem:          31995         374       30603         9      1016       31142
Swap:         976          65         911</pre></div>
<p>可以看到本机的Swap空间为976M左右,即1G空间。</p>
<p>下面我们将其扩大64G</p>
<p class="maodian"></p><h3>二、创建Swap文件</h3>
<div class="dxycode"><pre class="brush:bash;">sudo -i</pre></div>
<p>输入密码后,切换root。</p>
<div class="dxycode"><pre class="brush:bash;">mkdir /swap</pre></div>
<p>建立swap文件夹</p>
<div class="dxycode"><pre class="brush:bash;">cd /swap/</pre></div>
<p>进入到swap文件夹</p>
<div class="dxycode"><pre class="brush:bash;">sudo dd if=/dev/zero of=swapfile bs=64M count=1k</pre></div>
<p>建立swapfile,大小为bs*count = 64M * 1k = 64G</p>
<div class="dxycode"><pre class="brush:bash;">sudo mkswap -f swapfile</pre></div>
<p>将生成的文件转换为Swap文件</p>
<p>执行结果如下:</p>
<div class="dxycode"><pre class="brush:bash;">longzhiye@longzhiye-laptop:~$ sudo -i
longzhiye 的密码:
root@longzhiye-laptop:~# cd /
root@longzhiye-laptop:/# mkdir /swap
root@longzhiye-laptop:/# cd swap/
root@longzhiye-laptop:/swap# sudo dd if=/dev/zero of=swapfile bs=64M count=1k
记录了1024+0 的读入
记录了1024+0 的写出
68719476736 bytes (69 GB, 64 GiB) copied, 276.311 s, 249 MB/s
root@longzhiye-laptop:/swap# sudo mkswap -f swapfile
Setting up swapspace version 1, size = 64 GiB (68719472640 bytes)
无标签, UUID=c7feaf13-7f02-4941-a07f-86a43bdf3ef5</pre></div>
<p class="maodian"></p><h3>三、激活Swap文件</h3>
<div class="dxycode"><pre class="brush:bash;">sudo swapon swapfile</pre></div>
<div class="dxycode"><pre class="brush:bash;">free -m</pre></div>
<p>激活Swap文件,并重新查看Swap空间 内存</p>
<p>执行结果如下:</p>
<div class="dxycode"><pre class="brush:bash;">root@longzhiye-laptop:/swap# sudo swapon swapfile
swapon: /swap/swapfile:不安全的权限 0644,建议使用 0600。
root@longzhiye-laptop:/swap# free -m
            total      used      free      sharedbuff/cache   available
Mem:          31995         381         374         9       31238       31065
Swap:         66512          64       66448</pre></div>
<p>可以看到Swap空间内存从1G变为64G = 我们扩大的64G+原始的1G。</p>
<p class="maodian"></p><h3>四、设置为永久Swap</h3>
<p>未做此步骤,重启之前的设置都失效!!!!</p>
<div class="dxycode"><pre class="brush:bash;">sudo gedit /etc/fstab</pre></div>
<p>更改swap路径。</p>
<div class="dxycode"><pre># /etc/fstab: static file system information.
#
# Use &#39;blkid&#39; to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
#               
# / was on /dev/sda2 during installation
UUID=b726da21-83a0-497b-b3eb-a09f16403d60 /               ext4    errors=remount-ro 0       1
# /boot/efi was on /dev/sda1 during installation
UUID=C0BE-3630/boot/efi       vfat    umask=0077      0       1
#UUID=c7bb6d12-b247-4119-9c27-797f9995884e none            swap    sw            0       0
/swap/swapfile            swap            swap    sw            0       0</pre></div>
<p class="maodian"></p><h3>五、扩展</h3>
<p>如果需要卸载这个 swap 文件,可以进入建立的 swap 文件目录。执行下列命令。</p>
<div class="dxycode"><pre class="brush:bash;">sudo swapoff swapfile</pre></div>
<p>如果需要一直保持这个 swap ,可以把它写入 /etc/fstab 文件。</p>
<div class="dxycode"><pre class="brush:bash;">/swap/swapfile /swap swap defaults 0 0</pre></div>
<p>以上就是Ubuntu调整swap大小的详细内容,更多相关资料请阅读琼殿技术社区其它文章!</p>
頁: [1]
查看完整版本: Ubuntu调整swap大小的详细教程