Master the Linux ‘mkswap’ Command: A Comprehensive Guide

Peter Hou
2 min readMay 23, 2023

--

This article provides a detailed guide to the Linux 'mkswap' command. It covers the history of the command, when and why to use it, how to use it, and some common use cases and advanced techniques. The guide also provides a list of supported parameters and examples of how to use them.

Instructions

This article provides a comprehensive introduction to the Linux 'mkswap' command. It covers the command's history, when and why to use it, how to use it, the most common use cases, and some advanced techniques.

History

The 'mkswap' command is part of the Linux operating system's suite of tools for managing and maintaining the system. It was designed to set up a Linux swap area on a device or in a file.

When and why to use it

The 'mkswap' command is used when you want to create or set up a new swap space on your system. Swap space is a portion of a hard disk drive that is used for virtual memory. Swap space can help your system handle more processes, and it can be especially useful if your system often runs out of physical memory.

How to use it

The 'mkswap' command is fairly straightforward to use. The basic syntax is 'mkswap DEVICE', where DEVICE is the name of the device or file where you want to create the swap area.

$ sudo mkswap /dev/sdx

The commonly used parameters

  • -c This parameter instructs mkswap to check the device for bad blocks.
$ sudo mkswap -c /dev/sdx
  • -p This parameter allows you to specify a custom page size.
$ sudo mkswap -p 4096 /dev/sdx

Other supported parameters

  • -L label : Specify a label for the device
  • -v1 : Use the old-style (Linux 2.2) swap layout
  • -U uuid : Specify the UUID of the device

Most common use cases

One of the most common use cases for 'mkswap' is when you are setting up a new Linux installation and need to create a swap area on your hard disk.

$ sudo mkswap /dev/sdx

The tricky skills

One lesser-known usage of 'mkswap' is that you can actually create a swap file instead of a swap partition. This can be done by using the 'dd' command to create a blank file, and then running 'mkswap' on that file.

$ dd if=/dev/zero of=/swapfile bs=1M count=1024
$ sudo mkswap /swapfile

What needs to be noted

It's important to note that you should be very careful when using 'mkswap', as you can easily overwrite important data if you're not careful. Also, you need to have root privileges to use 'mkswap' command.

Conclusion

The 'mkswap' command is a powerful tool for managing swap space on your Linux system. While it's fairly straightforward to use, it also has some advanced features that can be very useful for more complex setups.

--

--

Peter Hou
Peter Hou

Written by Peter Hou

I am a Senior Software Engineer and tech lead in a top tech company.

No responses yet