创建交换分区

本文详细介绍了在Linux系统下如何创建、格式化和激活交换分区。首先通过创建扩展分区和逻辑分区,然后将逻辑分区类型更改为82用于标识为交换分区,接着使用mkswap进行格式化,最后通过swapon临时和永久激活交换分区。通过UUID在/etc/fstab文件中配置,确保重启后仍能自动激活。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建交换分区

查看内存信息
(物理内存,虚拟内存)

[root@servera ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1790         218        1333           8         237        1409
Swap:          2047           0        2047

创建指定大小的分区作为swap分区

从磁盘中创建出一块分区,交换分区就是把磁盘一段空间作为交换空间
1.创建3G的扩展分区
Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): e     扩展分区
Partition number (1-4, default 1): 
First sector (2048-20971519, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): +3G
2.打印磁盘分区的情况
Command (m for help): p
Disk /dev/nvme0n2: 10 GiB, 10737418240 bytes, 20971520 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: 0x97988063

Device         Boot Start     End Sectors Size Id Type
/dev/nvme0n2p1       2048 6293503 6291456   3G  5 Extended
3.创建出1G的逻辑分区来作为
Command (m for help): n
Partition type
   p   primary (0 primary, 1 extended, 3 free)
   l   logical (numbered from 5)
Select (default p): l

Adding logical partition 5
First sector (4096-6293503, default 4096): 
Last sector, +sectors or +size{K,M,G,T,P} (4096-6293503, default 6293503): +1G

Created a new partition 5 of type 'Linux' and of size 1 GiB.

更改分区类型

(为了方便标识swap交换分区)

l 查看分区类型
82是用来标识交换分区的
Command (m for help): l

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden or  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi ea  Rufus alignment
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         eb  BeOS fs        
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ee  GPT            
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f1  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f4  SpeedStor      
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      f2  DOS secondary  
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fb  VMware VMFS    
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fc  VMware VMKCORE 
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fd  Linux raid auto
1c  Hidden W95 FAT3 75  PC/IX           bc  Acronis FAT32 L fe  LANstep        
1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot    ff  BBT 
t 更改分区类型
Command (m for help): t
选择更改分区类型的编号
Partition number (1,5, default 5):   
选择要更改的类型编号 
Hex code (type L to list all codes): 82  

Changed type of partition 'Linux' to 'Linux swap / Solaris'.
P 打印分区表
p  打印分区表
Command (m for help): p
Disk /dev/nvme0n2: 10 GiB, 10737418240 bytes, 20971520 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: 0x97988063

Device         Boot Start     End Sectors Size Id Type
/dev/nvme0n2p1       2048 6293503 6291456   3G  5 Extended
/dev/nvme0n2p5       4096 2101247 2097152   1G 82 Linux swap / Solaris

Filesystem/RAID signature on partition 1 will be wiped.

交换分区格式化

[root@servera ~]# mkswap /dev/nvme0n2p5
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=44059b15-d980-4e5e-9fc9-4206318477f1

激活交换分区

临时激活
[root@servera ~]# swapon /dev/nvme0n2p5

查看内存信息

[root@servera ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1790         224        1297           8         268        1403
Swap:          3071           0        3071

查看对应交换分区设备名称

[root@servera ~]# swapon -s
Filename				Type		Size	Used	Priority
/dev/dm-1                              	partition	2097148	0	-2
/dev/nvme0n2p5                         	partition	1048572	0	-3
永久激活

使用UUID激活
(推荐)

查看/dev/nvme0nep5的UUID
[root@servera ~]# blkid  /dev/nvme0n2p5
/dev/nvme0n2p5: UUID="44059b15-d980-4e5e-9fc9-4206318477f1" TYPE="swap" PARTUUID="97988063-05"
编辑文件系统的配置文件
[root@servera ~]# vim /etc/fstab
编辑内容:
UUID=44059b15-d980-4e5e-9fc9-4206318477f1   swap   swap defaults 0 0 

使用绝对路径激活

编辑文件系统的配置文件
[root@servera ~]# vim /etc/fstab
编辑内容:
/dev/nvme0n2p5   swap    swap defaults 0 0 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值