{"id":857,"date":"2025-05-05T23:03:32","date_gmt":"2025-05-05T15:03:32","guid":{"rendered":"https:\/\/www.livetops.com\/?p=857"},"modified":"2025-10-13T20:32:13","modified_gmt":"2025-10-13T12:32:13","slug":"ubuntu-%e5%bc%80%e5%90%af-swap-%e5%ae%8c%e6%95%b4%e6%b5%81%e7%a8%8b%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/www.livetops.com\/en\/archives\/857","title":{"rendered":"A complete guide to the process of starting Swap on Ubuntu."},"content":{"rendered":"<p class=\"has-text-align-left\">Swap (swap space) is an important mechanism used in Linux systems to extend physical memory, when there is not enough physical memory, the system will swap inactive memory pages into Swap space. The following is the complete procedure for configuring Swap in Ubuntu:<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-text-align-left\">1. Check the current Swap status<\/h2>\n\n\n\n<p class=\"has-text-align-left\">First verify that Swap is enabled on your system:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo swapon --show<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>no output<\/strong>: Indicates that there is currently no active Swap space<\/li>\n\n\n\n<li><strong>exporting<\/strong>: Display information about enabled Swap partitions\/files<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading has-text-align-left\">2. Creating a Swap file<\/h2>\n\n\n\n<h3 class=\"wp-block-heading has-text-align-left\">2.1 Creating a Swap File<\/h3>\n\n\n\n<p class=\"has-text-align-left\">Recommended&nbsp;<code>fallocate<\/code>&nbsp;Quick Create (use if not available)&nbsp;<code>dd<\/code>):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Method 1: Use fallocate (recommended, faster)\nsudo fallocate -l 2G \/swapfile # Creating a 2GB Swap file\n# Method 2: Use dd (better compatibility)\nsudo dd if=\/dev\/zero of=\/swapfile bs=1G count=2 status=progress<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading has-text-align-left\">2.2 Setting file permissions<\/h3>\n\n\n\n<p class=\"has-text-align-left\">Make sure that only the root user has access:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo chmod 600 \/swapfile<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading has-text-align-left\">2.3 Formatting as a Swap Region<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo mkswap \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-left\">Example output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Setting up swapspace version 1, size = 2 GiB (2147479552 bytes) no label, UUID=xxxx-xxxx-xxxx-xxxx-xxxx<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading has-text-align-left\">2.4 Enabling Swap Files<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo swapon \/swapfile<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading has-text-align-left\">3. Verify Swap enabled status<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo swapon --show<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-left\">Example output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>NAME TYPE SIZE USED PRIO\n\/swapfile file 2G 0B -2<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-left\">or using&nbsp;<code>free-h<\/code>&nbsp;View:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>free-h<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-left\">Example output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>total used free shared buff\/cache availableMem: 3.8G 1.2G 100M 200M 2.5G 2.2GSwap: 2.0G 0B 2.0G<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading has-text-align-left\">4. Persistent configurations (power-on auto-mount)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading has-text-align-left\">4.1 Editing an fstab file<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/fstab<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading has-text-align-left\">4.2 Adding Swap Configuration Items<\/h3>\n\n\n\n<p class=\"has-text-align-left\">Add at the end of the document:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/swapfile none swap sw 0 0<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-left\">or configuration with prioritization (optional):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/swapfile none swap sw,pri=10 0 0<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>pri=10<\/code>: Sets the Swap priority (the higher the value the higher the priority)<\/li>\n\n\n\n<li>Save to exit:<code>Ctrl+X<\/code>\u00a0\u2192\u00a0<code>Y<\/code>\u00a0\u2192\u00a0<code>Enter<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading has-text-align-left\">5. Configure Swappiness parameters (optional)<\/h2>\n\n\n\n<p class=\"has-text-align-left\">The Swappiness value controls the system's tendency to use Swap:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>0-100<\/strong>: The smaller the value, the more you tend to use physical memory.<\/li>\n\n\n\n<li><strong>default value<\/strong>:: 60<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading has-text-align-left\">5.1 Viewing Current Values<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>cat \/proc\/sys\/vm\/swappiness<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading has-text-align-left\">5.2 Temporary modifications (reboot disabled)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo sysctl vm.swappiness=10<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading has-text-align-left\">5.3 Permanent modifications<\/h3>\n\n\n\n<p class=\"has-text-align-left\">Edit the configuration file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/sysctl.conf<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-left\">Add at the end of the document:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vm.swappiness=10<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-left\">Apply the configuration after saving the exit:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo sysctl -p<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading has-text-align-left\">6. Cautions<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>File Size Selection<\/strong>::\n<ul class=\"wp-block-list\">\n<li>Physical Memory \u2264 2GB: recommended Swap = 2 \u00d7 Physical Memory<\/li>\n\n\n\n<li>Physical Memory 2-8GB: recommended Swap = Physical Memory<\/li>\n\n\n\n<li>Physical memory \u2265 8GB: recommended Swap = 4-8GB (or as needed)<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Performance considerations<\/strong>::\n<ul class=\"wp-block-list\">\n<li>Swap files underperform Swap partitions<\/li>\n\n\n\n<li>Swap on SSDs is faster than HDDs<\/li>\n\n\n\n<li>Consider using\u00a0<code>zswap<\/code>\u00a0maybe\u00a0<code>zram<\/code>\u00a0Improve performance<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Secure deletion of Swap<\/strong>(if required):<br><code>sudo swapoff \/swapfile <\/code><br><code>sudo rm \/swapfile <\/code><br><code>sudo nano \/etc\/fstab # Delete corresponding line<\/code><\/li>\n\n\n\n<li><strong>Monitoring Swap Usage<\/strong>::<br><code>vmstat 1 5 # Refresh every second, 5 times in total<\/code><br><code>top # Press M to sort by memory<\/code><br><code>htop # More intuitive monitoring tool (installation required)<\/code><\/li>\n<\/ol>\n\n\n\n<p class=\"has-text-align-left\">By following these steps, you can successfully configure and enable Swap space on your Ubuntu system and adjust system parameters as needed for optimal performance.<\/p>","protected":false},"excerpt":{"rendered":"<p>Swap\uff08\u4ea4\u6362\u7a7a\u95f4\uff09\u662f Linux \u7cfb\u7edf\u4e2d\u7528\u4e8e\u6269\u5c55\u7269\u7406\u5185\u5b58\u7684\u91cd\u8981\u673a\u5236\uff0c\u5f53\u7269\u7406\u5185\u5b58\u4e0d\u8db3\u65f6\uff0c\u7cfb\u7edf\u4f1a\u5c06\u4e0d\u6d3b\u8dc3\u7684\u5185\u5b58\u9875 [&hellip;]<\/p>","protected":false},"author":1,"featured_media":1321,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"image","meta":{"footnotes":""},"categories":[275],"tags":[299,300,336,372,391],"class_list":["post-857","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-linux","tag-swap","tag-ubuntu","tag-performance","tag-system","tag-computer","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/www.livetops.com\/en\/wp-json\/wp\/v2\/posts\/857","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.livetops.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.livetops.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.livetops.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.livetops.com\/en\/wp-json\/wp\/v2\/comments?post=857"}],"version-history":[{"count":1,"href":"https:\/\/www.livetops.com\/en\/wp-json\/wp\/v2\/posts\/857\/revisions"}],"predecessor-version":[{"id":1392,"href":"https:\/\/www.livetops.com\/en\/wp-json\/wp\/v2\/posts\/857\/revisions\/1392"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.livetops.com\/en\/wp-json\/wp\/v2\/media\/1321"}],"wp:attachment":[{"href":"https:\/\/www.livetops.com\/en\/wp-json\/wp\/v2\/media?parent=857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.livetops.com\/en\/wp-json\/wp\/v2\/categories?post=857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.livetops.com\/en\/wp-json\/wp\/v2\/tags?post=857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}