Linux move swap space from one location to another location

See all GNU/Linux related FAQ
How do I move swap space file from one location or partition to another location under the Linux server?

Swap partition or file used by most modern operating systems, including Linux operating systems. This space on a disk in which the system can write portions of memory. Usually, this is a dedicated partition, but it may also be a swapfile.
Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Linux terminal
Category System Management
OS compatibility AlmaLinux Alpine Amazon Linux Arch CentOS Debian Fedora Linux Mint Mint openSUSE Pop!_OS RHEL Rocky Slackware Stream SUSE Ubuntu
Est. reading time 1 minutes
Advertisement

Linux Move swap space from one location to another location

There is no command or an easy method exists that allows you to move swap space from one location to another. You can turn off the current swap space with the swapoff command in the original location. For instance:
# swapoff /dev/device

Examples

I am going move /my-swap-file to /foo-swap-space location as follows:
# swapoff /my-swap-file
Verify it with the following command:
# swapon -s
Next create a new swap space with the mkswap command. In this example I am creating a new swap file called /foo-swap-space with 4GiB size:
# dd if=/dev/zero of=/foo-swap-space count=4 bs=1GiB
Set up permission:
$ sudo chmod -v 0600 /foo-swap-space
$ sudo chown -v root:root /foo-swap-space
$ ls -l /foo-swap-space

Run the following mkswap command to set up Linux swap area using a file:
# mkswap /foo-swap-space
Activating a swap space by typing the swapon command:
# swapon /foo-swap-space
Edit and update the /etc/fstab file to /foo-swap-space location. For more info see: How to create a Linux swap file.

Conclusion

You learned how to turn off the existing swap space and move it to the new location under Linux.

This entry is 6 of 7 in the Linux and UNIX Swap File Management Tutorial series. Keep reading the rest of the series:
  1. Linux Add a Swap File
  2. FreeBSD Add a Swap File
  3. Ubuntu Create and add a swap file
  4. Check Swap Usage Size and Utilization in Linux
  5. Linux Find Out What Process Are Using Swap Space
  6. Move swap space from one location to another location
  7. Solaris add a new swap file for database

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

0 comments… add one

Leave a Reply

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

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.