怎样手工建立交换文件?

系统安装、升级讨论
版面规则
我们都知道新人的确很菜,也喜欢抱怨,并且带有浓厚的Windows习惯,但既然在这里询问,我们就应该有责任帮助他们解决问题,而不是直接泼冷水、简单的否定或发表对解决问题没有任何帮助的帖子。乐于分享,以人为本,这正是Ubuntu的精神所在。
回复
头像
百草谷居士
帖子: 3910
注册时间: 2006-02-10 16:36
系统: Mint21.1/Deepin20.8

怎样手工建立交换文件?

#1

帖子 百草谷居士 » 2018-06-12 8:57

如果是在新安装ubuntu1804时,没有划出交换分区,系统会自动启用交换文件来替代交换分区,这也是Ubuntu现在推荐的方式。
可是问题是这样的,我在安装ubuntu1804时,已经存在交换分区了,现在又把交换分区删了,又不想重新安装,那么,如何手工建立交换文件并启用这种模式呢?
debian 12 / 深度系统 20.9 / Mint 21.3

为何热衷于搞发行版的多,搞应用程序开发的少?Linux最多余的就是各种发行版,最缺的就是应用程序,特别是行业应用程序。
头像
astolia
论坛版主
帖子: 6396
注册时间: 2008-09-18 13:11

Re: 怎样手工建立交换文件?

#2

帖子 astolia » 2018-06-12 9:32

又上不了google了? https://help.ubuntu.com/community/SwapF ... ap_file.3F
Create the Swap File:

We will create a 1 GiB file (/mnt/1GiB.swap) to use as swap:

代码: 全选

sudo fallocate -l 1g /mnt/1GiB.swap
fallocate size suffixes: g = Giga, m = Mega, etc. (See man fallocate).

If fallocate fails or it not available, you can use dd:

代码: 全选

sudo dd if=/dev/zero of=/mnt/1GiB.swap bs=1024 count=1048576
We need to set the swap file permissions to 600 to prevent other users from being able to read potentially sensitive information from the swap file.

代码: 全选

sudo chmod 600 /mnt/1GiB.swap
Format the file as swap:

代码: 全选

sudo mkswap /mnt/1GiB.swap
Enable use of Swap File

代码: 全选

sudo swapon /mnt/1GiB.swap
The additional swap is now available and verified with: cat /proc/swaps

Enable Swap File at Bootup

Add the swap file details to /etc/fstab so it will be available at bootup:

代码: 全选

echo '/mnt/1GiB.swap swap swap defaults 0 0' | sudo tee -a /etc/fstab
Example of making a swap file

This is an example of making and using a swap file on a computer with no swap partition.

代码: 全选

$ sudo fallocate -l 1g /mnt/1GiB.swap
$ sudo chmod 600 /mnt/1GiB.swap
$ sudo mkswap /mnt/1GiB.swap
Setting up swapspace version 1, size = 1048576 kB
$ sudo swapon /mnt/1GiB.swap
$ cat /proc/swaps 
Filename                                Type            Size    Used    Priority
/home/swapfile                          file            1048576 1048576 -1
$ echo '/mnt/4GiB.swap swap swap defaults 0 0' | sudo tee -a /etc/fstab
$ reboot
$ free -h
              total        used        free      shared  buff/cache   available
Mem:            15G        9.3G        454M        4.0G        5.8G        1.9G
Swap:          1.0G        1.0G          0B
头像
百草谷居士
帖子: 3910
注册时间: 2006-02-10 16:36
系统: Mint21.1/Deepin20.8

Re: 怎样手工建立交换文件?

#3

帖子 百草谷居士 » 2018-06-12 9:52

谢谢,我将交换文件设置为4G。好像用不到啊!

free -h
$ free -h
总计 已用 空闲 共享 缓冲/缓存 可用
内存: 7.7G 760M 6.0G 202M 1.0G 6.5G
交换: 4.0G 0B 4.0G
swapon
$ swapon
NAME TYPE SIZE USED PRIO
/home/4G.swap file 4G 0B -2
debian 12 / 深度系统 20.9 / Mint 21.3

为何热衷于搞发行版的多,搞应用程序开发的少?Linux最多余的就是各种发行版,最缺的就是应用程序,特别是行业应用程序。
头像
astolia
论坛版主
帖子: 6396
注册时间: 2008-09-18 13:11

Re: 怎样手工建立交换文件?

#4

帖子 astolia » 2018-06-12 10:11

有那么多快速的内存空着,干嘛要用慢的要死的交换文件?
非要玩的话

代码: 全选

sudo sysctl vm.swappiness=100
dd if=/dev/urandom of=/dev/shm/test bs=1M
头像
男菜鸟
帖子: 1351
注册时间: 2008-12-16 14:01
来自: 漂在江湖

Re: 怎样手工建立交换文件?

#5

帖子 男菜鸟 » 2018-06-13 22:49

mark
头像
百草谷居士
帖子: 3910
注册时间: 2006-02-10 16:36
系统: Mint21.1/Deepin20.8

Re: 怎样手工建立交换文件?

#6

帖子 百草谷居士 » 2018-06-14 14:11

我看了一下如果安装时没有交换分区,系统自设的交换文件在fstab文件的定义,是这样的:
swapfile none swap sw 0 0
swapfile 位于根节点。后面的选项还是有区别的。
debian 12 / 深度系统 20.9 / Mint 21.3

为何热衷于搞发行版的多,搞应用程序开发的少?Linux最多余的就是各种发行版,最缺的就是应用程序,特别是行业应用程序。
头像
tang.zhe
帖子: 1505
注册时间: 2010-07-15 13:31

Re: 怎样手工建立交换文件?

#7

帖子 tang.zhe » 2018-06-15 7:45

:em11
头像
速腾1994
论坛版主
帖子: 17379
注册时间: 2008-11-01 20:43
系统: Arch+gnome

Re: 怎样手工建立交换文件?

#8

帖子 速腾1994 » 2018-06-19 22:15

swap是不需要手动挂载的。只要有swap分区,系统会自动挂载的
回复