How to create a swap file in linux using dd

Swap file creation
As root use dd to create a swap file the size of your choosing (M = Megabytes, G = Gigabytes). For example, creating a 512 MB swap file:

# dd if=/dev/zero of=/swapfile bs=1M count=512

Set the right permissions

# chmod 600 /swapfile

After creating the correctly sized file, format it to swap:

# mkswap /swapfile

Activate the swap file:

# swapon /swapfile

Edit fstab to add an entry for the swap file:

# vim /etc/fstab

/swapfile swap swap defaults 0 0

Remove swap file

To remove a swap file, the current swap file must be turned off.

As root:

# swapoff -a

Remove swap file:

# rm -f /swapfile

Finally remove the relevant entry from /etc/fstab.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s