:::: MENU ::::

Add a Swap File

  • Feb 12 / 2014
  • 0
Linux

Add a Swap File

When you’re running on a Linux System (or any Unix Operating System), it’s common to use a swap to complete and optimize the memory usage on the computer (especially for older ones). There are different ways to do that, you can:

  • Create a swap on a full partition system (most of Linux distribution will allow you to create it when installing the system or later using fdisk commands to create partition and activate it by following the same steps than below from #2)
  • Create a swap file later on any partition and activate it directly using command-line

I will present here the second case: the creation of a swap file (you will need to get root rights to perform most of the following commands).

  1. Create a file with dd command to create a 1024MB empty file (which will be used by swap).

    We are here writing a file of 1024*1024MB=1048576 blocks size with a Read/Write of 1024 bytes at a time.
  2. Now format the file to swap format
  3. Ensure that the rights access are correct on this file (for security purposes)
  4. Now activate swap on your file
  5. To deactivate swap on your file, juste use the following command

You can now check your swap availability by using the top or free -m command.

Right now, you probably want this swap file to be mounted and activated automatically at the system startup to avoid to perform these steps manually each time ? No problem, juste edit the /etc/fstab file and add this following line:


Your system has now a swap file ready to use !

Leave a comment

You must be logged in to post a comment.

Question ? Contact