M HYPE SPLASH
// news

Error creating Swap File. Ubuntu 18.04

By Sarah Scott

Please be gentle with me, I am a noob and will need simple explanations.

I wanted to increase swap file size, but encountering some weird behavior.

Full story - I had 2G and wanted to allocate more space. First, I followed a suggestion to have double of ram and entered 8G, but then changed my mind and decided to change it to 4G. BUT after following the same procedure as the first time, it stays 8G still.

I followed this guide

This is what I get (edited to show main points):

~$ sudo swapon --show
~$
~$ sudo fallocate -l 4G /swapfile
~$ ls -lh /swapfile
-rw------- 1 root root 8.0G Feb 14 12:14 /swapfile
~$ sudo swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 8G 0B -2

How do I set it to 4G and make sure it's functioning correctly?

3

1 Answer

First, stop using and delete the old swap file:

sudo swapoff /swapfile
sudo rm /swapfile

Secondly, create the new swapfile:

sudo dd if=/dev/zero of=/swapfile bs=1G count=4
sudo chmod 600 /swapfile

Thirdy, configure the file for use as a swapfile:

sudo mkswap /swapfile

Finally, utilize the new 4G swapfile:

sudo swapon /swapfile

Please note that you can but should not use fallocate to create your swapfile. Notes in man swapon indicate that it should work, if the underlying disk structure is XFS, but that problem may occur otherwise.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy