0% found this document useful (0 votes)
4 views

How To Create a Linux Swap File

This explain how to create swap file in a Linux server

Uploaded by

Sammy Domínguez
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

How To Create a Linux Swap File

This explain how to create swap file in a Linux server

Uploaded by

Sammy Domínguez
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

5/22/24, 3:28 PM How To Create a Linux Swap File

eBook - RHCE Certification Preparation

Menu

How To Create a Linux Swap File

Aaron Kili Last Updated: July 14, 2023 Read Time: 2 mins Linux Commands 6 Comments

In this article, we will explain swap space, and learn how to create swap space using a
swap file in Linux: this is important in case we don’t have a swap partition created on the
hard disk.

Swap space/partition is space on a disk created for use by the operating system when
memory has been fully utilized. It can be used as virtual memory for the system; it can
either be a partition or a file on a disk.

When the kernel runs out of memory, it can move idle/inactive processes into swap
creating room for active processes in the working memory. This is memory management
that involves swapping sections of memory to and from virtual memory.

Suggested Read: 8 Useful Commands to Monitor Swap Space Usage in Linux

With that said, below are the steps we can follow to create a swap space using a file.

How to Create and Enable Swap in Linux

1. In this example, we will create a swap file of size 2GB using the dd command as follows.
Note that bs=1024 means read and write up to 1024 bytes at a time and count = (1024 x
2048)MB size of the file.

# dd if=/dev/zero of=/mnt/swapfile bs=1024 count=2097152

https://www.tecmint.com/create-a-linux-swap-file/ 1/15
5/22/24, 3:28 PM How To Create a Linux Swap File

Alternatively, use the fallocate command as follows.

# fallocate --length 2GiB /mnt/swapfile

And then set the appropriate permissions on the file; make it readable only by root user as
follows.

# chmod 600 /mnt/swapfile

2. Now setup the file for swap space with the mkwap command.

# mkswap /mnt/swapfile

3. Next, enable the swap file and add it to the system as a swap file.

# swapon /mnt/swapfile

https://www.tecmint.com/create-a-linux-swap-file/ 2/15
5/22/24, 3:28 PM How To Create a Linux Swap File

4. Afterwards, enable the swap file to be mounted at boot time. Edit the /etc/fstab file and
add the following line in it.

/mnt/swapfile swap swap defaults 0 0

In the line above, each field means:

/mnt/swapfile – device/file name

swap – defines device mount point

swap – specifies the file-system type

defaults – describes the mount options

0 – specifies the option to be used by the dump program

0 – specifies the fsck command option

6. To set how often the swap file can be used by the kernel, open the /etc/sysctl.conf file
and add the line below.

Note that the default value of how frequent swap space can be used is 60 (maximum value
is 100). The higher the number, the more frequent swap space utilization by the kernel.
When the value is set to 0, the swap file will only be used if the operating system has fully
utilized memory.

vm.swappiness=10

6. Now verify the swap file was created using the swapon command.

# swapon -s
OR
# free
OR
# cat /proc/swaps

https://www.tecmint.com/create-a-linux-swap-file/ 3/15
5/22/24, 3:28 PM How To Create a Linux Swap File

Check Swap Space in Linux

We can optionally reboot the system to effect the above changes using the following
command.

# reboot

Remember to also read through these useful Linux memory management guides:

1. How to Clear RAM Memory Cache, Buffer and Swap Space on Linux
2. 10 ‘free’ Commands to Check Memory Usage in Linux
3. Smem – Reports Memory Consumption Per-Process and Per-User Basis in Linux
4. Find Top Running Processes by Highest Memory and CPU Usage in Linux

That’s It! If you have any issues, use the feedback form below to send us any questions or
important additional ideas to this topic.

commandline tools

PREVIOUS ARTICLE:

https://www.tecmint.com/create-a-linux-swap-file/ 4/15
5/22/24, 3:28 PM How To Create a Linux Swap File

How to Load and Unload Kernel Modules in Linux

NEXT ARTICLE:

How to Determine and Fix Boot Issues in Linux

Photo of
author

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web
developer, and currently a content creator for TecMint who loves working with
computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system


administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts
Have Subscribed)

Was this article helpful? Please add a comment or buy me a coffee to show
your appreciation.

Related Posts

https://www.tecmint.com/create-a-linux-swap-file/ 5/15
5/22/24, 3:28 PM How To Create a Linux Swap File

Mutt – A Command Line Email Client to Send Mails from Terminal

15 Useful ‘dpkg’ Commands for Debian/Ubuntu Users

https://www.tecmint.com/create-a-linux-swap-file/ 6/15
5/22/24, 3:28 PM How To Create a Linux Swap File

Sysadmins & DevOps: 20 Must-Know Advanced Linux Commands

How to Safely Eject a USB Drive in Linux

https://www.tecmint.com/create-a-linux-swap-file/ 7/15
5/22/24, 3:28 PM How To Create a Linux Swap File

How to Make File and Directory Undeletable, Even By Root in Linux

