Home >Operation and Maintenance >Linux Operation and Maintenance >How to set up swap on Linux cloud server

How to set up swap on Linux cloud server

PHPz
PHPzforward
2023-05-18 23:55:521975browse

Step one: Confirm the status of swap

Use the following command to check the memory status:

free -m

If the result shows that swap is 0, it means that swap does not exist and you need to create swap.

total              used       free     shared    buffers     cached
Mem:               1840       1614     226       15          36       1340
-/+ buffers/cache:            238      1602
Swap:              0          0        0

Or you can use this command to view it. If no results are output, swap does not exist.

swapon -s

Step 2: Create swap

We use the following command to create a swap file with a size of 2GB.

dd if=/dev/zero of=/swapfile count=2048 bs=1M

The output results are as follows:

2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB) copied, 10.5356 s, 204 MB/s

Step 3: Activate swap

swap will not be automatically recognized by default. We need to set appropriate permissions before it can be used normally. Execute the following command to set the permissions of the swap file to 600, which means it can be read and written by the root user.

chmod 600 /swapfile

Activate swap:

mkswap /swapfile

The output results are as follows:

Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=ff3fc469-9c4b-4913-b653-ec53d6460d0e

Step 4: Enable swap

Execute the following command to enable swap

swapon /swapfile

By default, the swap file will not be automatically started with the system, so it needs to be written to the file system.

vi /etc/fstab

Add the following code at the end:

/swapfile   none    swap    sw    0   0

After saving with the :wq command, swap will be started with the system.

Finally, we can use the free command to confirm whether swap is set successfully.

free -m

The output results are as follows. You can see that swap is no longer 0, indicating that swap has been set successfully.

total       used       free     shared    buffers     cached
Mem:          1840       1754         86         16         23       1519
-/+ buffers/cache:        210       1630
Swap:         2047          0       2047

The above is the detailed content of How to set up swap on Linux cloud server. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete