• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

Create new swap file on CentOS 7 / RHEL 7

December 1, 2014 by Sharad Chhetri

In this tutorial we will learn how to create new swap file on CentOS 7 / RHEL 7. Generally on Cloud Servers ( AWS/ DigitalOcean) or VPS, swap space is not created by default. This requirement we can achieve within a few steps.

Prerequisites

Must have free space on mounted disk. You can check by using df -Th command.

Steps to create / add new swap file on Linux

1. Create swapfile-additional file with dd command in / (root). You can select any other partition but it should be mounted (For eg. /opt, /usr ,/NewMountedPartition)

dd if=/dev/zero of=/swapfile-additional bs=1M count=4048

dd = It is a unix command used for convert and copy a file
if = read from FILE instead of stdin
/dev/zero = /dev/zero is a special file in Unix-like operating systems that provides as many null characters (ASCII NUL, 0x00) as are read from it
of = write to FILE instead of stdout
/swapfile-additional = file named swapfile-additional will be created in /
bs = Read and write bytes at a time but if you do not mention MB or GB like only number it will read as bytes. for eg. bs=1024 means 1024 bytes
count = Copy input blocks in our case it is 1024 (1M * 4048 = 4GB)

2. Run mkswap command to make swap area

mkswap /swapfile-additional

3. Change the permission of file swapfile-additional

chmod 600 /swapfile-additional

4. Permanent mounting the swap space by editing the /etc/fstab file .
Use your file editor, I generally use vi editor.

vi /etc/fstab

Paste below given content in /etc/fstab file

/swapfile-additional swap swap    0   0

5. Now mount the swap area, run below given command.

mount -a

6. Enable the swap area

swapon -a

7. Check the number swap space mounted on your system

swapon -s

8. To check how much is swap space available on system.Run below given command

free -m

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. How to find swap partition or file in linux
  2. Add swap file in linux without reboot after OS installation
  3. Linux enable or disable multiple swap space
  4. How to know how many swap space exist in linux system
  5. NOTICE: nagios.cmd file not found. Please specify the location of this file in your /etc/vshell.conf file
  6. How to create iso file from CD/DVD and how to mount .iso file in folder
  7. create a system account below uid 500 on RHEL/CentOS/Scientific Linux
  8. How to convert rpm file into deb file
  9. Convert video file into gif file through command line in linux
  10. How to create a file with cat command

Filed Under: Linux Tagged With: swap

Reader Interactions

Comments

  1. Ton says

    November 19, 2016 at 1:11 pm

    for me last step was > sudo swapon /swapfile-additional

  2. Jim says

    February 17, 2016 at 2:12 am

    “mount -a” isn’t required for enabling swap partitions. “swapon -a” does the trick.

  3. k says

    September 14, 2015 at 8:21 pm

    it was helpful/ thanks a lot

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Linux Command

What is Linux Internal And External Command

Linux Basic Commands With Examples For Every Beginner

tr command to convert lines to space , tab and vertical tab

smbpasswd command not found on CentOS 7 and RHEL 7

Solution : semanage command not found

Unix / Linux : How to print duplicate lines from file

More Posts from this Category

You Might Like These Articles!

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

libfuse

dlopen(): error loading libfuse.so.2 – Got Error On Ubuntu

Failed to open/create the internal network

VirtualBox Error: Failed to open/create the internal network

Always Useful Tips And Tricks

How to change hostname in Ubuntu 12.04 and 12.10 without system restart

Error installing rails

Non interactive ,without typing password do ssh to Server : By sshpass

GNU Screen

How to increase Password Expire date without resetting the password

Virtual Machine inaccessible status on VirtualBox 4.3

Keep logs of user after sudo su – : Secondary Logging

Explore 90+ Article On "Linux Tips And Tricks"

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission.
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy