Managing Swap Files and Partitions

30 minutes
  • 2 Learning Objectives

About this Hands-on Lab

Managing swap files and partitions may be a necessary system administration task, if our system ever runs low on memory. In this activity, we will be creating a swap partition and a swap file. We will also be looking at activating and deactivating swap space, viewing swap usage, and making swap space available persistently. At the conclusion, we will understand how to work with swap files and partitions when needed to augment system memory.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create a Persistent 1 GB Swap Partition

Using fdisk, create the swap partition:

sudo -i
fdisk /dev/xvdg

Press the following to create the partition:

  • n for new partition
  • Enter to select the default (primary) type
  • Enter for the default first sector
  • +1G for the size of the partition
  • t to change the type
  • L to list the types
  • 82 for Linux Swap
  • w to write changes and quit

Execute mkswap to format the partition:

mkswap /dev/xvdg1

Add an entry to /etc/fstab:

/dev/xvdg1   none     swap    defaults        0 0

Verify swap partition is activated:

swapon -s
swapon -a
swapon -s
Create a Persistent 512 MB Swap File

Use the dd command to create a 512 MB /root/extra.swp file:

dd if=/dev/zero of=/root/extra.swp bs=1M count=512

Format and activate the /root/extra.swap by executing mkswap:

mkswap /root/extra.swp
swapon /root/extra.swp

Set more secure 0600 permissions using the chmod command:

chmod 0600  /root/extra.swp
ls -l /root/extra.swp

Add an entry to /etc/fstab to activate the swap file:

/root/extra.swp none swap defaults 0 0

Activate the swap file entry:

swapoff /root/extra.swp
swapon -a
swapon -s

Additional Resources

The Senior System Administrator has noticed that our applications have been crashing due to Out Of Memory (OOM) errors caused by inadequate available memory. We have been asked to create a 1 GB swap partition and 512 MB swap file that will persist after rebooting to solve this problem.

We should use fdisk to create a 1GB swap type partition, format it with mkswap, add an entry to /etc/fstab, and use swapon to activate it persistently. We were also asked to create a 512MB swap file. Using the dd utility, we will fill the file with zero bytes from the /dev/zero file. This swap file should be called /root/extra.swp. Although the default permissions of 0644 will work on this file, we should change the permission with chmod to 0600, as recommended by the swapon command and for good security practice. Then we will add an entry to /etc/fstab and use the swapon command to activate the swap file persistently.

Logging In

Use the credentials provided on the hands-on lab overview page, and log in as cloud_user. Since we'll need root privileges for everything, let's just execute sudo -i as soon as we get logged in.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?