5 Different Types of Shell Commands and Their Usage in Linux

6 Comments Leave a Reply

https://www.tecmint.com/create-a-linux-swap-file/ 8/15
5/22/24, 3:28 PM How To Create a Linux Swap File

rabitu171
January 13, 2022 at 8:00 pm

Really helped me out because I forgot to create a swap partition on my OpenSUSE


install…

Reply

mohamed
February 22, 2020 at 6:25 pm

Thank you for this amazing article, it helped me to create a Swap file in Linux.

Reply

Aaron Kili
February 24, 2020 at 12:46 pm

@mohamed

Great! Many thanks for the feedback.

Reply

https://www.tecmint.com/create-a-linux-swap-file/ 9/15
5/22/24, 3:28 PM How To Create a Linux Swap File

Nityanand Shet
March 28, 2019 at 4:48 pm

mkswap /mnt/swapfile step is missing i guess. I am able to create swap after only
this step.

# swapoff -v /swapfile
# chmod 600 /mnt/swapfile
# swapon /mnt/swapfile
# swapon: /mnt/swapfile: read swap header failed: Invalid argument
# mkswap /mnt/swapfile
# swapon /mnt/swapfile
# free -m

Sample Output

total used free shared buff/cache


available
Mem: 15883 2337 9955 16 3591
13206
Swap: 2047 0 2047

Reply

oldgaro
December 21, 2017 at 3:37 am

Quoted:
https://www.tecmint.com/create-a-linux-swap-file/ 10/15
5/22/24, 3:28 PM How To Create a Linux Swap File

I’d like to add another detail. In this line one may choose to use other units to make
things more simple:

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

You can turn it into:

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

This means that the block size is 1 MB, so count=512 means “I need 512 megs”,
there is no need to do any other calculations.

Reply

Aaron Kili
December 27, 2017 at 1:13 pm

@oldgaro

Thanks for the useful tip, it’s really handy.

Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your
decision to leave a comment and value your contribution to the discussion. It's
important to note that we moderate all comments in accordance with our comment
policy to ensure a respectful and constructive conversation.
https://www.tecmint.com/create-a-linux-swap-file/ 11/15
5/22/24, 3:28 PM How To Create a Linux Swap File

Rest assured that your email address will remain private and will not be published or
shared with anyone. We prioritize the privacy and security of our users.

Name *

Email *

Save my name, email, and website in this browser for the next time I comment.

Post Comment

Search...

Do You Enjoy My Blog?

Support from readers like YOU keeps this blog running. Buying me a cup of coffee is a simple and
affordable way to show your appreciation and help keep the posts coming!

https://www.tecmint.com/create-a-linux-swap-file/ 12/15
5/22/24, 3:28 PM How To Create a Linux Swap File

Buy Me a Coffee

Linux Commands and Tools

Cheat – The Ultimate Linux Commands Cheat Sheet for Beginners and Administrators

How to Use GNU bc (Basic Calculator) in Linux

How to Force User to Change Password at Next Login in Linux

How to Manage /etc with Version Control Using Etckeeper on Linux

How to Optimize and Compress JPEG or PNG Images in Linux Commandline

How to Add or Remove Linux User From Group

https://www.tecmint.com/create-a-linux-swap-file/ 13/15
5/22/24, 3:28 PM How To Create a Linux Swap File

Linux Server Monitoring Tools

Pyinotify – Monitor Filesystem Changes in Real-Time in Linux

How to Install Cacti (Network Monitoring) Tool on RHEL Systems

How to Monitor Website and Application with Uptime Kuma

Monit – A Open Source Tool for Managing and Monitoring Linux System

How to Monitor Linux Server and Process Metrics from Browser

How to Install Zabbix on RHEL 8

Learn Linux Tricks & Tips

2 Ways to Create an ISO from a Bootable USB in Linux

How to List Files Installed From a RPM or DEB Package in Linux

YTP-DL: The Easiest Way to Download Songs from YouTube Videos

4 Ways to Generate a Strong Pre-Shared Key (PSK) in Linux

How to Recover a Deleted File in Linux

Bash-it – Bash Framework to Control Your Scripts and Aliases

Best Linux Tools

25 Outstanding Backup Utilities for Linux Systems in 2024

10 Top Open Source Caching Tools for Linux in 2024

8 Best RDP (Remote Desktop) Clients for Linux in 2024

11 Best Screen Recorders For Linux in 2024

23 Best Open Source Text Editors for Linux in 2024

https://www.tecmint.com/create-a-linux-swap-file/ 14/15
5/22/24, 3:28 PM How To Create a Linux Swap File

10 Tools to Take or Capture Desktop Screenshots in Linux

Tecmint: Linux Howtos, Tutorials & Guides © 2024. All Rights Reserved.
The material in this site cannot be republished either online or offline, without our permission.

Hosting Sponsored by : Linode Cloud Hosting

https://www.tecmint.com/create-a-linux-swap-file/ 15/15

You might also like