Create And Enable Swap Partition in Linux

October 4, 2013 | By
| Reply More

Swap space in a useful memory that allows your server to continue offer it's services even if the server is completely out of main memory (RAM). Linux OS should have swap space even if it is never going to use that. A minimal amount of swap should be fine.

There are some applications that require certain amount of swap space, make sure to have sufficient space in swap partition for those applications.

For example, Oracle and SAP Netweaver are known examples of applications that make use of swap space. Usually, you create swap partition while installing linux, but if you have not created while installing, you can create it while the system is running. In this example I will show you how to create and enable swap from a partition and from a file.

1. Create and enable swap from partition.

Create a partition from available storage device(disk). For example if you have unpartitioned space left on a device you can use that space for swap. Use fdisk utility to create a partition and from the menu press 'n' to create primary partition and then select default options to create /dev/sdg1 . Below is the output from my test server.

[root@server1 ~]# fdisk /dev/sdg

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)
p
Partition number (1-4): 1
First cylinder (1-224, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-224, default 224):
Using default value 224

Command (m for help): p

Disk /dev/sdg: 235 MB, 235456000 bytes
64 heads, 32 sectors/track, 224 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xe6d95fed

Device Boot Start End Blocks Id System
/dev/sdg1 1 224 229360 83 Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.

Use mkswap command to format the swap device.

[root@server1 ~]# mkswap /dev/sdg1
Setting up swapspace version 1, size = 229356 KiB
no label, UUID=aee57e6f-5962-48f3-8296-7c8e25310686

Use swapon to activate the swap space.

[root@server1 ~]# swapon /dev/sdg1

Verify the swap space has been added.

[root@server1 ~]# free -m
total used free shared buffers cached
Mem: 676 262 414 0 9 155
-/+ buffers/cache: 97 579
Swap: 673 0 673

I had previously around 440 MB of total swap and I've added more 230 MB that is the /dev/sdg1

Add an entry in /etc/fstab so that the swap space is activated automatically the next time you reboot your server.

/dev/sdg1 /swap defaults 0 0

Note:  You can also create a swap partition type which has ID 82 with fdisk utility.

Run frisk /dev/sdg and select n to create a new partition.  When the partition is created select 't' to change partition ID. Enter 82 as partition ID and enter 'w' to save and exit from fdisk utility.  When you create a swap partition using this you can directly  use this swap partition by using swapon command. This way you do not have to format it with mkswap command.

2. Create and Enable swap from file

You can use dd command to create a large file and use that file as a swap area for your operating system. The linux kernel will not distinguish it whether the swap is on a partition or on a file.

Create an empty file of 1GB using dd command.

[root@server1 ~]# dd if=/dev/zero of=/swapfile bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 16.1937 s, 66.3 MB/s

Use mkswap to use this file as swap space.

[root@server1 ~]# mkswap /swapfile
mkswap: /swapfile: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=814fb660-06a3-4020-989d-db91dc0dd642

Type free -m to verify the current amount of swap space on your server.

[root@server1 ~]# free -m
total used free shared buffers cached
Mem: 676 623 53 0 2 513
-/+ buffers/cache: 106 570
Swap: 673 0 673

Activate the swap partition.

[root@server1 ~]# swapon /swapfile

Type free -m again to verify that you have just added 1GB of additional swap space.

[root@server1 ~]# free -m
total used free shared buffers cached
Mem: 676 623 53 0 2 513
-/+ buffers/cache: 107 569
Swap: 1697 0 1697

Add an entry in /etc/fstab so that the swap space is activated automatically the next time you reboot your server.

/swapfile swap defaults 0 0

As you can see the swap space has been increased to 1G that we added from /swapfile.

Filed Under : LINUX HOWTO

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.