Stop making swap partitions–use swap files instead

jenders1 pts0 comments

Stop making swap partitions · GitHub

/" data-turbo-transient="true" />

Skip to content

-->

Search Gists

Search Gists

Sign in

Sign up

You signed in with another tab or window. Reload to refresh your session.<br>You signed out in another tab or window. Reload to refresh your session.<br>You switched accounts on another tab or window. Reload to refresh your session.

Dismiss alert

{{ message }}

Instantly share code, notes, and snippets.

joshenders/swap.md

Last active<br>June 18, 2026 19:38

Show Gist options

Download ZIP

Star

(0)

You must be signed in to star a gist

Fork

(0)

You must be signed in to fork a gist

Embed

Select an option

Embed<br>Embed this gist in your website.

Share<br>Copy sharable link for this gist.

Clone via HTTPS<br>Clone using the web URL.

No results found

Learn more about clone URLs

Clone this repository at &lt;script src=&quot;https://gist.github.com/joshenders/c4960cec9c63a7b7d68ffa9543356c43.js&quot;&gt;&lt;/script&gt;

" readonly="readonly" data-autoselect="true" data-target="primer-text-field.inputElement " aria-describedby="validation-83514743-44e3-489c-aaf2-f738e57d4976" class="form-control FormControl-monospace FormControl-input FormControl-small rounded-left-0 rounded-right-0 border-right-0" type="text" name="gist-share-url-sized-down" />

Save joshenders/c4960cec9c63a7b7d68ffa9543356c43 to your computer and use it in GitHub Desktop.

Embed

Select an option

Embed<br>Embed this gist in your website.

Share<br>Copy sharable link for this gist.

Clone via HTTPS<br>Clone using the web URL.

No results found

Learn more about clone URLs

Clone this repository at &lt;script src=&quot;https://gist.github.com/joshenders/c4960cec9c63a7b7d68ffa9543356c43.js&quot;&gt;&lt;/script&gt;

" readonly="readonly" data-autoselect="true" data-target="primer-text-field.inputElement " aria-describedby="validation-0439d181-196e-4347-b9c3-1cb6f3c69e70" class="form-control FormControl-monospace FormControl-input FormControl-small rounded-left-0 rounded-right-0 border-right-0" type="text" name="gist-share-url-original" />

Save joshenders/c4960cec9c63a7b7d68ffa9543356c43 to your computer and use it in GitHub Desktop.

Download ZIP

Stop making swap partitions

Raw

swap.md

Stop making swap partitions—use swap files instead!

Swap files have had the same performance characteristics as swap partitions for more than 20 years and yet, linux distributions continue to encourage the use of swap partitions during install.

Swap files are easier to use/add/remove/modify/extend after installation. They're better in every way––use swap files!

Create a swap file.

💡 You can use dd for this too but if your filesystem supports it, fallocate is faster.

fallocate -l 4G /swapfile

Only root should be able to write to the swap file.

chmod 0600 /swapfile

Format the swap file.

mkswap /swapfile

Enable it.

swapon /swapfile

Make it persist across boots.

echo "/swapfile none swap defaults 0 0" >> /etc/fstab

joshenders

commented

Dec 7, 2023

edited

Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link

Copy Markdown

Author

If you're using btrfs:

btrfs filesystem mkswapfile --size 2G swapfile<br>swapon swapfile

https://btrfs.readthedocs.io/en/latest/Swapfile.html

-->

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free<br>to join this conversation on GitHub .<br>Already have an account?<br>Sign in to comment

You can’t perform that action at this time.

swap gist clone swapfile partitions github

Related Articles