Files
linux-wiki/content/小白手册/服务器日常/ecs添加swap.md
2021-10-21 15:13:29 +08:00

148 lines
2.5 KiB
Markdown
Executable File
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "阿里云ecs添加swap"
date: 2021-10-18T16:59:28+08:00
draft: false
---
查看当前是没有swap的
```bash
$ free -m
total used free shared buffers cached
Mem: 498 310 187 0 17 170
-/+ buffers/cache: 122 375
Swap: 0 0 0
$ sudo df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/xvda1 ext4 20G 3.7G 16G 20% /
tmpfs tmpfs 250M 0 250M 0% /dev/shm
```
我们将swap挂载到/var下
$ sudo dd if=/dev/zero of=/var/swap bs=1M count=2048
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB) copied, 47.8375 s, 44.9 MB/s
$ sudo mkswap /var/swap
$ sudo chmod 0600 /var/swap
$ sudo swapon /var/swap
挂载完毕后查看内存使用情况。发现swap出现了。
[root@iZ23zvh0w6mZ lnmp1.1-full]# free -m
total used free shared buffers cached
Mem: 498 456 41 0 10 100
-/+ buffers/cache: 346 152
Swap: 2047 0 2047
okswap已经可以使用了接下来我们要将swap开机自动挂载。
编辑/etc/fstab文件
$ sudo vi /etc/fstab
\#
\# /etc/fstab
\# Created by anaconda on Mon Aug 11 16:27:41 2014
\#
\# Accessible filesystems, by reference, are maintained under '/dev/disk'
\# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
\#
UUID=e76a7b8d-20c2-4f94-bdd1-f4054a34c206 / ext4 defaults,barrier=0 1 1
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
none /proc/xen xenfs defaults 0 0
/var/swap swap swap defaults 0 0
因为阿里云默认是不让用户使用swap的貌似现在的系统模版更新了这一行新装系统都没有了如果没有请忽略此步。
编辑/etc/rc.d/rc.local文件将文件中的swapoff行注释或删掉。
$ sudo vi /etc/rc.d/rc.local
\#!/bin/sh
\#
\# This script will be executed *after* all the other init scripts.
\# You can put your own initialization stuff in here if you don't
\# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
ulimit -SHn 65535
\#swapoff -a //加上"#"号注释掉或者直接删除