Create swap file on a running Linux machine

Solution 1:

Assuming that you want to create 512MB swap file:

# dd if=/dev/zero of=/swapfile bs=1024 count=524288

Setup swap area with:

# mkswap /swapfile

Activate it:

# swapon /swapfile

Add to /etc/fstab for permanent:

echo -e "/swapfile\t\tswap\t\tswap\t\tdefaults\t0 0" >> /etc/fstab

and verify with free -m.