Adding more Swap Space to your Linux Machine


Points: 1 point, 8879 views Comments: 9 Comments User: Param

Certain software's require minimum swap space for installation. I ran into the same problem when installing oracle on my Fedora machine. Does adding more swap space mean you have to install again from scratch? Luckily, no. In this article I will show you how to add some extra space to your existing tux. You could also reshuffle and resize your partitions and make increase swap space but that is a pretty severe change to the existing machine.

Step 1: Learn to use Swap commands

Swapon and SwapOff are two command provided with Linux to enable/disable devices and files for paging and swapping.

Lets first check how much swap space is there on my machine. At the CLI, we type "swapon -s" which displays swap usage summary by device.

The sizes mentioned in above screenshot are in Kb, and my system currently has only 490MB out of which 72MB has been already used. That's the reason why oracle rpm threw errors during installation.

Step 2: Available Disk Space

Before we go ahead and create swap file, lets see how much disk space we have. At the CLI, we would now run the command "df -h". `df` command reports file system disk space usage on a Linux system. We also included the option of "-h" which print sizes in human readable format (e.g., 1K 234M 2G).

Under the "Available" column I have approximately 22Gigs of free space on root partition, that’s where my new swap file will be.

Step 3: Create Swap File using DD command

In order to create an additional swap file, I am going to use the dd (data dump) command.

In the above dd command we used :

  • if=/dev/zero ; points to /dev/zero file which contains only zero's and will write those to the output file. In contrast to /dev/null, which only acts as a sink for data, /dev/zero also acts as a source. All reads on /dev/zero return as many NULL’s as characters requested.
  • of=/extraswap ; points to /extraswap file which we will use as swap
  • count=2048 ; is the file-size for /extraswap file which gets created.
  • bs=1M ; determines how many bytes should be read and written at a time

You can replace 2048 with the number of megabytes you want in your additional swap file.

Step 4: Setup Linux swap area using MKSWAP command

Once, extraswap file is generated; we will have to prepare it for use as a swap partition by using mkswap command. mkswap sets up a Linux swap area on a device or in a file. The device argument provided to mkswap is generally a disk partition (something like /dev/hda2 or /dev/sdb3) but it can also be a file (like in our case).

Step 5: SwapOn the new /extraswap file

To activate my new swap file, I run the command "swapon /extraswap" on the CLI. After that you can run "swapon -s" and view the addition made.

You can also use "free -m" command to verify addition of your new swap space.

Step 6: Make swap upgrade permanent through fstab file

To make these changes permanent and load the new /extraswap everytime system reboots, we will have to modify the fstab file in /etc/ folder.

Open /etc/fstab in your VI and add another line to include information regarding new swap file.

Done! Now you can reboot, do whatever and your system will have 2048Mb of more swap space (and hopefully my oracle installation wont be a PITA now).


How 2 get more swap

Great work, used it on a Fedora Core 4 easily ! Everything I needed to get things done.

Continue with your great solutions !

swap

my system was built without swap been added to the built.
can the above command be used to add swap?

Should work fine

Try out the above steps on your system, it should work fine.

installing 10g your entry was immense help

I also got stuck with too small a partition. Thanks to you I fixed in a snap.

Excellent Demonstration

Well done mate, demonstration like yours are well spot on to the need and save plenty of time browsing around the net or resources.. keep up the good work.

cheers,
Zahid

Perfect Solution

Excellant. Worked like a charm for my oracle install. Thanks a bunch.

oh please don't take screen

oh please don't take screen shots, but place ascii text in your solutions, so they one can copy/paste

Worked like a charm

I used your instructions and it worked in my Centos 5xx Server

PLz tell me !!!

OK it works perfectly. please tell me if I need to delete this swap file then what i have to do. can i directly delete this file from disk or i will do some changes in configuration?

Post new comment

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h1> <quote> <img>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.