moonlightwatch 55f7eb783e
All checks were successful
Build and Upload / Explore-Gitea-Actions (push) Successful in 2m12s
fix: 移除草稿标记
2025-03-26 20:12:00 +08:00

71 lines
1.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Linux 创建交换文件
description: 在Linux系统中创建交换文件并添加为交换分区
date: 2025-03-26
slug: linux-create-swapfile
image: swap_space.png
keywords:
- linux
- swap
categories:
-
tags:
- linux
- operation
---
# 一、创建swap文件
```bash
dd if=/dev/zero of=/swapfile1 bs=1024 count=524288
```
创建 /swapfile1 ,大小为 512M。 计算方式是:`blocksize * count`,即 `1024 * 524288`
创建文件也可以用 `fallocate` `命令,更加直观:
```bash
fallocate -l 512M /swapfile1
```
# 二、限制文件权限
仅给 root 用户读写权限即可
```bash
chown root:root /swapfile1
chmod 0600 /swapfile1
```
# 三、格式化文件
```bash
mkswap /swapfile1
```
# 四、启用swap文件
```bash
swapon /swapfile1
```
开机自动挂载,需要在 `/dev/fstab` 文件增加配置:
```bash
/swapfile1 none swap sw 0 0
```
# 五、解除swap文件
```bash
swapoff /swapfile1
```
如果不再需要swap文件记得移除 `/dev/fstab` 文件里的swap文件配置。