Files
nexus/wiki/concepts/永久挂载.md
2026-04-22 08:02:59 +08:00

87 lines
2.5 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: "永久挂载"
tags: [linux, storage, network, ubuntu]
date: 2026-04-26
---
# 永久挂载 (Persistent Mount)
## Definition
永久挂载指在 Linux 系统启动时自动挂载文件系统(如 NFS、SMB、USB 等),通过配置文件实现开机后自动挂载,无需手动执行 mount 命令。
## Core Concept: /etc/fstab
`/etc/fstab`Filesystem Table是 Linux 系统用于定义文件系统挂载配置的核心文件,系统启动时由 `mount -a` 命令读取并自动挂载所有配置项。
## Format
```
<device> <mount_point> <type> <options> <dump> <pass>
```
## Key Parameters
### NFS Permanent Mount Example
```
192.168.3.17:/volume2/backup /mnt/nas_backup nfs defaults,timeo=900,retrans=5,_netdev 0 0
```
| Parameter | Description |
|-----------|-------------|
| `defaults` | 使用默认挂载选项rw, suid, dev, exec, auto, nouser, async |
| `timeo=900` | 超时时间 90 秒(单位 1/10 秒),网络慢时避免频繁失败 |
| `retrans=5` | 超时后重试 5 次 |
| `_netdev` | **关键参数**:告知系统这是网络设备,等网络完全启动后才挂载 |
## Critical: _netdev Parameter
`_netdev` 是 NFS/SMB 等网络文件系统挂载的**必备参数**
- 防止系统启动时因网络未就绪而卡死
- 确保 `Remote File Systems` 服务先启动完成
-`systemctl enable remote-fs.target` 配合使用
## Verification Steps
**⚠️ 永远不要直接重启测试!**
```bash
# 1. 备份原文件
sudo cp /etc/fstab /etc/fstab.bak
# 2. 测试挂载(不重启)
sudo umount /mnt/nas_backup
sudo mount -a
# 3. 验证
df -h | grep nas_backup
```
## Common Issues
### Issue 1: 重启后挂载失效
**Cause**: `nfs-common` 服务启动慢于 `mount -a`
**Solution**:
```bash
sudo systemctl enable remote-fs.target
```
### Issue 2: 挂载点写入本地磁盘
**Cause**: NAS 掉线时挂载失败,但脚本仍写入本地目录
**Solution**: 在备份脚本中添加挂载点检查
```bash
if ! mountpoint -q /mnt/nas_backup; then
echo "错误NAS 未挂载,备份任务取消!" >> /var/log/rsync_backup.log
exit 1
fi
```
## Related Concepts
- [[增量备份]] — 永久挂载为增量备份提供可靠的存储目标
- [[NFS]] — 网络文件系统是永久挂载的典型应用
- [[挂载点检查]] — 备份前的安全检查机制
## Related Entities
- [[rsync]] — 永久挂载的典型使用场景rsync 增量备份的目标存储
## See Also
- [[Cron定时任务]] — 永久挂载后自动执行备份
- [[进程管理]] — 网络断开时的进程控制