Ubuntu 增加 swap 交换空间

本文首发于 https://blog.kangxuanpeng.com/post/ubuntu-add-swap-size


apt install 的时候遇到了错误 FATAL -> Failed to fork.

[root@Ubuntu:~]# apt install xxx
Reading package lists... Done
Building dependency tree       
Reading state information... Done
FATAL -> Failed to fork.

经过排查发现是交换空间不足,所以本文是在 Ubuntu 环境下增加 swap 交换空间。


查看当前系统 swap 大小


[root@Ubuntu:~]# free -m
              total        used        free      shared  buff/cache   available
Mem:            481         339           9          15         131         114
Swap:             0           0           0
[root@Ubuntu:~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            210M     0  210M   0% /dev
tmpfs            49M  656K   48M   2% /run
/dev/vda1       9.8G  3.1G  6.3G  34% /
tmpfs           241M     0  241M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           241M     0  241M   0% /sys/fs/cgroup
tmpfs            49M     0   49M   0% /run/user/1000

指定 swap 文件

/* 必须用 root 账号

[root@Ubuntu:~]# mkdir /swap
[root@Ubuntu:~]# cd /swap/
[root@Ubuntu:swap]# ls
[root@Ubuntu:swap]# dd if=/dev/zero of=swapfile bs=100M count=50
50+0 records in
50+0 records out
5242880000 bytes (5.2 GB, 4.9 GiB) copied, 43.7988 s, 120 MB/s
1 if=文件名:输入文件名,缺省为标准输入。即指定源文件。< if=input file >
2 of=文件名:输出文件名,缺省为标准输出。即指定目的文件。< of=output file >
3 ibs=bytes:一次读入bytes个字节,即指定一个块大小为bytes个字节。
4 cbs=bytes:一次转换bytes个字节,即指定转换缓冲区大小。
5 skip=blocks:从输入文件开头跳过blocks个块后再开始复制。
6 seek=blocks:从输出文件开头跳过blocks个块后再开始复制。
7 count=blocks:仅拷贝blocks个块,块大小等于ibs指定的字节数。

转换 swap 文件

[root@Ubuntu:swap]# mkswap -f swapfile
mkswap: swapfile: insecure permissions 0644, 0600 suggested.
Setting up swapspace version 1, size = 4.9 GiB (5242875904 bytes)
no label, UUID=18adba41-e214-48d6-a8d7-eb06d7e7137c
[root@Ubuntu:swap]# chmod 0600 /swap/swapfile 
[root@Ubuntu:swap]# mkswap -f swapfile
mkswap: swapfile: warning: wiping old swap signature.
Setting up swapspace version 1, size = 4.9 GiB (5242875904 bytes)
no label, UUID=148b286b-a2c6-4ae2-beff-d7d3e0403df0

激活 swap 文件

swapon 命令来激活,可以看到激活前后的交换空间大小变化

[root@Ubuntu:swap]# free -m
              total        used        free      shared  buff/cache   available
Mem:            481         326          17          15         137         127
Swap:             0           0           0
[root@Ubuntu:swap]# swapon /swap/swapfile
[root@Ubuntu:swap]# free -m
              total        used        free      shared  buff/cache   available
Mem:            481         328          15          15         137         125
Swap:          4999           0        4999

/* 取消挂载则是 swapoff swap

至此,成功增加了交换空间。又可以愉快地 apt install 了。

  1. Do you have a spam problem on this blog; I also am a blogger, and I was wondering your situation; we have created some nice practices and we are looking to exchange strategies with other folks, why not shoot me an e-mail if interested. asmr 0mniartist

  2. Good day! Do you know if they make any plugins to help with Search Engine Optimization? I'm trying to get my blog to rank for some targeted keywords but I'm not seeing very good success. If you know of any please share. Kudos! asmr 0mniartist

  3. I like the valuable information you provide in your articles. I will bookmark your blog and check again here regularly. I am quite sure I'll learn plenty of new stuff right here! Good luck for the next! 0mniartist asmr

  4. It's remarkable to go to see this web page and reading the views of all friends concerning this post, while I am also eager of getting know-how. 0mniartist asmr

  5. Cool blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple tweeks would really make my blog jump out. Please let me know where you got your design. With thanks 0mniartist asmr

  6. Dix

    The essays are to vary among 500-650 phrases. Consider it in this way because you begin to write: five hundred phrases is just one single spaced web page (250 words is just one Doublespaced webpage ), and that means you ought to write about an typical full page into page along with 50% , singlespaced articles. http://www.forumpropolis.org/2021/03/30/when-i-press-the-gas-i-hear-a-clicking-noise/ https://prolendingmortgage.com/what-is-9-20-as-a-decimal/