# Mounting NFS

Cách kết nối một folder từ máy có nfs server tới máy linux hiện.

**Dữ liệu của máy trên server nfs sẽ được ưu tiên mount vào. Có nghĩa là bạn mount vào folder mà có files thì coi chừng mất sạch mấy files đó.**

#### Cài NFS common cho Client

```bash
sudo apt update

sudo apt install nfs-common
```

```bash
df -h

cd /mnt

sudo mkdir anything

sudo mount 192.168.1.111:/other/2025/things/ /mnt/anything/

sudo chmod 777 /mnt/anything/

sudo nano /etc/fstab
```

Thêm vào

192.168.1.111:/other/2025/things/ /mnt/anything nfs defaults 0 0

```bash
df -h

umount 192.168.1.111:/other/2025/things/
```

**Advanced hơn xíu:** Vì một số lý do nào đó mà nfs client bị disconnect với nfs server, chúng ta có thể chỉnh auto reconnect với nfs client :

```bash
sudo nano /etc/fstab
192.168.1.111:/other/2025/things/ /mnt/anything nfs defaults,_netdev,noresvport,x-systemd.automount,x-systemd.requires=network-online.target,retry=forever,timeo=14,retrans=3 0 0
```

**Các tùy chọn config đã dùng:**

- **`defaults`**: Chọn các tùy chọn mặc định của NFS, gồm `rw,suid,dev,exec,auto,nouser,async`.
- **`_netdev`**: Đảm bảo rằng NFS chỉ được mount sau khi mạng đã sẵn sàng.
- **`noresvport`**: Dùng cổng động thay vì cổng được dành riêng, giúp kết nối ổn định hơn khi NAS khởi động lại.
- **`x-systemd.automount`**: Kích hoạt tự động mount khi có truy cập vào thư mục, giảm thời gian boot.
- **`x-systemd.requires=network-online.target`**: Đảm bảo mạng phải online trước khi mount NFS.
- **`retry=forever`**: Nếu mount thất bại, hệ thống sẽ thử lại liên tục cho đến khi thành công.
- **`timeo=14`**: Thiết lập timeout là 14 \* 0.1 giây = **1.4 giây** trước khi gửi lại yêu cầu.
- **`retrans=3`**: Nếu mất gói tin, hệ thống sẽ thử lại tối đa 3 lần trước khi báo lỗi.
- **`0 0`**: Không cần backup, không chạy `fsck` vì NFS không cần kiểm tra lỗi trên hệ thống file.

#### <span class="hljs-attribute">Cài NFS cho server</span>

```bash
sudo apt update

sudo apt install nfs-kernel-server

sudo chown nobody:nogroup /mnt/my-nfs-folder/

sudo chmod 777 /mnt/my-nfs-folder/

sudo nano /etc/exports

# insert
/mnt/my-nfs-folder/ 192.168.0.0/24(rw,sync,no_subtree_check)



sudo exportfs -a

sudo systemctl restart nfs-kernel-server

```