{"id":5538,"date":"2016-01-01T15:32:52","date_gmt":"2016-01-01T07:32:52","guid":{"rendered":"http:\/\/rmohan.com\/?p=5538"},"modified":"2016-01-01T15:32:52","modified_gmt":"2016-01-01T07:32:52","slug":"swap-on-centos-7","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=5538","title":{"rendered":"Swap on CentOS 7"},"content":{"rendered":"<p>we begin, we should take a look at our server&#8217;s storage to see if we already have some swap space available. While we can have multiple swap files or swap partitions, one should generally be enough.<br \/>\nWe can see if the system has any configured swap by using <code>swapon<\/code>, a general-purpose swap utility. With the <code>-s<\/code> flag, <code>swapon<\/code> will display a summary of swap usage and availability on our storage device:<\/p>\n<pre><code lang=\"\">swapon -s\r\n<\/code><\/pre>\n<p>If nothing is returned by the command, then the summary was empty and no swap file exists.<br \/>\nAnother way of checking for swap space is with the <code>free<\/code> utility, which shows us the system&#8217;s overall memory usage. We can see our current memory and swap usage (in megabytes) by typing:<\/p>\n<pre><code lang=\"\">\r\n\r\n[root@clusterserver3 ~]# free\u00a0 -m\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 total\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 used\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 free\u00a0\u00a0\u00a0\u00a0\u00a0 shared\u00a0 buff\/cache\u00a0\u00a0 available\r\nMem:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3704\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 104\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3464\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 8\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 135\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3433\r\nSwap:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3967\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3967\r\n[root@clusterserver3 ~]#\r\n\r\n<\/code><\/pre>\n<h2 id=\"create-a-swap-file\">create a Swap File<\/h2>\n<pre><code lang=\"\">Now that we know our available storage space, we can go about creating a swap file within our filesystem. We will create a file called <code>swapfile<\/code> in our root (<code>\/<\/code>) directory, though you can name the file something else if you prefer. The file must allocate the amount of space that we want for our swap file.\r\n The fastest and easiest way to create a swap file is by using <code>fallocate<\/code>. This command creates a file of a preallocated size instantly. We can create a 4 gigabyte file by typing:\r\n<\/code><\/pre>\n<pre><code lang=\"\"> fallocate -l <span class=\"highlight\">4G<\/span> <span class=\"highlight\">\/swapfile\r\n\r\n\r\n[root@clusterserver3 \/]# ls -ltr \/swapfile\r\n-rw-r--r-- 1 root root 4294967296 Jan\u00a0 2 00:34 \/swapfile\r\n[root@clusterserver3 \/]#\r\n\r\n<\/span><\/code><\/pre>\n<h2 id=\"enable-a-swap-file\">Enable a Swap File<\/h2>\n<pre><code lang=\"\"><span class=\"highlight\">Right now, our file is created, but our system does not know that this is supposed to be used for swap. We need to tell our system to format this file as swap and then enable it.\r\n Before we do that, we should adjust the permissions on our swap file so that it isn't readable by anyone besides the root account. Allowing other users to read or write to this file would be a huge security risk. We can lock down the permissions with <code>chmod<\/code>:\r\n<\/span><\/code><\/pre>\n<pre><code lang=\"\"> chmod 600 <span class=\"highlight\">\/swapfile<\/span>\r\n<\/code><\/pre>\n<pre><code lang=\"\"><span class=\"highlight\">\r\n\r\n[root@clusterserver3 \/]# mkswap \/swapfile\r\nSetting up swapspace version 1, size = 4194300 KiB\r\nno label, UUID=75cdf9e6-f034-4aed-845e-59035534eeaf\r\n[root@clusterserver3 \/]#\r\n\r\n<\/span><\/code><\/pre>\n<h2 id=\"make-the-swap-file-permanent\">Make the Swap File Permanent<\/h2>\n<pre><code lang=\"\"><span class=\"highlight\">Our swap file is enabled at the moment, but when we reboot, the server will not automatically enable the file for use. We can change that by modifying the <code>fstab<\/code> file, which is a table that manages filesystems and partitions.\r\n Edit the file with <code>sudo<\/code> privileges in your text editor:\r\n<\/span><\/code><\/pre>\n<pre><code lang=\"\">vi \/etc\/fstab\r\n<\/code><\/pre>\n<pre><code lang=\"\"><span class=\"highlight\">At the bottom of the file, you need to add a line that will tell the operating system to automatically use the swap file that you created:\r\n<\/span><\/code><\/pre>\n<pre><code lang=\"\"><span class=\"highlight\">\/swapfile<\/span>   swap    swap    sw  0   0\r\n<\/code><\/pre>\n<pre><code lang=\"\"><span class=\"highlight\">\r\n\r\n<\/span><\/code><\/pre>\n<h2 id=\"tweak-your-swap-settings-(optional)\">weak Your Swap Settings (Optional)<\/h2>\n<pre><code lang=\"\"><span class=\"highlight\">There are a few options that you can configure that will have an impact on your system's performance when dealing with swap. These configurations are optional in most cases, and the changes that you make will depend on your application needs and your personal preference.\r\n<\/span><\/code><\/pre>\n<h3 id=\"swappiness\">Swappiness<\/h3>\n<pre><code lang=\"\"><span class=\"highlight\">The <code>swappiness<\/code> parameter determines how often your system swaps data out of memory to the swap space. This is a value between 0 and 100 that represents the percentage of memory usage that will trigger the use of swap.\r\n With values close to zero, the system will not swap data to the drive unless absolutely necessary. Remember, interactions with the swap file are \"expensive\" in that they are a lot slower than interactions with memory, and this difference in read and write speed can cause a significant reduction in an application's performance. Telling the system not to rely on the swap as much will generally make your system faster.\r\n Values that are closer to 100 will try to put more data into swap in an effort to keep more memory free. Depending on your applications' memory profile, or what you are using your server for, this might be the better choice in some cases.\r\n We can see the current swappiness value by reading the <code>swappiness<\/code> configuration file:\r\n<\/span><\/code><\/pre>\n<pre><code lang=\"\">cat \/proc\/sys\/vm\/swappiness\r\n<\/code><\/pre>\n<pre><code lang=\"\">30\r\n<\/code><\/pre>\n<pre><code lang=\"\"><span class=\"highlight\">CentOS 7 defaults to a swappiness setting of 30, which is a fair middle ground for most desktops and local servers. For a VPS system, we'd probably want to move it closer to 0.\r\n We can set the swappiness to a different value by using the <code>sysctl<\/code> command. For instance, to set the swappiness to 10, we could type:\r\n<\/span><\/code><\/pre>\n<pre><code lang=\"\">sudo sysctl vm.swappiness=<span class=\"highlight\">10<\/span>\r\n<\/code><\/pre>\n<pre><code lang=\"\">vm.swappiness = <span class=\"highlight\">10<\/span>\r\n<\/code><\/pre>\n<pre><code lang=\"\"><span class=\"highlight\">This setting will persist until the next reboot. To make the setting persist between reboots, we can add the outputted line to our <code>sysctl<\/code> configuration file:\r\n<\/span><\/code><\/pre>\n<pre><code lang=\"\">sudo nano \/etc\/sysctl.conf\r\n<\/code><\/pre>\n<pre><code lang=\"\"><span class=\"highlight\">Add your swappiness setting to the bottom of the file:\r\n<\/span><\/code><\/pre>\n<pre><code lang=\"\">vm.swappiness = <span class=\"highlight\">10<\/span>\r\n<\/code><\/pre>\n<pre><code lang=\"\"><span class=\"highlight\">When you are finished adding the line, you can save and close the file. The server will now automatically set the swappiness to the value you declared on each bootup.\r\n<\/span><\/code><\/pre>\n<h3 id=\"cache-pressure\">Cache Pressure<\/h3>\n<pre><code lang=\"\"><span class=\"highlight\">Another related value that you might want to modify is the <code>vfs_cache_pressure<\/code>. This setting affects the storage of special filesystem metadata entries. Constantly reading and refreshing this information is generally very costly, so storing it on the cache for longer is excellent for your system's performance.\r\n You can see the current value of this cache pressure by querying the <code>proc<\/code> filesystem again:\r\n<\/span><\/code><\/pre>\n<pre><code lang=\"\">cat \/proc\/sys\/vm\/vfs_cache_pressure\r\n<\/code><\/pre>\n<pre><code lang=\"\">100\r\n<\/code><\/pre>\n<pre><code lang=\"\"><span class=\"highlight\">As it is currently configured, our system removes inode information from the cache far too quickly. We can set this to a more conservative setting, like 50, by using <code>sysctl<\/code>:\r\n<\/span><\/code><\/pre>\n<pre><code lang=\"\">sudo sysctl vm.vfs_cache_pressure=<span class=\"highlight\">50<\/span>\r\n<\/code><\/pre>\n<pre><code lang=\"\">vm.vfs_cache_pressure = <span class=\"highlight\">50<\/span>\r\n<\/code><\/pre>\n<pre><code lang=\"\"><span class=\"highlight\">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:\r\n<\/span><\/code><\/pre>\n<pre><code lang=\"\">sudo nano \/etc\/sysctl.conf\r\n<\/code><\/pre>\n<pre><code lang=\"\"><span class=\"highlight\">At the bottom, add the line that specifies your new value:\r\n<\/span><\/code><\/pre>\n<pre><code lang=\"\">vm.vfs_cache_pressure = <span class=\"highlight\">50<\/span>\r\n<\/code><\/pre>\n<pre><code lang=\"\"><span class=\"highlight\">\r\n\r\n<\/span> <\/code><\/pre>\n<pre><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>we begin, we should take a look at our server&#8217;s storage to see if we already have some swap space available. While we can have multiple swap files or swap partitions, one should generally be enough. We can see if the system has any configured swap by using swapon, a general-purpose swap utility. With the [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5538"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5538"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5538\/revisions"}],"predecessor-version":[{"id":5539,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5538\/revisions\/5539"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}