linux 使用分区空间添加swap

当Linux系统内存不足且swap分区容量不够时,可以通过手动创建新的分区来扩展swap空间。本文介绍了通过分区方法实现swap分区扩展的具体操作步骤。

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

ENV:

[test@centos7 ~]$ uname -r
3.10.0-514.el7.x86_64
[test@centos7 ~]$ cat /etc/redhat-release 
CentOS Linux release 7.3.1611 (Core) 
[test@centos7 ~]$ free -ht
              total        used        free      shared  buff/cache   available
Mem:           1.8G        123M        1.5G        8.4M        142M        1.5G
Swap:          2.0G          0B        2.0G
Total:         3.8G        123M        3.5G

当系统内存不够,swap分区也不够时,linux系统可以进行手动的添加swap分区,实现对系统swap分区的扩展。百度搜索发现有好几种方法添加swap,其他方法没有操作过,不知道效果如何,这里对分区实现swap的过程。具体操作:

[test@centos7 ~]$ lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   20G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   19G  0 part 
  ├─cl-root 253:0    0   17G  0 lvm  /
  └─cl-swap 253:1    0    2G  0 lvm  [SWAP]
sdb           8:16   0    8G  0 disk 
sr0          11:0    1  7.7G  0 rom 
添加硬盘sdb
[test@centos7 ~]$ sudo su -
[sudo] password for test: 
Last login: Wed Jun 26 13:53:29 CST 2019 on tty1
为保证操作的正常进行,将用户切换成超管root
[root@centos7 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xdb9d499a.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-16777215, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-16777215, default 16777215): +2G
Partition 1 of type Linux and of size 2 GiB is set

Command (m for help): p

Disk /dev/sdb: 8589 MB, 8589934592 bytes, 16777216 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
Disk label type: dos
Disk identifier: 0xdb9d499a

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@centos7 ~]# mkswap /dev/sdb1   //这里不要问为什么是sdb1,上面已经显示了
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=e81ca89f-7270-43da-a3be-d879f10685aa   //这里的UUID要记住,后面要在fstab中使用
[root@centos7 ~]# swapon /dev/sdb1
[root@centos7 ~]# free -ht
              total        used        free      shared  buff/cache   available
Mem:           1.8G        126M        1.5G        8.4M        144M        1.5G
Swap:          4.0G          0B        4.0G
Total:         5.8G        126M        5.5G
[root@centos7 ~]# vi /etc/fstab 
UUID=e81ca89f-7270-43da-a3be-d879f10685aa  swap  swap  defaults 0 0  //在文件最后加入,其中UUID也可以使用/dev/sdb1,但为了防止系统硬件读取错误,建议使用UUID

如要去除swap分区
[root@centos7 ~]# swapoff /dev/sdb1
[root@centos7 ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        125M        1.5G        8.4M        145M        1.5G
Swap:          2.0G          0B        2.0G
同时要删除在/etc/fstab文件中添加的swap分区(不要把其他的行删除掉)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值