Linux_交换分区SWAP
一、交换分区SWAP
1️⃣:交换分区SWAP就是LINUX下的虚拟内存分区,它的作用是在物理内存使用完之后,将磁盘空间(也就是SWAP分区)虚拟成内存来使用。
2️⃣:交换分区一般指定虚拟内存的大小为实际内存的1~1.5倍。
3️⃣:如果实际内存超过8GB,可以直接划分16GB给虚拟内存即可,如果虚拟内存不够用的情况,须增加一个虚拟磁盘,由于不能给原有的磁盘重新分区,所以可以选择新建。
二、创建SWAP
1、方式一:创建SWAP文件
使用ddif=/dev/zeroof=/root/swapbs=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 //这是刚刚创建的交换分区
Linux_交换分区SWAP的更多相关文章
- Linux 交换分区swap
Linux 交换分区swap 一.创建和启用swap交换区 如果你的服务器的总是报告内存不足,并且时常因为内存不足而引发服务被强制kill的话,在不增加物理内存的情况下,启用swap交换区作为虚拟内存 ...
- Linux设置交换分区swap
参考: http://www.vpser.net/opt/vps-add-swap.html https://www.zntec.cn/archives/vps-swap.html http://yz ...
- mount挂载和交换分区swap
目录 mount挂载 挂载方法 选项 查看设备 卸载命令 文件挂载配置文件fstab 交换文件与分区 swap优先级 三个工具free,df,du 扩展 移动介质 使用光盘 挂载USB设备 mount ...
- linux 交换分区 swap
linux swap分区用来保证内存过载时也可以使用,是在磁盘级别对内存的一次扩展,swap分区必须是一个单独的分区 创建过程: 1.用fdisk 命令新建分区,在创建过程中通过L命令和t命令来调整分 ...
- Linux交换分区swap
一.SWAP 说明 1.1 SWAP 概述 当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用.那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被 ...
- 41 修改树莓派交换分区 SWAP 的大小
http://blog.lxx1.com/3289 SWAP就是LINUX下的虚拟内存分区,它的作用是在物理内存使用完之后,将磁盘空间(也就是SWAP分区)虚拟成内存来使用.它和Windows系统的交 ...
- CentOS设置交换分区swap
环境查看 查看未设置交换分区之前 free -h 新加一块磁盘用于交换分区/dev/sdc 格式化 mkswap /dev/sdc 设置为交换分区 swapon /dev/sdc 再次查看 设置为重启 ...
- 交换分区swap
一.查看当前的交换分区[root@server0 ~]# free -mtotal used free shared buff/cache availableMem: 489 140 145 ...
- 【转】交换分区SWAP
SWAP就是LINUX下的虚拟内存分区,它的作用是在物理内存使用完之后,将磁盘空间(也就是SWAP分区)虚拟成内存来使用. 它和Windows系统的交换文件作用类似,但是它是一段连续的磁盘空间,并且对 ...
随机推荐
- IndexError: list index out of range Python常见错误
引用超过list最大索引,此错误非常常见,注意列表的元素个数 ----------------------------------------------
- Distributed | Google File System
本文为我读GFS论文后做的总结,包括文件系统的读写流程,和一些重要机制. [Google File System] 设计理念 组件失效被认为是常态事件.GFS包括数百上千台普通设备,在任何时间都有可能 ...
- PBFT共识算法详解
PBFT(Practical Byzantine Fault Tolerance,实用拜占庭容错) 一.概述 拜占庭将军问题最早是由 Leslie Lamport 在 1982 年发表的论文<T ...
- IDEA - 返回上一步,回到下一步 代码 快捷键
回到上一步 ctrl + alt + < 回到下一步 ctrl + alt + >
- 22. VUE 插槽-详解
插槽 一直对插槽不理解,今天学习,并整理一下,希望日后可以灵活运用. (一)插槽内容 先简单来个例子,看一下插槽的租作用. 1.1 不使用插槽 父组件中: <div id="app&q ...
- JAVAEE_Servlet_09_Adapter适配器GenericServlet
适配器 GenericServlet * 适配器 (Adapter) - 适配器的作用? 1.我们目前所有的Servlet类都直接实现了javax.servlet.Servlet接口,但是该接口中有些 ...
- 谈谈react hooks的优缺点
前言Hook 是 React 16.8 的新增特性.它是完全可选的,并且100%向后兼容.它可以让你使用函数组件的方式,运用类组件以及 react 其他的一些特性,比如管理状态.生命周期钩子等.从概念 ...
- 1068 Find More Coins
Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...
- Linux-鸟菜-5-文件权限
Linux-鸟菜-5-文件权限 Linux作为多用户多任务的操作系统,文件权限以及目录配置相对于Windows的话应该更重要更细致一些.Linux一般将文件可存取的身份分为三个类别,分别是owner/ ...
- 动手实现一个适用于.NET Core 的诊断工具
前言 大家可能对诊断工具并不陌生,从大名鼎鼎的 dotTrace,到 .NET CLI 推出的一系列的高效诊断组件(dotnet trace,dotnet sos,dotnet dump)等, 这些工 ...