Jesin's Blog

Welcome to the Portal of Technology

  • Facebook
  • GitHub
  • Google+
  • RSS
  • Twitter
  • Home
  • Categories
    • Domains
    • Linux
    • Networking
    • PHP
    • Virtualization
    • Web Design
    • Web Servers
    • Windows
  • WordPress Plugins
    • Custom Error Pages
    • HTTP Digest Authentication
    • Mailgun Email Validator
  • Toolbox
    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool
  • About Me
  • Contact Me
  • Sitemap
Home ›
Linux ›
How to create a swap file in Linux

How to create a swap file in Linux

May 5, 2011 Linux Jesin A Leave a Comment

linux category thumbnail

A swap file can be created while installing Linux operating system, if you forgot to do so you can read this tutorial and create one after installation. Even if you need additional swap memory this tutorial will be useful. I’ve only explained the command line way of creating a swap file which most administrators will use, its upto you to explore the GUI technique. For executing the commands in this tutorial you need to have root access or atleast sudo permissions for these commands and permissions to edit the fstab file.

Step 1: Create a dummy file for the required size

If you are using a hard disk partition skip this step. The first step is to create a dummy file of the required file size. The dd command will be used, a dummy file of the specified size will be created. Use the following command

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

The above command will create a 1 GB file /swapfile1. The bs is the number of bytes and count is the number of times to create such bytes so 1M*1024=1GB. You can specify any location for the swap e.g. /root/swpfile, /swaps/swapfile1 Just ensure the directory exists before executing the above command

Step 2: Format it with filesystem swap

The dummy file must be formatted to filesystem type swap, use mkswap to do this execute any one of the following command

mkswap /swapfile1
mkswap -L myswap1 /swapfile1

The second command labels the file with the name myswap1. If you are formatting a device specify the device name

mkswap /dev/sda4
mkswap -L swap1 /dev/sda4

Step 3: Add an entry to the /etc/fstab file

Adding an entry to the /etc/fstab file ensures the file is mounted as swap everytime the system boots. Open /etc/fstab with a text editor and add any one of the following lines

/swapfile1 swap swap defaults 0 0
/dev/sda4 swap swap defaults 0 0

save the file and exit.

Step 4: Check if the configuration is right and mount the swap file.

Use the swapon command to do this

swapon -a

If you see any errors check the /etc/fstab file and correct it. Make sure you reboot the system only AFTER all errors are rectified.

Related posts:

linux category thumbnailMount an ISO file in Linux linux category thumbnailLinux ACL Tutorial change default runlevel in linuxHow to change the default runlevel in Linux linux category thumbnailHow to manage jobs in Linux – fg bg kill Ctrl+Z linux category thumbnailHow to configure NIS server in Linux

Tags: linux commands, linux system files

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get a wealth of information delivered to your inbox. Subscribe and never miss a single article.

  • Tutorials and howtos
  • Code, scripts and commands
  • Online Tools

* No spam, unsubscribe anytime

Hire Me

  • SSL installation and hardening (A+ on Qualys SSL test)
  • Apache & Nginx configuration
  • Email deliverability improvement (10/10 on Mail Tester & MailGenius)
  • WordPress customization, optimization and migration
  • and much more…

    Tools

    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool

    Nav

    • Home
    • About Me
    • Contact Me
    • Privacy Policy
    • Sitemap
    Vultr SSD VPS

    Creative Commons License
    Jesin's Blog by Jesin A is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
    Based on a work at websistent.com.