Quick and Dirty Swap File (Linux)

Quick and Dirty Swap File (Linux)

Phill Luby
25th May 2017

Home Insights Quick and Dirty Swap File (Linux)

Need a swap file on a Virtual Machine that didn’t come with one? Don’t care about the size, name or location? This is for you…

#!/bin/bash
fallocate -l 1G /swap
chmod 0600 /swap
mkswap /swap
swapon /swap
echo "/swap none swap sw 0 0" >> /etc/fstab

I use lots of tiny virtual servers for development and testing and on most hosting providers they don’t come with a swap file by default. Sometimes I need a little more memory to get over a hump, such as installing or compiling software, but not for general operation so a bigger machine is overkill. I found lots of step-by-step guides online but I couldn’t find a full cut & paste script very easily online so I’ve posted one here.

Not for use in production, unless the size and location all happen to match your requirements.

Share Article

Insights.

Exciting News! Agile Yorkshire Shortlisted for Leeds Digital Festival Awards
Exciting News! Agile Yorkshire Shortlisted for Leeds Digital Festival Awards

a community we’ve proudly supported for over a decade

Discover More
Ada Lovelace Day: A Celebration of Women in Tech!
Ada Lovelace Day: A Celebration of Women in Tech!

This event will explore and promote strategies for increasing accessibility to technology careers for individuals from disadvantaged communities.

Discover More
An A-Level Student's First Journey into the World of Work: Jai Soni's Inspiring Week at NewRedo
An A-Level Student's First Journey into the World of Work: Jai Soni's Inspiring Week at NewRedo

“This week has been an eye-opening experience. I’ve learned so much from Alan and the team, not just about engineering but also about how our work can make a real difference. It’s been incredibly inspiring,”

Discover More