This is a note for myself from Zim-wiki, as I never remember all swap-related commands.
Swap files creation and management in Linux are extremely easy tasks, done very seldom. So easy, that when in need I never remember even the simplest command to deal with it. So here they are, those few of them, hopefully covering all the practical needs.
Before creating or managing something, let's find out how much swap space is already available in the system.
To check the current swap state:
$ sudo swapon -s
Let's create a new swap file now. I will call it justswap
, and place it in the /1st
directory. Swap files can be created anywhere in the filesystem. All we need is to have the read-write access to that location.
Seems like it is possible to have any number of active swap files in the system. I'm not aware of any limit to it.
First, think about how large it should be. say, we want it 1G (1000M) large. create it:
$ sudo dd if=/dev/zero of=/1st/justswap bs=1M count=1000
Second, tell the system we will use it as a swap file:
$ sudo mkswap /1st/justswap
Finally, activate it:
$ sudo swapon /1st/justswap
If we don't need it anymore, we can flush and switch off only one specific swap file at a time:
$ sudo swapoff /1st/justswap
Or, flush and deactivate all swap file(s) and partition(s) at once:
$ sudo swapoff -a
All swap files were flushed and switched off. but the physical files still remain intact. To activate all available swap file(s) and partition(s) again:
$ sudo swapon -a
To avoid manually create the swap file on every boot, we can simply add it to /etc/fstab:
/1st/justswap none swap sw 0 0
That's all about swap files.