Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 KB | None | 0 0
  1. Step 1 -- Checking the System for Swap Information
  2. -------------------------------------------------
  3.  
  4. Before we begin, we can check if the system already has some swap space available. It is possible to have multiple swap files or swap partitions, but generally one should be enough.
  5.  
  6. We can see if the system has any configured swap by typing:
  7.  
  8. ```
  9. sudo swapon --show
  10.  
  11. ```
  12.  
  13. If you don't get back any output, this means your system does not have swap space available currently.
  14.  
  15. You can verify that there is no active swap using the `free` utility:
  16.  
  17. ```
  18. free -h
  19.  
  20. ```
  21.  
  22. ```
  23. Output total used free shared buff/cache available
  24. Mem: 985M 84M 222M 680K 678M 721M
  25. Swap: 0B 0B 0B
  26.  
  27. ```
  28.  
  29. As you can see in the Swap row of the output, no swap is active on the system.
  30.  
  31. Step 2 -- Checking Available Space on the Hard Drive Partition
  32. -------------------------------------------------------------
  33.  
  34. Before we create our swap file, we'll check our current disk usage to make sure we have enough space. Do this by entering:
  35.  
  36. ```
  37. df -h
  38.  
  39. ```
  40.  
  41. ```
  42. OutputFilesystem Size Used Avail Use% Mounted on
  43. udev 481M 0 481M 0% /dev
  44. tmpfs 99M 656K 98M 1% /run
  45. /dev/vda1 25G 1.4G 23G 6% /
  46. tmpfs 493M 0 493M 0% /dev/shm
  47. tmpfs 5.0M 0 5.0M 0% /run/lock
  48. tmpfs 493M 0 493M 0% /sys/fs/cgroup
  49. /dev/vda15 105M 3.4M 102M 4% /boot/efi
  50. tmpfs 99M 0 99M 0% /run/user/1000
  51.  
  52. ```
  53.  
  54. The device with `/` in the `Mounted on` column is our disk in this case. We have plenty of space available in this example (only 1.4G used). Your usage will probably be different.
  55.  
  56. Although there are many opinions about the appropriate size of a swap space, it really depends on your personal preferences and your application requirements. Generally, an amount equal to or double the amount of RAM on your system is a good starting point. Another good rule of thumb is that anything over 4G of swap is probably unnecessary if you are just using it as a RAM fallback.
  57.  
  58. Step 3 -- Creating a Swap File
  59. -----------------------------
  60.  
  61. Now that we know our available hard drive space, we can create a swap file on our filesystem. We will allocate a file of the swap size that we want called `swapfile` in our root (/) directory.
  62.  
  63. The best way of creating a swap file is with the `fallocate` program. This command instantly creates a file of the specified size.
  64.  
  65. Since the server in our example has 1G of RAM, we will create a 1G file in this guide. Adjust this to meet the needs of your own server:
  66.  
  67. ```
  68. sudo fallocate -l 1G /swapfile
  69.  
  70. ```
  71.  
  72. We can verify that the correct amount of space was reserved by typing:
  73.  
  74. ```
  75. ls -lh /swapfile
  76.  
  77. ```
  78.  
  79. ```
  80. -rw-r--r-- 1 root root 1.0G Apr 25 11:14 /swapfile
  81.  
  82. ```
  83.  
  84. Our file has been created with the correct amount of space set aside.
  85.  
  86. Step 4 -- Enabling the Swap File
  87. -------------------------------
  88.  
  89. Now that we have a file of the correct size available, we need to actually turn this into swap space.
  90.  
  91. First, we need to lock down the permissions of the file so that only the users with root privileges can read the contents. This prevents normal users from being able to access the file, which would have significant security implications.
  92.  
  93. Make the file only accessible to root by typing:
  94.  
  95. ```
  96. sudo chmod 600 /swapfile
  97.  
  98. ```
  99.  
  100. Verify the permissions change by typing:
  101.  
  102. ```
  103. ls -lh /swapfile
  104.  
  105. ```
  106.  
  107. ```
  108. Output-rw------- 1 root root 1.0G Apr 25 11:14 /swapfile
  109.  
  110. ```
  111.  
  112. As you can see, only the root user has the read and write flags enabled.
  113.  
  114. We can now mark the file as swap space by typing:
  115.  
  116. ```
  117. sudo mkswap /swapfile
  118.  
  119. ```
  120.  
  121. ```
  122. OutputSetting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
  123. no label, UUID=6e965805-2ab9-450f-aed6-577e74089dbf
  124.  
  125. ```
  126.  
  127. After marking the file, we can enable the swap file, allowing our system to start utilizing it:
  128.  
  129. ```
  130. sudo swapon /swapfile
  131.  
  132. ```
  133.  
  134. Verify that the swap is available by typing:
  135.  
  136. ```
  137. sudo swapon --show
  138.  
  139. ```
  140.  
  141. ```
  142. OutputNAME TYPE SIZE USED PRIO
  143. /swapfile file 1024M 0B -2
  144.  
  145. ```
  146.  
  147. We can check the output of the `free` utility again to corroborate our findings:
  148.  
  149. ```
  150. free -h
  151.  
  152. ```
  153.  
  154. ```
  155. Output total used free shared buff/cache available
  156. Mem: 985M 84M 220M 680K 680M 722M
  157. Swap: 1.0G 0B 1.0G
  158.  
  159. ```
  160.  
  161. Our swap has been set up successfully and our operating system will begin to use it as necessary.
  162.  
  163. Step 5 -- Making the Swap File Permanent
  164. ---------------------------------------
  165.  
  166. Our recent changes have enabled the swap file for the current session. However, if we reboot, the server will not retain the swap settings automatically. We can change this by adding the swap file to our `/etc/fstab` file.
  167.  
  168. Back up the `/etc/fstab` file in case anything goes wrong:
  169.  
  170. ```
  171. sudo cp /etc/fstab /etc/fstab.bak
  172.  
  173. ```
  174.  
  175. Add the swap file information to the end of your `/etc/fstab` file by typing:
  176.  
  177. ```
  178. echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
  179.  
  180. ```
  181.  
  182. Next we'll review some settings we can update to tune our swap space.
  183.  
  184. Step 6 -- Tuning your Swap Settings
  185. ----------------------------------
  186.  
  187. There are a few options that you can configure that will have an impact on your system's performance when dealing with swap.
  188.  
  189. ### Adjusting the Swappiness Property
  190.  
  191. The `swappiness` parameter configures how often your system swaps data out of RAM to the swap space. This is a value between 0 and 100 that represents a percentage.
  192.  
  193. With values close to zero, the kernel will not swap data to the disk unless absolutely necessary. Remember, interactions with the swap file are "expensive" in that they take a lot longer than interactions with RAM and they can cause a significant reduction in performance. Telling the system not to rely on the swap much will generally make your system faster.
  194.  
  195. Values that are closer to 100 will try to put more data into swap in an effort to keep more RAM space free. Depending on your applications' memory profile or what you are using your server for, this might be better in some cases.
  196.  
  197. We can see the current swappiness value by typing:
  198.  
  199. ```
  200. cat /proc/sys/vm/swappiness
  201.  
  202. ```
  203.  
  204. ```
  205. Output60
  206.  
  207. ```
  208.  
  209. For a Desktop, a swappiness setting of 60 is not a bad value. For a server, you might want to move it closer to 0.
  210.  
  211. We can set the swappiness to a different value by using the `sysctl` command.
  212.  
  213. For instance, to set the swappiness to 10, we could type:
  214.  
  215. ```
  216. sudo sysctl vm.swappiness=10
  217.  
  218. ```
  219.  
  220. ```
  221. Outputvm.swappiness = 10
  222.  
  223. ```
  224.  
  225. This setting will persist until the next reboot. We can set this value automatically at restart by adding the line to our `/etc/sysctl.conf` file:
  226.  
  227. ```
  228. sudo nano /etc/sysctl.conf
  229.  
  230. ```
  231.  
  232. At the bottom, you can add:
  233.  
  234. /etc/sysctl.conf
  235.  
  236. ```
  237. vm.swappiness=10
  238.  
  239. ```
  240.  
  241. Save and close the file when you are finished.
  242.  
  243. ### Adjusting the Cache Pressure Setting
  244.  
  245. Another related value that you might want to modify is the `vfs_cache_pressure`. This setting configures how much the system will choose to cache *inode* and *dentry* information over other data.
  246.  
  247. Basically, this is access data about the filesystem. This is generally very costly to look up and very frequently requested, so it's an excellent thing for your system to cache. You can see the current value by querying the `proc` filesystem again:
  248.  
  249. ```
  250. cat /proc/sys/vm/vfs_cache_pressure
  251.  
  252. ```
  253.  
  254. ```
  255. Output100
  256.  
  257. ```
  258.  
  259. As it is currently configured, our system removes inode information from the cache too quickly. We can set this to a more conservative setting like 50 by typing:
  260.  
  261. ```
  262. sudo sysctl vm.vfs_cache_pressure=50
  263.  
  264. ```
  265.  
  266. ```
  267. Outputvm.vfs_cache_pressure = 50
  268.  
  269. ```
  270.  
  271. Again, this is only valid for our current session. We can change that by adding it to our configuration file like we did with our swappiness setting:
  272.  
  273. ```
  274. sudo nano /etc/sysctl.conf
  275.  
  276. ```
  277.  
  278. At the bottom, add the line that specifies your new value:
  279.  
  280. /etc/sysctl.conf
  281.  
  282. ```
  283. vm.vfs_cache_pressure=50
  284.  
  285. ```
  286.  
  287. Save and close the file when you are finished.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement