0

I'm a newish user to Ubuntu and setting up a server for home use. With previous versions of Ubuntu server I created a swap partition on my first drive. However 20.04 uses a swap.img file on the root partition. I want to move this to my swap partition and did the following:

swapon -s

SHOWS "/swap.img"

sudo nano /etc/fstab

SHOWS "/swap.img none swap sw 0 0"

I already have a SWAP partition that I set up during install so I have a /swap folder in root which is mounted to the correct partition.

I copied swap.img to the swap/ folder then changed the fstab file to read:

/swap/swap.img none swap sw 0 0

I rebooted the system and swapon -s

SHOWS: "/swap/swap.img"

I removed the original swap.img file from root and rebooted Everything seems to work fine but my question is "Is this the correct way to do this?" as I can find no mention anywhere else.

Thanks in advance

wii
  • 1
  • 1
  • You're going at it the wrong way. Yes, since many releases ago Ubuntu uses the swapfile instead of the swap partition. However, users are free to use a swap partition if they want to. So, it's a matter of setting the OS to use a swap partition, certainly NOT "moving the swapfile to a swap partition". – ChanganAuto Apr 21 '22 at 15:55
  • @ChanganAuto Thanks for the reply. So I guess what I should do is: "mkswap /dev/sda5" (sda5 is my /swap partition), then "swapon /dev/sda5" ? Do I need to change anything in the fstab file? It currently reads: "/dev/disk/by-uuid/904b75e0-37d4-4665-a16a-b50b421d2532 /swap ext4 defaults 0 1" Thanks again. P.S. If this works is it OK to delete the swap.img file in root? – wii Apr 21 '22 at 17:08
  • My suggestion in my reply did not work. Could someone explain how to use a swap partition, rather than a swap.img file in server 20.04 please? Thanks again. – wii Apr 21 '22 at 22:18
  • https://askubuntu.com/questions/1154481/how-can-i-use-a-swap-partition-instead-of-a-swap-file – ChanganAuto Apr 21 '22 at 22:21
  • @ChanganAuto Thanks for the link. That works great. However, it also made me realise how DUMB I am. During install I was creating an ext4 partition and mounting it as /swap (WRONG). Instead I should be creating a linux-swap partition, then after booting doing "swapoff /swap.img" and change the details in the fstab file (put a # infront of the line /swap.img ). Very simple when you know how, but I didn't which made me DUMB. I don't know how obvious this is in the official install guide but googling the answer left me with nothing. Thanks again for the help and link. – wii Apr 22 '22 at 11:27
  • You're welcome and don't beat yourself so much. You've all been there in one way or another. Indeed, swap requires a special partition type and it's entirely managed by the system if it ever needs it. – ChanganAuto Apr 22 '22 at 11:43

0 Answers0