Ubuntu Linux Create and Add Swap File Tutorial
2015-11-01 18:39
459 查看
from: http://www.cyberciti.biz/faq/ubuntu-linux-create-add-swap-file/
What is a swap file on Ubuntu server or desktop system?
As a sysadmin it is necessary to add more swap space after installation on the server. Swap file allows Ubuntu Linux to use hard disk to increase virtual memory.
Virtual Memory = RAM + Swap space/file
Virtual Memory (1GB) = Actual RAM (512MB) + Swap space/file (512MB)
When the Ubuntu server runs low on memory, it swaps a section of RAM (say an idle program like foo) onto the hard disk (swap space) to free up memory for other programs. Then when you need that program (say foo again), kernel swapped out foo program, it changes
places with another program in RAM.
Procedure to add a swap file on a Ubuntu Linux
Open the Terminal app or use the ssh client to get into the remote server. Login as a root user using sudo command:
sudo -s
Create a swap file command
Type the following command to create a 2GB swap file on Ubuntu:
# dd if=/dev/zero of=/swapfile bs=1G count=2
Sample outputs:
2+0 records in
2+0 records out
2147483648 bytes (2.1 GB) copied, 20.2256 s, 106 MB/s
Verify that file has been created on the server:
# ls -lh /swapfile
Sample outputs:
-rw-r--r-- 1 root root 2.0G Oct 29 14:07 /swapfile
Creating swap space using fallocate command instead of dd command
Instead of the dd command, you can use the the faster fallocate command to create swap file as follows:
# fallocate -l 1G /swapfile-1
# ls -lh /swapfile-1
Sample outputs:
-rw-r--r-- 1 root root 1.0G Oct 29 14:11 /swapfile-1
Secure the swap file
Type the following chmod command and chown command to secure and set correct file permission for security reasons:
# chown root:root /swapfile
# chmod 0600 /swapfile
# ls -lh /swapfile
Sample outputs:
-rw------- 1 root root 2.0G Oct 29 14:07 /swapfile
A world-readable swap file is a huge local vulnerability. The above commands make sure only root user can read and write to the file.
Turn on the swap file
First, use the mkswap command as follows to enable the swap space on Ubuntu:
# mkswap /swapfile
Sample outputs:
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=10231c61-6e55-4dd3-8324-9e2a892e7137
Finally, activate the swap file, enter:
# swapon /swapfile
Verify new swap file and settings on Ubuntu
Type the following command
# swapon -s
Sample outputs:
Filename Type
Size Used
Priority
/dev/sda5 partition
3998716 704
-1
/swapfile file
2097148 0
-2
You can also run the following commands to verify swap file and its usage:
# grep -i --color swap /proc/meminfo
# top
# htop
# atop
How can I disable swapfile on Ubuntu?
You need to use the swapoff command as follows:
# swapoff /swapfile
# swapon -s
Update /etc/fstab file
You need to make sure the swap file enabled when server comes on line after the reboot. Edit /etc/fstab file, enter:
# vi /etc/fstab
Append the following line:
/swapfile none swap sw 0 0
Save and close the file.
Tuning the swap file i.e. tuning virtual memory
You can tune the following two settings:
swappiness
min_free_kbytes
vfs_cache_pressure
How do I set swappiness on a Ubuntu server?
The syntax is:
# sysctl vm.swappiness=VALUE
# sysctl vm.swappiness=20
OR
# echo VALUE > /proc/sys/vm/swappiness
# echo 30 > /proc/sys/vm/swappiness
The value in /proc/sys/vm/swappiness file controls how aggressively the kernel will swap memory pages. Higher values increase agressiveness, lower values descrease aggressiveness. The default value is 60. To make changes permanent add the following line to
/etc/sysctl.conf:
echo 'vm.swappiness=30' >> /etc/sysctl.conf
For database server such as Oracle or MySQL I suggest you set a swappiness value of 10. For more information see the official Linux kernel virtual memory settings page.
What is a swap file on Ubuntu server or desktop system?
As a sysadmin it is necessary to add more swap space after installation on the server. Swap file allows Ubuntu Linux to use hard disk to increase virtual memory.
Virtual Memory = RAM + Swap space/file
Virtual Memory (1GB) = Actual RAM (512MB) + Swap space/file (512MB)
When the Ubuntu server runs low on memory, it swaps a section of RAM (say an idle program like foo) onto the hard disk (swap space) to free up memory for other programs. Then when you need that program (say foo again), kernel swapped out foo program, it changes
places with another program in RAM.
Procedure to add a swap file on a Ubuntu Linux
Open the Terminal app or use the ssh client to get into the remote server. Login as a root user using sudo command:
sudo -s
Create a swap file command
Type the following command to create a 2GB swap file on Ubuntu:
# dd if=/dev/zero of=/swapfile bs=1G count=2
Sample outputs:
2+0 records in
2+0 records out
2147483648 bytes (2.1 GB) copied, 20.2256 s, 106 MB/s
Verify that file has been created on the server:
# ls -lh /swapfile
Sample outputs:
-rw-r--r-- 1 root root 2.0G Oct 29 14:07 /swapfile
Creating swap space using fallocate command instead of dd command
Instead of the dd command, you can use the the faster fallocate command to create swap file as follows:
# fallocate -l 1G /swapfile-1
# ls -lh /swapfile-1
Sample outputs:
-rw-r--r-- 1 root root 1.0G Oct 29 14:11 /swapfile-1
Secure the swap file
Type the following chmod command and chown command to secure and set correct file permission for security reasons:
# chown root:root /swapfile
# chmod 0600 /swapfile
# ls -lh /swapfile
Sample outputs:
-rw------- 1 root root 2.0G Oct 29 14:07 /swapfile
A world-readable swap file is a huge local vulnerability. The above commands make sure only root user can read and write to the file.
Turn on the swap file
First, use the mkswap command as follows to enable the swap space on Ubuntu:
# mkswap /swapfile
Sample outputs:
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=10231c61-6e55-4dd3-8324-9e2a892e7137
Finally, activate the swap file, enter:
# swapon /swapfile
Verify new swap file and settings on Ubuntu
Type the following command
# swapon -s
Sample outputs:
Filename Type
Size Used
Priority
/dev/sda5 partition
3998716 704
-1
/swapfile file
2097148 0
-2
You can also run the following commands to verify swap file and its usage:
# grep -i --color swap /proc/meminfo
# top
# htop
# atop
How can I disable swapfile on Ubuntu?
You need to use the swapoff command as follows:
# swapoff /swapfile
# swapon -s
Update /etc/fstab file
You need to make sure the swap file enabled when server comes on line after the reboot. Edit /etc/fstab file, enter:
# vi /etc/fstab
Append the following line:
/swapfile none swap sw 0 0
Save and close the file.
Tuning the swap file i.e. tuning virtual memory
You can tune the following two settings:
swappiness
min_free_kbytes
vfs_cache_pressure
How do I set swappiness on a Ubuntu server?
The syntax is:
# sysctl vm.swappiness=VALUE
# sysctl vm.swappiness=20
OR
# echo VALUE > /proc/sys/vm/swappiness
# echo 30 > /proc/sys/vm/swappiness
The value in /proc/sys/vm/swappiness file controls how aggressively the kernel will swap memory pages. Higher values increase agressiveness, lower values descrease aggressiveness. The default value is 60. To make changes permanent add the following line to
/etc/sysctl.conf:
echo 'vm.swappiness=30' >> /etc/sysctl.conf
For database server such as Oracle or MySQL I suggest you set a swappiness value of 10. For more information see the official Linux kernel virtual memory settings page.
相关文章推荐
- Linux1:Linux概述
- linux下字符串处理工具一:grep
- [Linux学习笔记] Linux常用命令 - 番外篇(Shell使用技巧)
- linux mysql 操作命令
- How To: set udev rule for setting the disk permission on ASM disks when using multipath on Linux 6.x
- CentOS下MySQL的安装和修改用户密码
- Linux第七次实验笔记
- Linux硬连接和软连接的区别
- 使用Linux(CentOS) 搭建SVN服务器全攻略
- vi编辑器用法总结
- ubuntu 安装后要做的6件事
- Linux下用c语言实现whereis.
- How To: Multipath Linux x86-64 Release 6.4
- snake
- 20151101 zhoumo..orz linux内存性能监控
- Linux文件存储结构,目录项、inode、数据块
- Linux系统用户与用户组介绍
- CentOS设置服务开机启动的两种方法
- CentOS6.3虚拟机配置安装
- Linux常用命令大全