EARTH DAY!

“Well! We have just one!

 

Add a Swap file on CentOS 7

Step 1: Open a terminal window

# su –  or # sudo -s

Step 2: Creation of the Storage File for the new swap file

Type the following command to create 512MB swap file (1024 * 512MB = 524288 block size):

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

If you don’t want to have to reboot use the following commands:
(This is useful for debugging automout issue)

524288+0 records in
524288+0 records out
536870912 bytes (537 MB) copied, 3.23347 s, 354 MB/s

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 /swap.
2. of=/swap : Read from /dev/zero write storage file to /swapf.
3. bs=1024 : Read and write 1024 BYTES bytes at a time.
4. Count=524288 : Copy only 523288 BLOCKS input blocks.

Step 3 : Secure the new created swap file

Setup the correct file permission:

# chown root:root /swap
# chmod 0600 /swap

A world-readable swap file is a huge local vulnerability.

Step 4: Set up a Centos 7 swap area

Type the following command to set up a Linux swap area in a file:

# mkswap /swap

Sample outputs:

Setting up swapspace version 1, size = 524284 KiB
no label, UUID=0e5e7c60-bbba-4089-a76c-2bb29c0f0839

Step 5: Enabling the swap file

Finally, activate /swap swap space immediately, enter:

# swapon /swapfile1

Step 6: Update /etc/fstab file

To activate /swap after CentOS 7 system reboot, add entry to /etc/fstab file. Open this file using a text editor such as nano:

# nano /etc/fstab

Copy & Paste the following line:

/swap none swap sw 0 0

Save and close the file.

Verify if your swap file is activated?

# free -m

total       used       free     shared    buffers     cached
Mem:          1876       1798         77          0        119       1440
-/+ buffers/cache:        237       1638
Swap:         4607          0       4607

Display swap usage summary

# swapon -s

Filename				Type		Size	Used	Priority
/dev/sda6                               partition	4194296	0	0
/swap                                   file		524280	0	0

Another option is to view /proc/meminfo file:

# less /proc/meminfo
# grep -i –color swap /proc/meminfo

SwapCached:        30748 kB
SwapTotal:       6291448 kB
SwapFree:        6154008 kB

Optional, test the new swap file:

# swapoff -a
# swapon -a

Enjoy!