How to Create or Add Swap Partition in Linux


I will explain how to create or Add Swap partition in Linux if it doesn’t exist in this post.

When you check swap partition or file whether it exists or not, if it does not exist then you should create or add.

This is occurred fresh install of Linux and probably it is forgetten.


Check Swap size like following.

[root@MehmetSalih ~]# free -m
             total       used       free     shared    buffers     cached
Mem:         24111       5064      19046       3600         33       4437
-/+ buffers/cache:        593      23517
Swap:            0          0          0
[root@MehmetSalih ~]#


If it is not created like above then we need to create or add it like following steps.


I have used /dev/sdb disk for this.


Format /dev/sdb disk with fdisk.

[root@MehmetSalih ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x59a8f188.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help): n
Command action
e extended
p primary partition (1-4)
Partition number (1-4): 1
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610):
Using default value 2610

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@MehmetSalih ~]#


Create Swap partition like following.

[root@MehmetSalih ~]#
[root@MehmetSalih ~]# mkswap /dev/sdb1
Setting up swapspace version 1, size = 20964788 KiB
no label, UUID=97809171-047f-440d-9e1c-df329313b00a
[root@MehmetSalih ~]#
[root@MehmetSalih ~]#


Add following entry to the /etc/fstab.

/dev/sdb1 swap swap defaults 0 0

[root@MehmetSalih ~]# vi /etc/fstab

Activate swap partition like following steps.

[root@MehmetSalih ~]#
[root@MehmetSalih ~]# swapon -s
Filename Type Size Used Priority
[root@MehmetSalih ~]#
[root@MehmetSalih ~]#
[root@MehmetSalih ~]# swapon -va
swapon on /dev/sdb1
swapon: /dev/sdb1: found swap signature: version 1, page-size 4, same byte order
swapon: /dev/sdb1: pagesize=4096, swapsize=21467947008, devsize=21467948544
[root@MehmetSalih ~]#


When I check swap partition, it is created like following.

[root@MehmetSalih ~]# free -m
             total       used       free     shared    buffers     cached
Mem:         24111       7805      16305       3600         70       5028
-/+ buffers/cache:       2706      21404
Swap:        20473          0      20473
[root@MehmetSalih ~]#


You can list it like following.

[root@MehmetSalih ~]#
[root@MehmetSalih ~]# swapon -s
Filename Type Size Used Priority
/dev/sdb1 partition 20964788 0 -1
[root@MehmetSalih ~]#
[root@MehmetSalih ~]#




Do you want to learn Linux System Administration for Beginners, then read the following articles.

About Mehmet Salih Deveci

I am Founder of SysDBASoft IT and IT Tutorial and Certified Expert about Oracle & SQL Server database, Goldengate, Exadata Machine, Oracle Database Appliance administrator with 10+years experience.I have OCA, OCP, OCE RAC Expert Certificates I have worked 100+ Banking, Insurance, Finance, Telco and etc. clients as a Consultant, Insource or Outsource.I have done 200+ Operations in this clients such as Exadata Installation & PoC & Migration & Upgrade, Oracle & SQL Server Database Upgrade, Oracle RAC Installation, SQL Server AlwaysOn Installation, Database Migration, Disaster Recovery, Backup Restore, Performance Tuning, Periodic Healthchecks.I have done 2000+ Table replication with Goldengate or SQL Server Replication tool for DWH Databases in many clients.If you need Oracle DBA, SQL Server DBA, APPS DBA,  Exadata, Goldengate, EBS Consultancy and Training you can send my email adress [email protected].-                                                                                                                                                                                                                                                 -Oracle DBA, SQL Server DBA, APPS DBA,  Exadata, Goldengate, EBS ve linux Danışmanlık ve Eğitim için  [email protected] a mail atabilirsiniz.

Leave a Reply

Your email address will not be published. Required fields are marked *