
Use fdisk, gdisk, or parted to create a partition for swap. For example, following are the steps to create a swap partition on /dev/sdb using fdisk.
Determine swap space
Prior to adding swap, the free command can be used to view the current swap space. In this example,
~]# free -h
Total Used Free
. . .
Swap: 500MB 200MB 300MB
Create a new partition
Type fdisk /dev/sdb and press enter.
[root@server1 ~]# 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.
Command (m for help):
Type n (new) and press enter.
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p):
Type p (primary partition) press enter.
Select (default p): p
Partition number (2-4, default 2):
When prompted Partition number, press enter to use the default partition number
Partition number (2-4, default 2):
When prompted First sector, press enter to use the default first sector
When prompted Last sector, press enter to use the default last sector
Type w and press enter to write the changes
To set the type
Type fdisk /dev/sdb2 and press enter.
[root@server1 ~]# fdisk /dev/sdb1
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.
Command (m for help):
Type t (type) and press enter.
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes):
Type 82 (swap) and press enter.
Hex code (type L to list all codes): 82
Changed type of partition to 'Swap'
Command (m for help):
Type w and press enter to write the changes.
Command (m for help): w
Create a swap file system
Use the mkswap command to create a swap file system.
~]# mkswap /dev/sdb2
Turn on swap.
~]# swapon /dev/sdb2
Did you find this article helpful?
If so, consider buying me a coffee over at