zl程序教程

您现在的位置是:首页 >  其他

当前栏目

ubuntu 22.04手动创建swapfile文件以配置swap(适用于未创建swap分区的情况)

文件配置Ubuntu 创建 情况 分区 手动 适用
2023-09-14 09:01:51 时间

在 Kubuntu 系统中创建 Swap 文件

在 Kubuntu 系统中创建 Swap 文件的方法与 Ubuntu 系统中的方法相同。下面是一个简单的示例,演示如何在 Kubuntu 系统中创建一个 10GB 的 Swap 文件:

  1. 创建一个空文件,大小为 10GB:
sudo fallocate -l 10G /swapfile
  1. 设置文件权限,只允许 root 用户读写:
sudo chmod 600 /swapfile
  1. 将文件格式化为 Swap 文件:
sudo mkswap /swapfile
  1. 激活 Swap 文件:
sudo swapon /swapfile
  1. 将 Swap 文件设置为永久性的,方法是将其路径添加到 /etc/fstab 文件中:
echo '/swapfile swap swap defaults 0 0' | sudo tee -a /etc/fstab

完成以上步骤后,您就成功地在 Kubuntu 系统中创建了一个永久性的 Swap 文件。

实操记录

zxl@linux:~$ sudo fallocate -l 10G /swapfile
zxl@linux:~$ ll /swapfile 
-rw------- 1 root root 10737418240 Apr 14 23:29 /swapfile
zxl@linux:~$ ll /swapfile -h
-rw------- 1 root root 10G Apr 14 23:29 /swapfile
zxl@linux:~$ sudo chmod 600 /swapfile
zxl@linux:~$ sudo mkswap /swapfile
mkswap: /swapfile: warning: wiping old swap signature.
Setting up swapspace version 1, size = 10 GiB (10737414144 bytes)
no label, UUID=a1fe8ba9-e228-4b3f-8297-05c22acbe397
zxl@linux:~$ free -m
               total        used        free      shared  buff/cache   available
Mem:           15859        2800        8660         522        4398       12218
Swap:              0           0           0
zxl@linux:~$ sudo swapon /swapfile
zxl@linux:~$ free -m
               total        used        free      shared  buff/cache   available
Mem:           15859        2809        8655         518        4394       12213
Swap:          10239           0       10239
zxl@linux:~$ 
zxl@linux:~$ echo '/swapfile swap swap defaults 0 0' | sudo tee -a /etc/fstab
/swapfile swap swap defaults 0 0
zxl@linux:~$