14

Are there any conventions on where to put swap files on Linux?

I usually just create them in /var/swapfile, but that seems a bit too high up in the hierarchy.

4 Answers 4

11

AFAIK there is no location defined in the File System Hierarchy Standard.

Because /var is for often-changed files, I would say it is okay to place it here. But remember that if you use a flat-file swap instead of a partition, consider to use a fast file system for /var.

1
7

The conventional way is to have a partition dedicated to swap. If you use a file it's at your own discretion, but Malte's suggestion of the file system being important is a good one.

7
  • +1 for it's own partition. I usually have swap as a partition at the beginning of the drive. /boot then swap is my general approach.
    – egorgry
    Jun 10, 2011 at 18:30
  • 7
    I personally don't really care about the last few percentage points of swap performance enough to take the extra complexity of a separate partition. If my server starts seriously swapping, I have a problem anyway. The swap space is more of a last-resort safety net in case some process starts gobbling up memory.
    – Jo Liss
    Jun 11, 2011 at 14:10
  • That's fine, you're welcome to use a file. I find partitions quite easy to manage but I see your reasoning. You just asked what the convention was and that's pretty much the default scenario.
    – Caleb
    Jun 11, 2011 at 14:18
  • @egorgry - Didn't BSD historically try and always put swap on the 2nd partition to be in the middle?
    – Jé Queue
    Mar 1, 2012 at 17:52
  • I find it quite useful to put swap space on a file in /var, because it makes a little easier to move system to new storage (one thing less to care about). I don't care about performance, since I almost don't use swap any more. Maybe its main use today is for laptop hibernation, and obviously as "safety net".
    – gerlos
    Dec 15, 2014 at 15:47
1

Just to add my 2 cents to Malte's and Caleb's answers, and also to the excelent reference by Paul:

Using swap partitions instead of swapfiles was mentioned often. You have to see that the main issue with swapfiles is not performance (the kernel takes care of that), but instead needing contagious non-sparse disk space. Therefore it is best to create all swapfiles right after the creation of the underlying filesystem. Once the filesystem is in use, with file fragments scattered all around, it will be hard to get hold of some contiguous space. Still, in contrast a partition does not offer anything better: it is guaranteed to be a continuous space, but you also have to create it early on, and it is a major pain to change later.

Also, to address the main question of where to put the swapfile. Just like with swap partitions, the contents of a swapfile is highly confidential in nature, therefore using encryption is recommended. As in case of whole partitions where mostly LVM volumes over LUKS is the practice, swapfiles should be placed on a filesystem which is on top of a LUKS encrypted device. You have to keep in mind, that there are various problems with hibernation on encrypted swap. Also, don't forget access rights. Just like you don't want someone to read or write to your memory, protecting swapfiles is essential. Possibly the best place is inside a directory access-able only by root. (e.g. /var/swap/swapfile01_8G)

As mentioned before creating swapfiles after the system is in use might be a problem. Therefore using this for scaling up after finding out later that more swap is needed could be unfeasible. On the other hand scaling back while having the total swap space made up of multiple swapfiles would be easy. For this reason, it might worth allocating swap space more judiciously, and in multiple files, because it will be easy to scale back if you confirm later that it is significantly oversized. (Buy more disk space for god's sake)

2
  • The swap file needs not to be contiguous. See: unix.stackexchange.com/questions/58265/… May 11, 2023 at 12:58
  • @DavidBalažic Your referenced answer merely says not being aware of any such requirement. Your own claim is stronger, yet the reference to substantiate it is still missing. Not that my answer was better in that regard :P It seems both Arch Wiki and this article only mentions the performance characteristics. May 14, 2023 at 22:36
-2

If you want to mirror the Windows scenario, try /. If you want to share the file in a multi-OS environment, try putting it on a /home partition.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .