Linux swap creation and configuration

Under Linux, similar to the role swap "virtual memory" under the Windows system. When the physical memory, disk space part out when the partition SWAP (virtual memory to) use, in order to address the case of insufficient memory capacity. Under Linux swap implemented in two forms, one is achieved by a physical swap disk partition, the other is achieved by means of the virtual memory swap file.

First, the physical disk partitions to achieve swap

1, created by the partition software partition , under the partition type fdisk to 82, under gdisk to 8200, partep partition tag is not defined swap, in fact, does not change after the partition partition type will do the test can also be used, but for easy viewing in the partitioning tool the best type to swap partition corresponding to partition type, partition type appreciated that the type of file system software is used to partition the partition described to be carried, such as partition type 83 expressed linxu partitions formatted for ext3 ext4 and other linux file system, method partitions created here do not write there are ways partition you created earlier.

2, the command using the Format the partition mkswap swap partition
-------------------------------------- -----------------------------
root @ Debian: ~ # mkswap / dev / sdb1
Setting up swapspace Version 1, size = 1024 MiB (1073737728 bytes) # swap capacity for. 1G
NO label, the UUID = 47fe2c83-c4b9-4ee5-ad91-4a9642f66df5
the root Debian @: ~ #
------------------- ------------------------------------------------

3, the swap partition, use swapon / dev / sdb1 take effect
------------------------------------- ------------------------------
root @ Debian: ~ # # to see -h as Free before swap did not increase the capacity of
         total used free shared BUFF / Cache the Available
Mem: 8.5M 311M 1.6G 1.4G 2.0G 250M
Swap: 2.0G 2.0G # 0B ago did not increase the capacity of 2G

root @ Debian: ~ # swapon / dev / sdb1 # swap the entry into force of the newly added
root @ debian: ~ # free -h # When you are finished adding capacity
         Total Used as Free Shared BUFF / Cache the Available
Mem: 8.5M 311M 1.6G 1.4G 2.0G 250M
Swap: 3.0G 3.0G # 0B finished adding capacity into a 3G
root @debian: ~ #
--------------------------------------------- ----------------------

4, Linux swapon command parameters and usage
swapon is open swap, there will be a relative of a swap instruction to close, swapoff

swapon [options] [device]

-h Displays help information
-V Display version information
-v show verbose mode
-s Display swap usage, you can view all active swap
-a The / etc / fstab file all settings enabled device swap the
-p set the priority right, you can choose a number 0-32767 to him in the middle. Or in the / etc / fstab inside with pri = [value] ([value ] is a number from 0 to 32767 intermediate), then you can easily use swapon -a directly to start them, and have priority setting . Example: shows the usage of the swap ------------------------------------------- -------------------------- the root Debian @: ~ # the swapon -s Filename the Type Size Used the Priority / dev / sda5 2.0951 million Partition # 0 -1 creates together with the system installed swap priority. 1 / dev / swap priority created sdb1 partition 1048572 0 -2 # 2 after, the partition type, size. 1G the root Debian @: ~ # ------- -------------------------------------------------- ------------
  







5, Linux swapoff command parameters and usage

swapon [options] [device]

-a The / etc / fstab file all settings turned off to swap equipment

举例:
---------------------------------------------------------------------
root@debian:~# swapoff /dev/sdb1     # 关闭swap分区/dev/sdb1
root@debian:~# swapon -s
Filename    Type      Size     Used   Priority
/dev/sda5   partition   2095100    0     -1
root@debian:~#
---------------------------------------------------------------------

6、将新添加的交换分区添加到/etc/fstab文件中使之开机启动

根据不同的发行版将下面的内容添加到/etc/fstab中,添加完后最好用swapon -a命令查看添加的是否有问题

UUID=47fe2c83-c4b9-4ee5-ad91-4a9642f66df5   none    swap    sw   0    0 #Debian9.5默认的格式,UUID为对应的交换分区UUID

UUID=47fe2c83-c4b9-4ee5-ad91-4a9642f66df5    swap  swap   defaults    0   0 #CentOS7-1810与openSUSE15默认的格式,UUID为对应的交换分区UUID

二、使用文件来实现swap
当系统内没有剩余可用的分区时我们可以用在Linux的某个目录下创建一个空白文件,通过把这个文件格式化成swap从而实现创建swap的过程。

1、通过dd命令在/tmp/目录下新增加一个1G大小的空文件
---------------------------------------------------------------------
root@debian:~# dd if=/dev/zero  of=/tmp/swap  bs=1G  count=1   #增加一个1G大小的空文件
记录了1+0 的读入
记录了1+0 的写出
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 5.46136 s, 197 MB/s
root@debian:~# chmod 600 /tmp/swap        #修改文件权限,只有root读写
root@debian:~# ls -lh /tmp/swap           #查看创建的文件大小和权限
-rw------- 1   root   root   1.0G  8月 26 14:25   /tmp/swap
root@debian:~#
---------------------------------------------------------------------

2、使用mkswap命令把创建的文件格式化为swap
---------------------------------------------------------------------
root@debian:~# mkswap /tmp/swap
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=a0aa03d4-406d-4ecd-af04-12998e408192
root@debian:~#
---------------------------------------------------------------------

3、使用swapon命令使swap生效
---------------------------------------------------------------------
root@debian:~# swapon   /tmp/swap
root@debian:~# swapon -s
Filename    Type      Size   Used   Priority
/dev/sda5   partition   2095100   0    -1
/tmp/swap   file     1048572    0    -2      #创建成功
root@debian:~#
---------------------------------------------------------------------

4、将新添加的交换分区添加到/etc/fstab文件中使之开机启动

根据不同的发行版将下面的内容添加到/etc/fstab中,添加完后最好用swapon -a命令查看添加的是否有问题
---------------------------------------------------------------------
/tmp/swap    none    swap    sw    0    0    #Debian9.5默认的格式,这里不要用UUID,这是因为系统只会查询块设备,不会查询文件

/tmp/swap    swap   swap   defaults    0   0     #CentOS7-1810与openSUSE15默认的格式

---------------------------------------------------------------------

Guess you like

Origin www.cnblogs.com/pipci/p/11413433.html
Recommended