Score:1

Extend ubuntu swap without reboot and risk

th flag

I manage an Ubuntu server part-time in a scientific research laboratory, which has 512G memory and 8G swap space. Recently, we occasionally encounter low-frequency memory shortage, so I plan to expand the swap space. Currently, this server cannot accept any shutdown or accident. I find that they all recommend restarting the device after configuring the swap,

eg: Increase swap in 20.04

and I'm not sure if I can avoid this

My plan is:

First, find current swap file

$ sudo swapon --show
NAME      TYPE SIZE USED PRIO
/swap.img file   8G 7.3G   -2

swap off and remove

sudo swapoff /swap.img 
sudo rm /swap.img

create new swap file

sudo dd if=/dev/zero of=/swap.img bs=1M count=92160
sudo chmod 600 /swap.img

create swap

sudo mkswap /swap.img

enable swap

sudo swapon /swap.img
# not reboot

Is it ok and safe to do?

Marco avatar
br flag
No need to remove "old" swapfile, create an **additional** one (not overwriting!). Linux can work with multiple swapfiles. https://askubuntu.com/questions/563430/how-to-create-a-swapfile
zhang avatar
th flag
Hi, thanks for your help, sorry for more question, I am not sure is it necessary to add the new swapfiles to `/etc/fastb` for if I just add not instead the old swap, or the `swapon` will do it automatic
Marco avatar
br flag
If you want to have the config over a reboot, you need to add it in `/etc/fstab`.
pl flag
(however, yes, you can create a second swap file (I do this all the time on servers) and just enable it for that session, without modifying `/etc/fstab`) (to complete the answer)
waltinator avatar
it flag
Creating a swap file with `dd` will create a discontinuous file, which won't work for swap. Read `man mkswap swapon fstab`.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.