4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

EC2 サーバー内に SWAP ファイルを作る (お手軽版)

Last updated at Posted at 2018-05-22

EC2の AMI では SWAP が作られません。
念のために SWAP 領域をつくりたい時のコマンドです

参考
https://centos.bungu-do.jp/archives/65
https://dev.classmethod.jp/cloud/ec2linux-swap-bestpractice/
https://qiita.com/na0AaooQ/items/278a11ed905995bd16af

SWAP 領域を 2GB 分作る

df -h # ディスクの空き容量を確認
sudo swapon -s # Swapfile が無いことを確認
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon -s
sudo sed -i '$ a /swapfile   swap        swap    defaults        0   0' /etc/fstab

さきほど作った SWAP 領域を削除

sudo swapoff /swapfile
sudo vi /etc/fstab
# 下記の行を削除
-------------------------------------
/swapfile   swap        swap    defaults        0   0
-------------------------------------
sudo rm /swapfile

SWAP を作り直す (ファイルを増量する方法)

SWAP ファイルを作り直します。
一旦ディスクの SWAP を削除するので、使用中のメモリの量を事前に確認すること。

sudo swapoff /swapfile
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

既存の SWAP ファイルを使い続けながら移動する方法

既存の SWAP ファイルを使い続けながら、新しい SWAP に移動する方法です。

既存のSWAPを /swapfile1 として、
新しいい SWAP を /swapfile2 とします

一時的に swap ファイルを二重に用意しないといけないので、ディスク容量に注意です。

sudo dd if=/dev/zero of=/swapfile2 bs=1M count=2048
sudo chmod 600 /swapfile2
sudo mkswap /swapfile2
sudo swapon /swapfile2
sudo swapon -s
sudo sed -i '$ a /swapfile2   swap        swap    defaults        0   0' /etc/fstab
sudo vi /etc/fstab
# /swapfile1 の行を削除
sudo swapoff /swapfile1
sudo rm /swapfile1
4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?