How to create and add Swap partitions in Ubuntu Linux

Source: Internet
Author: User

How to create and add Swap partitions in Ubuntu Linux

Q: I am a beginner in Ubuntu 14.04 LTS. I need an additional swap file to Improve the Performance of my Ubuntu server. How can I use commands to add a swap partition for my Ubuntu 14.04 LTS through SSH connection.

Except for storing Swap files as a disk to improve the efficiency of pure use in Ubuntu, Swap files are nothing. In this tutorial, you will learn how to create and use Swap files on Ubuntu.

In Ubuntu server or desktop, what is a Swap file?

As a system administrator, it is necessary to add additional swap files after the system is installed. The swap files also allow Ubuntu to use the hard disk to increase the virtual memory.

Virtual Memory = RAM + Swap space/file

Virtual Memory (1 GB) = Actual RAM (512 MB) + Swap space/file (512 MB)

When the Ubuntu server runs out of memory, it exchanges a piece of RAM (such as the foo idle program) to the hard disk (swap space) to release other programs in memory. Then when you need this program (such as foo again), the kernel will switch the Foo program, which will change the location through a program in RAM.

To add a swap partition in Ubuntu

Open a Terminal application or use an SSH client to remotely connect to the server. Use the sudo command to log in as the root user.

# Sudo-s

Create a swap partition command

Run the following command on Ubuntu to create a 2 GB swap partition.

# Dd if =/dev/zero of =/swapfile bs = 1G count = 2

Returned results

Records in

2 + 0 records out

2147483648 bytes (2.1 GB) copied, 20.2256 s, 106 MB/s

Verify the partition created on the server

# Ls-lh/swapfile

Returned results

-Rw-r -- 1 root 2.0G Oct 29/swapfile

Use the fallocate command to create a swap Partition

In addition to the dd command, the fallocate command can also be used to create swap partitions more quickly. As follows:

# Fallocate-l 1G/swapfile-1
# Ls-lh/swapfile-1

Returned results

-Rw-r -- 1 root 1.0G Oct 29/swapfile-1

Protect swap partitions

In terms of security, enter the following chmod and chown commands to protect and set the correct file permissions.

# Chown root: root/swapfile
# Chmod 0600/swapfile
# Ls-lh/swapfile

Setting up swapspace version 1, size = 2097148 KiB

No label, UUID = 10231c61-6e55-4dd3-8324-9e2a892e7133

Returned results

-Rw ------- 1 root 2.0G Oct 29:07/swapfile

A readable swap partition is a major local vulnerability. The preceding command ensures that only the root user has the permission to read and write data.

Start swap Partition

# Mkswap/swapfile
Returned results

Setting up swapspace version 1, size = 2097148 KiB

No label, UUID = 10231c61-6e55-4dd3-8324-9e2a892e7133

The swap partition is activated.
# Swapon/swapfile

 

Verify and set

Enter the following command

# Swapon-s

Returned results

Filename Type Size Used Priority

/Dev/sda5 partition 3998716 704-1

/Swapfile file 2097148 0-2

You can also run the following command to verify the created swap partition and its usage.

How can I disable swap partitions on Ubuntu?

You can use the following command

# Swapoff/swapfile
# Swapon-s

Update the/etc/fstab file

After restart, make sure that the swap partition is enabled after the server is online and edit the/etc/fstab file. Input:

# Vi/etc/fstab

Add a row as follows:

/Swapfile none swap sw 0 0

Save and exit

Adjust the swap partition to adjust the virtual partition.

You can adjust the following two options

  1. Swappiness)
  2. Minimum number of bytes to be released (min_free_kbytes)
  3. Virtual File Cache pressure (vfs_cache_pressure)

 

How to Set swapiness on Ubuntu

Syntax:

# Sysctl vm. swappiness = VALUE
# Sysctl vm. swappiness = 20

Or Surface

# Echo VALUE>/proc/sys/vm/swappiness
# Echo 30>/proc/sys/vm/swappiness

The value of/proc/sys/vm/swapiness controls how the kernel forcibly switches the memory space. A higher value corresponds to an increase in the mandatory intensity, while a lower value corresponds to a decrease in the mandatory intensity. The default value is 60. Add a line of command to the/etc/sysctl. conf file to take effect permanently, as shown below:

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

For databases such as Oracle or MySQL, we recommend that you set the value of swapiness to 10. For more information, visit the official Linux kernel virtual memory

How to Create SWAP partition files in Ubuntu 14.04

How to expand/delete SWAP partitions

How to Create a SWAP partition in an OpenStack Virtual Machine instance

In Linux, how many SWAP spaces should be divided?

Linux SWAP partition creation and memory release

Linux SWAP partition Configuration

Ubuntu 12.04 SWAP settings

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.