1. Home
  2. Computing & Technology
  3. Linux
Linux Newbie Administrator FAQ: Accessing Drives

Part 4 of the Linux Newbie Administrator Guide

4.2.13 Swap space
(continued after the ads...)

Related Resources
Linux Newbie Administrator Guide
0. Linux Benefit
1. Before Installation
2. Linux Resources/Help
3. Basic Operations FAQ
4. Newbie Admin FAQ
~ 4.1 Lilo
~ 4.2 Drives
~ 4.3 X-Windows
~ 4.4 Configurations
~ 4.5 Networking
5. Shortcuts / Commands
6. Linux Applications
7. Learn Linux Commands
A. How to Upgrade Kernel?

Swap is an extension of the physical memory of the computer. Most likely, you created a swap partition during the initial RedHat setup. You can verify the amount of swap space available on your system using:
cat /proc/meminfo
The general recommendation is that one should have: at least 4 MB swap space, at least 32 MB total (physical+swap) memory for a system running command-line-only, at least 64 MB of total (physical+swap) memory for a system running X-windows, and swap space at least 1.5 times the amount of the physical memory on the system.
If this is too complicated, you might want to have a swap twice as large as your physical (silicon) memory, but not less than 64 MB.
If you ever need to change your swap, here are some basics.

4.2.13.1 Swap partitions

You can have several swap partitions. [Older Linux kernels limit the size of each swap partition to up to approximately 124 MB, but the linux kernels 2.2.x up do not have this restriction.] Here are the steps to create and enable a swap partition:
- Create the partition of the proper size using fdisk (partition type 82, "Linux swap").
- Format the partition checking for bad blocks, for example:
mkswap -c /dev/hda4
You have to substitute /dev/hda4 with your partition name. Since I did not specify the partition size, it will be automatically detected.
- Enable the swap, for example:
swapon /dev/hda4
To have the swap enabled automatically at bootup, you have to include the appropriate entry into the file /etc/fstab, for example:
/dev/hda4 swap swap defaults 0 0
If you ever need to disable the swap, you can do it with (as root):
swapoff /dev/hda4

4.2.13.2 Swap files

Swapping to files is usually slower than swapping to a raw partition, so this is not the recommended permanent swapping technique. Creating a swap file, however, can be a quick fix if you temporarily need more swap space. You can have up to 8 swap files, each with size of up to 16 MB. Here are the steps for making a swap file:
- Create a file with the size of your swap file:
dd if=/dev/zero of=/swapfile bs=1024 count=8192
This physically creates the swap file /swapfile, the block size is 1024 bytes, the file contains 8192 blocks, the total size is about 8 MB. [The dd command copies files. In the example above, the input file (if) was /dev/zero, the output file (of) was /swapfile . You cannot use the cp (copy) command for creating a swap file because the swap file must be physically continuous on the hard drive.]
- Set up the file with the command:
mkswap /swapfile 8192
- Force writing the buffer cache to disk by issuing the command:
sync
- Enable the swap with the command:
swapon /swapfile
When you are done using the swap file, you can turn it off and remove:
swapoff /swapfile
rm /swapfile
You may also want to see the nice info written by Linus Torvalds himself:
man mkswap

Next > Part 4.3: Working with X-windows

Can't find what you are looking for?
Search the

Stay up-to-date!
Subscribe to the Linux free newsletter.

Explore Linux
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Linux

©2009 About.com, a part of The New York Times Company.

All rights reserved.