一、交换分区SWAP
1️⃣:交换分区SWAP就是LINUX下的虚拟内存分区,它的作用是在物理内存使用完之后,将磁盘空间(也就是SWAP分区)虚拟成内存来使用。
2️⃣:交换分区一般指定虚拟内存的大小为实际内存的1~1.5倍。
3️⃣:如果实际内存超过8GB,可以直接划分16GB给虚拟内存即可,如果虚拟内存不够用的情况,须增加一个虚拟磁盘,由于不能给原有的磁盘重新分区,所以可以选择新建。
二、创建SWAP
1、方式一:创建SWAP文件
使用
dd
if
=
/dev/zero
of=
/root/swap
bs=1M count=2048
创建2G大小的文件
- if:指定源- -般写/dev/zero
- of:指定目标
- bs:定义块大小
- count:数量
- if:指定源- -般写/dev/zero
[root@localhost ~]# dd if=/dev/zero of=/root/swap bs=1M count=2048 2048+0 records in 2048+0 records out 2147483648 bytes (2.1 GB, 2.0 GiB) copied, 44.8512 s, 47.9 MB/s [root@localhost ~]# du -sh /root/swap 2.0G /root/swap
- 格式化刚刚创建的swap文件
[root@localhost ~]# mkswap -f /root/swap // -f :指定格式化文件(目标是文件,则加-f ;若为磁盘分区,则不需要加-f) mkswap: /root/swap: insecure permissions 0644, 0600 suggested. Setting up swapspace version 1, size = 2 GiB (2147479552 bytes) no label, UUID=20e1675b-3206-497c-b33b-9175be73083f
- 根据提示设置改格式化后文件的权限为600(此步骤也可以不做)
[root@localhost ~]# ll swap -rw-r--r-- 1 root root 2147483648 Aug 20 03:03 swap [root@localhost ~]# chmod 600 /root/swap [root@localhost ~]# ll swap -rw------- 1 root root 2147483648 Aug 20 03:03 swap
- 查看刚刚格式化swap文件的属性
[root@localhost ~]# blkid /root/swap /root/swap: UUID="20e1675b-3206-497c-b33b-9175be73083f" TYPE="swap"
- 查看当前SWAP分区情况
[root@localhost ~]# free -h total used free shared buff/cache available Mem: 1.8Gi 207Mi 65Mi 5.0Mi 1.5Gi 1.4Gi Swap: 2.0Gi 3.0Mi 2.0Gi
- 挂载SWAP分区
[root@localhost ~]# swapon /root/swap [root@localhost ~]# free -h total used free shared buff/cache available Mem: 1.8Gi 209Mi 64Mi 5.0Mi 1.5Gi 1.4Gi Swap: 4.0Gi 3.0Mi 4.0Gi
- 卸载SWAP分区
[root@localhost ~]# swapoff /root/swap [root@localhost ~]# free -h total used free shared buff/cache available Mem: 1.8Gi 207Mi 110Mi 5.0Mi 1.5Gi 1.4Gi Swap: 2.0Gi 3.0Mi 2.0Gi
2、方式二:使用磁盘分区创建SWAP分区
- 创建一个磁盘主分区
[root@localhost ~]# fdisk /dev/nvme0n2 Welcome to fdisk (util-linux 2.32.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): n Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-104857599, default 2048): Last sector, +sectors or +size{K,M,G,T,P} (2048-104857599, default 104857599): +4G Created a new partition 1 of type 'Linux' and of size 4 GiB. Partition #1 contains a swap signature. The signature will be removed by a write command. Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
- 调整刚刚创建的磁盘分区ID为82
[root@localhost ~]# fdisk /dev/nvme0n2 Welcome to fdisk (util-linux 2.32.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): p //查看分区列表 Disk /dev/nvme0n2: 50 GiB, 53687091200 bytes, 104857600 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0x8f03f9d8 Device Boot Start End Sectors Size Id Type /dev/nvme0n2p1 2048 8390655 8388608 4G 83 Linux Command (m for help): t //更改分区ID Selected partition 1 //默认显著磁盘的第一个分区;如果有多个分区可选,就需要手动选择分区 Hex code (type L to list all codes): 82 //输入分区编号82;可以使用 ‘l’查看看所有的分区ID Changed type of partition 'Linux' to 'Linux swap / Solaris'. Command (m for help): p //查看分区列表 Disk /dev/nvme0n2: 50 GiB, 53687091200 bytes, 104857600 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0x8f03f9d8 Device Boot Start End Sectors Size Id Type /dev/nvme0n2p1 2048 8390655 8388608 4G 82 Linux swap / Solaris //id 为82 ;type 为 Linux swap Command (m for help): w //保存退出 The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
- 格式化刚刚创建的磁盘分区
[root@localhost ~]# mkswap /dev/nvme0n2p1 Setting up swapspace version 1, size = 4 GiB (4294963200 bytes) no label, UUID=64bff483-41ba-4180-ae1c-eab2e1075c84
[root@localhost ~]# blkid /dev/nvme0n2p1 /dev/nvme0n2p1: UUID="64bff483-41ba-4180-ae1c-eab2e1075c84" TYPE="swap" PARTUUID="8f03f9d8-01"
- 在/etc/fstab文件写入永久挂载(开机自动挂载)
//先查看一下SWAP分区的UUID UUID="64bff483-41ba-4180-ae1c-eab2e1075c84" //在/etc/fstab文件写入内容 [root@localhost ~]# vim /etc/fstab .......... UUID="64bff483-41ba-4180-ae1c-eab2e1075c84" swap swap defaults 0 0
- 挂载SWAP分区
//手下查看一下SWAP分区情况 [root@localhost ~]# free -h total used free shared buff/cache available Mem: 1.8Gi 204Mi 1.3Gi 8.0Mi 253Mi 1.4Gi Swap: 2.0Gi 0B 2.0Gi //挂载SWAP分区 [root@localhost ~]# swapon /dev/nvme0n2p1 //查看挂载后的SWAP分区情况 [root@localhost ~]# free -h total used free shared buff/cache available Mem: 1.8Gi 208Mi 1.3Gi 8.0Mi 253Mi 1.4Gi Swap: 6.0Gi 0B 6.0Gi
- 卸载SWAP分区
//查看SWAP分区情况 [root@localhost ~]# free -h total used free shared buff/cache available Mem: 1.8Gi 208Mi 1.3Gi 8.0Mi 253Mi 1.4Gi Swap: 6.0Gi 0B 6.0Gi //卸载SWAP分区 [root@localhost ~]# swapoff /dev/nvme0n2p1 //查看卸载后SWAP分区情况 [root@localhost ~]# free -h total used free shared buff/cache available Mem: 1.8Gi 204Mi 1.3Gi 8.0Mi 253Mi 1.4Gi Swap: 2.0Gi 0B 2.0Gi
- 使用swapon -s 查看所有在使用交换分区
[root@localhost ~]# swapon -s Filename Type Size Used Priority /dev/dm-1 partition 2146300 0 -2 /dev/nvme0n2p1 partition 4194300 0 -3 //这是刚刚创建的交换分区