Swap partion creation and priority

Posted August 12, 2019 3.6k views
CentOSInitial Server Setup

Hi guys,

I’m looking to create a swap partition on my server. I want it to be at least 3 GB big so that I can actually have more RAM there and server my websites faster or is it not how that works?

Additionally, if possible I would like to have priority of partition and if I see that it works good, I’ll add more.

Can you please let me know how to approach this?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

Hi Remdore,

I see what you are trying to do, however SWAP is not as good as actual RAM. You might ask why? Simply put, virtual memory is a combination of RAM and disk space that running processes can use. Swap space is the portion of virtual memory that is on the hard disk, used when RAM is full. However SWAP is actually HDD/SSD so it’s slower. If the applications on your droplet do actually need to use that much SWAP they’ll be considerable slower. Having said that it’s good to have SWAP enabled

How to add SWAP

First as you mentioned, we’ll add 3GB of SWAP

sudo fallocate -l 1G /swapfile

If faillocate is not installed, you’ll need to do

yum update
yum install faillocate

Now that you have the swapfile, you’ll need to set the right permissions. Please note only the root user should/will have access to the file

sudo chmod 600 /swapfile

Set up a Linux swap area.

sudo mkswap /swapfile

Enable the swap

sudo swapon /swapfile

To make the change permanent open the /etc/fstab file and append the following line:

/swapfile swap swap defaults 0 0

Verify the swap status.

sudo swapon --show

Now your server has SWAP.

As I saw, you want to have different priorities on different SWAP. The default SWAP priority is -1. If you have two swap partitions one with priority 10 and one with priority -1 the swap partion with priority 10 will be used first.

To change the priority of a swap partions, you can

swapon -p 10 /path/to/swap file/partion

That’s it, you now have everything configured as you wished

Kind regards,
Kalin D.

  • Thank you for the fast response Kdimitrov!

    It really makes sense now and I’ll try it as soon as possible.

    Now that we’ve started this discussion, can you let me know why did you make the permissions of the swapfile 600 and not 644 like it’s usually configured?

    • Generally you are correct however in this case we need to use 600. By using the said permissions, we forbid access of other groups to the file.

      Let me try give you some examples of how permissions work

      So the permissions used by a file/directory are read, write, execute. They all have a different value assigned to them read = 4, write = 2 and execute = 1.

      Let’s say you have 755 permissions on a file

      0755 = User:rwx Group:r-x World:r-x

      Or 750

      0750 = User:rwx Group:r-x World:--- (i.e. World: no access)

      Now let’s see what will the 600 permissions of the swapfile represent

      0600 = User:rw Group:--- World: ---

      This would mean the user root will have read and write permissions. That way you ensure the file is used to it’s purpose and nobody else can actually do anything with it, as they don’t actually need to.

      Kind regards,
      Kalin D.