Skip to content

How to Move Swapfile to a Different Disk on Ubuntu

Steps
5
Category
Linux
Covers
Disk Management, Swap, Ubuntu
Versions & support
About Ubuntu →
On this page 5 steps

The flexibility and control offered by the Linux operating system, and particularly Ubuntu, make it a favored choice for many users worldwide. One such instance of flexibility is the ability to relocate system resources such as the swapfile. This article provides a detailed guide on moving your swapfile to another disk on Ubuntu.

Before we dive in, let’s quickly recap: a swapfile is a space on the hard disk that is used when the amount of physical RAM memory is full. When a Linux system exhausts its RAM, it transfers inactive pages from the RAM to the swap space. This process ensures the smooth operation of your system, particularly if you’re dealing with resource-intensive tasks.

Here is a step-by-step guide to safely move your swapfile from one disk to another:

Step 1 of 5Disable the Current Swapfile

Before we can move the swapfile, we first need to disable the current one. This can be done by using the `swapoff` command:

sudo swapoff -a 

Step 2 of 5Create a New Swapfile

Next, create a new swapfile on the disk where you want the swapfile moved to. To create a new swapfile, use the `fallocate` command. For instance, if we want to create a swapfile of 2 GB, we would use:

sudo fallocate -l 2G /mnt/mynewdrive/swapfile 

The command above will create a 2 GB file named “swapfile” in the /mnt/mynewdrive directory.

Step 3 of 5Set Up the Swap Area

After creating the file, the next step is to set up this file as swap area. This can be done with the mkswap command:

sudo mkswap /mnt/mynewdrive/swapfile 

Step 4 of 5Make the Swapfile Permanent

To ensure the swapfile is still there even after a reboot, you need to add the swapfile details to the `/etc/fstab` file. First, open up the `/etc/fstab` file with nano (or your chosen text editor):

sudo nano /etc/fstab 

Then, add the following line to the bottom of the file:

Press `Ctrl+X`, then `Y` and Enter to save and close the file.

Step 5 of 5Enable the New Swapfile

Finally, to enable the new swapfile, use the swapon command:

sudo swapon /mnt/mynewdrive/swapfile 

And there you have it! You’ve successfully moved your swapfile to another disk. You can check the status of the swap space using the following command:

sudo swapon --show 

Moving your swapfile can be a practical decision if you’ve added a new drive to your Ubuntu system that is faster or has more space. By following these steps, you can optimize your system to ensure that it’s using resources efficiently. Remember, always make sure to adjust the swapfile size based on your specific system requirements and workloads.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top