Linux Add a Swap File

Source: Internet
Author: User

http://www.cyberciti.biz/faq/linux-add-a-swap-file-howto/

Procedure to Add a Swap File under Linux

You need the command to dd create swap file. The mkswap command is used to set up a Linux swap area on a device or in a file.

Step #1: Login as the Root User

Open a terminal window (select Applications > Accessories > Terminal) or Login to remote server using the SSH client . Switch to the root user by typing su - (or sudo -s ) and entering the root password, when prompted:
$ su -
OR
$ sudo -s

Step #2: Create Storage File

Type the following command to create 512MB swap file (1024x768 * 512MB = 524288 block size):
# dd if=/dev/zero of=/swapfile1 bs=1024 count=524288
Sample outputs:

524288+0 Records in524288+0 records out536870912 bytes (537 MB) copied, 3.23347 s, 166 MB/s

Where,

    1. if=/dev/zero: Read From/dev/zero file. /dev/zero is a special file in this provides as many null characters to build storage file Called/swapfile1.
    2. of=/swapfile1: Read From/dev/zero Write storage file to/swapfile1.
    3. bs=1024: Read and write 1024x768 BYTES BYTES at a time.
    4. count=524288: Copy only 523288 BLOCKS input BLOCKS.
Step #3: Set up a Linux Swap area

Type the following command to set up a Linux swap area in a file:
# mkswap /swapfile1
Sample outputs:

Setting up Swapspace version 1, size = 524284 kibno label, uuid=e1fe2038-6554-4d1a-a108-037acd4e02e2

Setup correct file permission for security reasons, enter:
# chown root:root /swapfile1
# chmod 0600 /swapfile1

A world-readable swap file is a huge local vulnerability. The above command make sure only root user can read/write to the file. Finally, Activate/swapfile1 swap space immediately, enter:
# swapon /swapfile1

Update/etc/fstab file

To Activate/swapfile1 after Linux system reboot, add entry To/etc/fstab file. Open this file using a text editor such as VI:
# vi /etc/fstab
Append the following line:
/swapfile1 swap swap defaults 0 0
Save and close the file. Next time Linux comes up after reboot, it enables the new swap file for you automatically.

How does I Verify Swap is Activated or not?

Simply Use the command:
$ free -m

                    Total used       free     shared    buffers     cachedmem:          1876       1798          0        119       1440-/+ Buffers/cache:        237       1638Swap:         4607          0       4607
How can I display swap usage summary on Linux?

Type the following Swapon command:
# swapon -s
Sample outputs:

Filenametypesizeusedpriority/dev/sda6                               partition419429600/swapfile1                              file5242800-1

Another option is to View/proc/meminfo file:
$ less /proc/meminfo
$ grep -i --color swap /proc/meminfo

Sample outputs:

swapcached:        30748 kbswaptotal:       6291448 kbswapfree:        6154008 KB

You can also use top command, atop command, and/or htop command to display information about swap usage:
# top
# atop
# htop

Sample outputs from a database server running on a CentOS Linux server:

1 [| | 3.9%] tasks:171, 106 thr;                        1 running 2 [0.0%] Load average:0.06 0.12 0.09 3 [| |                          2.0%] uptime:22 days, 07:07:28 4 [0%] 5 [0%] 6 [ 0%] 7 [0%] 8 [0%]mem[| | | | | | | | | | | | |  2112/11909MB] swp[| 134/6143MB]PID USER PRI NI VIRT RES SHR s CPU% mem% time+ Command 8523 mysql 0 4545M 414M 4816 s 0.0 3.5      0:00.00/USR/LIBEXEC/MYSQ 8524 MySQL 0 4545M 414M 4816 S 0.0 3.5 0:02.74/usr/libexec/mysq 8525 MySQL 0 4545M 414M 4816 s 0.0 3.5 0:16.17/usr/libexec/mysq 8526 mysql 0 4545M 414M 4816 s 0.0 3.5 0:0   6.33/USR/LIBEXEC/MYSQ 8528 MySQL 0 4545M 414M 4816 S 0.0 3.5 4:02.14/usr/libexec/mysq 8529 MySQL 20  0 4545M 414M 4816 s 0.0 3.5 5:22.00/usr/libexec/mysq 8530 MySQL 0 4545M 414M 4816 s 0.0 3.5 0:04.63 /USR/LIBEXEC/MYSQ 8531 MySQL 0 4545M 414M 4816 S 0.0 3.5 0:50.95/usr/libexec/mysq 8532 mysql 20 0 4545M 414M 4816 s 0.0 3.5 0:08.10/usr/libexec/mysq 9359 mysql 0 4545M 414M 4816 s 0.0 3.5 22:53.28/us R/libexec/mysq
How can I disable devices and files for paging and swapping on Linux?

Need to use the Swapoff command:
# swapoff /swapfile1
# swapon -s

How does I set swappiness on a Linux server?

The syntax is:
# sysctl vm.swappiness=VALUE
# sysctl vm.swappiness=20

OR
# echo VALUE > /proc/sys/vm/swappiness
# echo 30 > /proc/sys/vm/swappiness

The value in /proc/sys/vm/swappiness file controls how aggressively the kernel would swap memory pages. Higher values increase agressiveness, lower values descrease aggressiveness. The default value is 60. To make changes permanent add the following line to /etc/sysctl.conf :

 ' vm.swappiness=30 ' >>/etc/sysctl.conf

Linux Add a Swap File

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.