Linux Move swap space from one location to another location

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
Est. reading time 2m

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
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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum