How to Add a Swap File Under Amazon Linux Instance – AWS


Add a Swap File Under Amazon Linux Instance

use the dd command to create swap file. The mkswap command is used to set up a Linux swap area on a device or in a file

 Login to remote Amazon Linux instance using the ssh client. Switch to the root user

Create Storage File-to create 1024MB swap file (1024 * 1024MB = 1048576 block size):

dd if=/dev/zero of=/swapfile1 bs=1024 count=1048576

Cloud Computing – Download Free EBooks and Whitepapers
Java – Download Free EBooks and Whitepapers
Windows – Download Free EBooks and Whitepapers

Where,

  1. if=/dev/zero : Read from /dev/zero file. /dev/zero is a special file in that provides as many null characters to build storage file called /swapfile1.
  2. of=/swapfile1 : Read from /dev/zero write stoage file to /swapfile1.
  3. bs=1024 : Read and write 1024 BYTES bytes at a time.
  4. count=1048576: Copy only 1048576 BLOCKS input blocks.

[root@ip-10-2xx-1xx-1xx ~]# dd if=/dev/zero of=/swapfile1 bs=1024 count=1048576
1048576+0 records in
1048576+0 records out
1073741824 bytes (1.1 GB) copied, 29.9079 s, 35.9 MB/s

————————————————————————————

Set Up a Linux Swap Area

[root@ip-10-212-171-172 ~]# mkswap /swapfile1
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=17a7833e-5ae0-4fce-ae74-ec910bb22238

Setup correct file permission (only root user can read/write to the file)

chown root:root /swapfile1
chmod 0600 /swapfile1

activate /swapfile1 swap space

swapon /swapfile1

To activate /swapfile1 after Linux system reboot, add entry to /etc/fstab file.

vi /etc/fstab

[root@ip-10-212-171-172 ~]# vi /etc/fstab
#
LABEL=/     /           ext4    defaults,noatime  1   1
tmpfs       /dev/shm    tmpfs   defaults        0   0
devpts      /dev/pts    devpts  gid=5,mode=620  0   0
sysfs       /sys        sysfs   defaults        0   0
proc        /proc       proc    defaults        0   0
/swapfile1 swap swap defaults 0 0

Reboot Amazon Linux Instance

Verify Swap is Activated or Not?

[root@ip-10-212-171-172 ~]# free -m
total       used       free     shared    buffers     cached
Mem:           594         95        498          0          6         55
-/+ buffers/cache:         33        560
Swap:         1023          0       1023
[root@ip-10-2xx-1xx-1xx ~]#

 

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