如何在Ubuntu 18.04中增加swap文件大小?

我有一台配备8GB内存和1TB硬盘的笔记本电脑。我有一个2GB的交换文件(Ubuntu 18.04默认使用交换文件而不是单独的交换分区),我想增加它以便使用休眠功能。
我想将其从2GB增加到16GB。这是GParted的屏幕截图:

GParted screenshot

我试过用fallocate -l 16G去增加它,但是没有成功。
此外,这里还有一个来自free -m的图像:

<code>free</code> output


这是为了交换文件,我想增加交换分区。我不认为这个线程与其他人的重复。 - Miroslav
从你发布的截图来看,没有交换分区。我猜你已经有一个交换文件了。据我所知,Ubuntu 18.04默认创建交换文件而不是交换分区。 - Thomas
2哦,是的,实际上从swapon --show命令可以看到/swapfile确实存在。我会尝试这个指南。 - Miroslav
如果您有8GB的RAM,只需要8GiB的交换空间。但是为什么您要使用休眠功能呢?Ubuntu在没有启用休眠功能时启动非常快速。 - oldfred
如果您想随意添加额外的交换文件,可以使用以下脚本:https://askubuntu.com/a/931170/295286 - Sergiy Kolodyazhnyy
1请您将文本文件、对话消息和程序输出列表以文本形式发布,而不是以图像形式。要实现后两者,您可以选择1)选择、复制并粘贴对话文本或终端内容,或者2)将程序输出保存到文件中,然后使用该文件。较长的列表(编辑器会告诉您什么长度过长)应上传到pastie服务并在问题中提供链接。谢谢。 - David Foerster
@oldfred,在笔记本电脑上使用休眠模式非常好,因为它不会消耗任何电量(与睡眠模式不同),而且你的程序会继续运行,就像你离开时一样。此外,在定期停电或长时间停电的情况下,如果你有UPS,休眠模式也非常适用。 - Chai T. Rex
我和其他人最近安装了Ubuntu 18.04,并创建了一个交换分区。在我的情况下(8GB内存的机器),交换分区只有1GB,这非常低。就像链接问题中的提问者一样,我只是打开了Firefox和一个IDE,就遇到了内存不足的错误。鉴于Ubuntu仍然没有任何机制来警告内存不足,我认为选择这种交换方式很愚蠢。 - Dan Dascalescu
2个回答

从Ubuntu 18.04开始,使用交换文件而不是专用的交换分区(除非使用LVM)。交换文件的名称为swapfile。要更改此交换文件的大小:
禁用交换文件并删除它(实际上不需要,因为您将覆盖它) sudo swapoff /swapfile sudo rm /swapfile 创建所需大小的新交换文件。感谢用户Hackinet,您可以使用以下命令创建一个4 GB的交换文件 sudo fallocate -l 4G /swapfile 在此命令中,将4G调整为所需的大小。
或者,您可以使用dd命令,但需要进行一些计算以提供正确的参数。如果要创建一个4 GB的交换文件,您需要写入4 * 1024个块,每个块大小为1024^2字节(= 1 MiB)。这将使您的计数等于4 * 1024 = 4096。使用以下命令创建这个大小的文件 sudo dd if=/dev/zero of=/swapfile bs=1M count=4096
仅为root分配读写权限(虽然不是必需的,但会增强安全性) sudo chmod 600 /swapfile 将文件格式化为交换文件: sudo mkswap /swapfile 该文件将在下次重新启动时被激活。如果要在当前会话中激活它: sudo swapon /swapfile 你可以使用命令swapon -s来查看可用的交换空间(无需管理员权限)。

2这个方法非常有效,但不幸的是,在我的设置中,它无法在重新启动后保留下来,因为我使用了加密分区: nvme0n1p3_crypt -> ubuntu--vg-root / ubuntu--vg-swap_1 你有什么办法可以让它持久化吗? - rypel
1bs=1M是最快的块大小吗?如果我改变它会发生什么? - Ben Winding
@rypel 你需要在 /etc/fstab 中进行一项输入。 - heynnema
2你需要在你的回答中添加文本,其中包括对/etc/fstab所需的修改。 - heynnema
1从Ubuntu 18.04开始,使用交换文件而不是专用的交换分区。但根据这个问题以及我上周安装Ubuntu 18.04.3的经验来看,情况似乎已经改变了。至于交换分区具体在哪里,我也不太清楚,可以参考这个链接 - Dan Dascalescu
好吧,我试图取消投票,但是出现了“除非编辑此答案,否则您的投票将被锁定。”在您进行编辑后,我会再次点赞。 - Dan Dascalescu
1在第二步中,要跳过所有的计算,您可以执行sudo fallocate -l 4G /swapfile。只需将4G的值更改为您想要的任何大小,如1G、2G、3G等。 - Hackinet
1@Hackinet,非常感谢!确实更加优雅!我已经将这个内容包含在答案中了。 - vanadium
这太棒了!我要注意的一件事是,如果在运行swapoff时交换空间正在使用中,它会增加内存压力;换句话说,它试图将交换空间中的内容移动到主内存中。因此,在实际内存不足时,你不应该尝试这样做,否则在尝试增加交换空间时可能会遇到OOM killer的问题 :-) - Dan
1也适用于Ubuntu 20.04,谢谢。 - partizanos

man mkswap中建议使用dd命令,如@vanadium post所示。
If  you  don't  know  the  page  size  that  your  machine uses, 
you may be able to look it up with 
"cat /proc/cpuinfo" 
(or you may not – the contents of this file depend on architecture and kernel version).

   To set up a swap file, it is necessary to create that file before   
   initializing  it  with  mkswap,  e.g. using a command like

          # fallocate --length 8GiB swapfile

   Note  that  a  swap  file must not contain any holes.  Using cp(1) to  
   create the file is not acceptable.
   Neither is use of fallocate(1) on file systems that support preallocated 
   files, such as XFS or ext4,  or on  copy-on-write  filesystems like btrfs.  

   It is recommended to use dd(1) and /dev/zero in these cases.
   Please read notes from swapon(8) before adding a swap file to copy-on- 
   write filesystems.

这里是man swapon的注释。
NOTES
       You should not use swapon on a file with holes.  This can be seen in
       the system log as

              swapon: swapfile has holes.

       The swap file implementation in the kernel expects to be able to write  
       to the file directly, without the assistance  of the filesystem.  This 
       is a problem on preallocated files (e.g.  fallocate(1)) on filesys‐
       tems like XFS or ext4, and on copy-on-write filesystems like btrfs.

       It is recommended to use dd(1) and /dev/zero to avoid holes on XFS
       and ext4.

       swapon may not work correctly when using a swap file with some  
       versions of btrfs.  This is due to  btrfs being  a copy-on-write 
       filesystem: the file location may not be static and corruption can 
       result.  
       Btrfs actively disallows the use of swap files on its filesystems
       by refusing to map the file.

       One possible workaround is to map the swap file to a loopback device.  
       This will allow the filesystem to determine the mapping properly but  
       may come with a performance impact.

       Swap over NFS may not work.