Adding Swap to Ubuntu


In this post I will be explaining you about adding Swap to Ubuntu manually after installing the Operating System.

If you are installing Ubuntu alone on the system, it won’t ask for adding the swap memory. All the time I am talking about Swap memory right, what is Swap memory actually?

Linux RAM consists of chunks of memory blocks called as Pages. If the pages are filled the system will become slow. To avoid this problem we will be adding Swap Memory. This will take some memory in the hard-disk and acts as an alternate RAM for Ubuntu. Linux swap allows the system to harness more memory that it was physically available.

However, allocating Swap Memory does have disadvantages. Because hard disks have a much slower memory than RAM, virtual private server performance may slow down considerably. Additionally, swap thrashing can begin to take place if the system gets swamped from too many files being swapped in and out.

Check for Swap:

Before we proceed to add a swap file we need to check whether there are any swap files are or not by having a look at the summary of swap usage. To check run the following command in the terminal.

               sudo swapon -s

An empty list will confirm that you have no swap files enabled:

               Filename                     Type                   Size                  Used                Priority

Check the File-system:

Once if we are confirmed that we do not have a swap file enabled, we can check how much space we have on the server with the df command. The swap file will take 512MB, since we are only using up about 8% of the /dev/sda, we can proceed.

               Command: df

               Filesystem       1K-blocks      Used           Available       Use%       Mounted on
               /dev/sda2        20907056       1437188     18421292      8%             /
               udev                  1964644          4                  1964640         1%            /dev
               tmpfs                789380            804              788576           1%            /run
               none                  5120                0                   5120                0%            /run/lock
               none                 1973448          592              1972856         1%            /run/shm

Creating and Enabling the Swap file:

Now it’s time to create a swap file using the dd command.

               sudo dd if=/dev/zero of=/swapfile bs=1024 count=512K

where of=/swapfile designates the file’s name. In this case the name is swapfile.

Subsequently we are going to create the swap file by creating the swap area:

              sudo mkswap /swapfile

Now you will be able to see the manually created swap file in the swap summary.

               sudo swapon -s

The results of the above command will be:

               Setting up swapspace version 1,   size  = 262140 KiB no label, UUID=103c4545-5f-c5-47f3-a8b3-dfbdbd64fd7eb

Finish up by activating the swap file:

               sudo swapon /swapfile

Now, you will be able to view the swap file in the swap summary:

               sudo swapon -s

               Filename                Type        Size            Used     Priority
               /swapfile                 file          262140      0             -1

This file will be there on the virtual private server until the machine reboots. You can ensure the swap is permanent by adding it to the fstab file. To do this run the following command in the terminal:

               sudo nano /etc/fstab

and paste the following lines there

               /swapfile               none         swap         sw       0      0

Finally to prevent the file readable by the other users you need to give the necessary permissions for the swap file:

             sudo chown root:root /swapfile

             sudo chmod 0600 /swapfile

Note: It is not necessary to take the exact values as if it is in the post. Those values are directly pasted from my terminal when I was adding swap to my file system.

That’s it now you have created the swap file.

Advertisements

Leave a Reply

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