Ubuntu: Make a swap file.

The main question why would you need a swap file when you can opt for a swap partition while installing…? Well people like me who normally screw up their hard disks with multiple partitions and formats normally end up not being able to create any more primary or logical extensions to the existing partitions and hence is unable to make a dedicated swap partition. This, or you forgot to set it up. Having a swap is good even when you are not going to use even half your RAM. You might never need it, but if you need it, this is how you do it.

Any way, we at hirir, have solution!!

Problem: Need swap space but cant (dont want to) make a swap partition.

Solution: Make a swap file.

Step 1: Decide location
Well, decide on a location to fix your swap partition. Ive decided on “/home/swap

Step 2: Become root!
This is becoming a habit now.

Step 3: Type into terminal: fallocate -l 4g /home/swap/4g.swap
This will create a file by the name 4g.swap in /home/swap. The file name is immaterial. Im just trying to indicate that Im making a 4GB swap. -l 4g specifies the length of the file 4g.swap. Suffixes like m g t p e are also possible. ie, mb,gb,tb and so on.

Step 4: Type into terminal: chmod 600 /home/swap/4g.swap
Here we are making it non world readable. Just to make sure no-one reads stuff from it.

Step 5: Type into terminal all over:

mkswap /home/swap/4g.swap
swapon /home/swap/4g.swap

Type into terminal: cat /proc/meminfo and see the swap there and cry out “Yippeee”

Step 6: Make it all permanent
To make it all permanent, we edit /etc/fstab
To do that type into terminal

gksudo gedit /etc/fstab

And add into it, at the end, the following line:

/home/swap/4g.swap none swap sw 0 0

All done!!

Type “free” into terminal and you will see swap space that you’ve just allocated.

Reboot and from then on you may hibernate your system. :) which is why I did all this.


About Manu Mohan

Just another 20yr old...With a screwed up college life and feels hes doesn't fit in..:)

Posted on December 4, 2011, in Linux Adventures, Ubuntu and tagged , , . Bookmark the permalink. 1 Comment.

  1. Do not follow this!!! It does not let you hibernate.. However, you will have your swap up and running..I shall be back on this.

Cracking your skull open while you hit it on the desk? Let me know about it...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: