How to set up swap on Linux cloud server

PHPz
Release: 2023-05-18 23:55:52
forward
1809 people have browsed it

Step one: Confirm the status of swap

Use the following command to check the memory status:

free -m
Copy after login
Copy after login

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
Copy after login

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

swapon -s
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

Activate swap:

mkswap /swapfile
Copy after login

The output results are as follows:

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

Step 4: Enable swap

Execute the following command to enable swap

swapon /swapfile
Copy after login

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
Copy after login

Add the following code at the end:

/swapfile   none    swap    sw    0   0
Copy after login

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
Copy after login
Copy after login

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
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!