How do I change swap partition in Linux? If I currently use /dev/hda3 for swap, and I rather would like to use /dev/hda4, which steps should I go through?

  • 1
    it is probably worth to create a logical partition /dev/hda5 in primary partition /dev/hda4 and use it for swap. This way, you don't have to spend the entire /dev/hda4 for swap, and can create other logical partitions in it
    – dmityugov
    Jun 2 '09 at 20:25

Do it as root:

swapoff /dev/hda3
mkswap /dev/hda4
swapon /dev/hda4

and edit swap entry in /etc/fstab

  • i do not seem to find any swap entry in /etc/fstab, also will i need to do the above commands in every linux installation i have which uses same swap?
    – xcodz-dot
    Jan 13 '21 at 5:57

If you have decent amount of RAM and your applications aren't memory-intensive, you might consider using a separate file as a swap instead of the whole partition. That way you can easily select the amount of swap space you use, either by adding more swap files, or resizing existing ones.

Let's say that your swapfile will reside in root directory as /swapfile, and will have size 512 MB. To create it issue commands as root:

$ dd if=/dev/zero of=/swapfile bs=1M count=512
$ mkswap /swapfile
$ swapon /swapfile

To automatically use it after reboot, insert in /etc/fstab:

/swapfile    none    swap    defaults    0 0

Adding more swap files is as simple as creating more files (/swapfile1, /swapfileX), formatting them using mkswap and enabling using swapon. If you want to disable a swapfile, you can use command swapoff /swapfile.

As for the performance between disk and file version, it's not that terribly different. You can even use swapfile as hibernation disk in laptops (although I always use separate partition for that anyway).

  • swapfile is not good idea, because ext4 is super SLOW un SSD for frequent write procedures. This is why by default linux swap has different file system. Correct way would be to resize partition table to make larger swap partition. Jul 1 '19 at 5:16

On the fly:

sudo swapoff /dev/hda3
sudo mkswap /dev/hda4
sudo swapon /dev/hda4

For bootime, after you have run the mkswap, edit the /etc/fstab file and the change the /dev/hda3 line accordingly.


You'll need to format /dev/hda4 as swap, which I think just deletes the file system tables, then just edit /etc/fstab and point swap to /dev/hda4. Then reboot and you should be good. It goes without saying that you'll lose any data on /dev/hda4. You can use gparted as a gui for the formatting.

  • 1
    You don't need to reboot Linux 2.6.x after formatting a partition, not even after changing the partition table of the disk.
    – drybjed
    Jun 2 '09 at 13:15
  • Good point, but I was sticking to the K.I.S.S. philosophy.
    – skitzot33
    Jun 2 '09 at 13:40
  • @skitzot33 Isn't not booting simpler than booting?
    – glglgl
    Sep 26 '16 at 15:05

mateusza's solution is ok, but the state may not be recalled when recovering from hibernation. Some additional operations are needed :

# prepare your partitions
swapoff /dev/sda2
mkswap /dev/sda3
swapon /dev/sda3

# find swap uuid
sudo blkid | grep swap
# OR
sudo echo ls -l /dev/disk/by-uuid | grep sda3 |  cut -d' '  -f8 >> /etc/fstab

# edit swap entry in /etc/fstab
gksu gedit /etc/fstab

# edit the uuid line in /etc/initramfs-tools/conf.d/resume
gksu gedit /etc/initramfs-tools/conf.d/resume

# In terminal, execute this command
sudo update-initramfs -u

you can test your hibernation here, if it doesn't work:

# check your /etc/default/grub file
gksu gedit /etc/default/grub

# Depending on your version : if a line looks like
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=UUID=<old_swap_partition_uuid>"
# then modify it accordingly; otherwise that's all, you're done

# execute in terminal
sudo update-grub

taken from askubuntu


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.