[TOC]
# NFS
## What is NFS?
- **Network File System**, it's a kind of **DFS**.
:::info
- **DFS (Distributed File System)**
- A file system with data stored on a server, makes it convenient to share files among users **on a network**. The data is accessed and processed as if it was stored on the local client machine.
- **The servers have full control over the data, and give access control to the clients**.
- The server allows the client users to share files and store data just as if they are storing the information locally.
> See more info on [Techopedia - Distributed File System](https://www.techopedia.com/definition/1825/distributed-file-system-dfs)
:::
> NFS was initially developed by Sun Microsystems.
- Now the newest version is NFSv4.2
## Architecture
- NFS Server should edit **/etc/exports** to config the permission of accessing files.

## Protocols/Port
- NFSv2 and NFSv3 servers have resided on **port 2049** (the default configuration).
- To enhance the possibilities for interoperability, an **NFSv4** implementation MUST support operation over the **TCP** transport protocol.
- On Ubuntu, you can use `rpcinfo -p` to find which ports NFS is using on your node.

## SOP
### NFS on Ubuntu
#### Server
1. Install **`nfs-kernel-server`**
```bash=
sudo apt install nfs-kernel-server
```
2. Creat the export directory
> If you don't want to put all shared files together in a directory, it's okay to skip this step.
```bash=
sudo mkdir -p /mnt/sharefolder
```
> `-p`: to check if there already existed a directory has the same name.
- Share this directory to all users
```bash=
sudo chown nobody:nogroup /mnt/sharefolder
```
> `nobody:nogroup`: means no limit for anyone
3. Assign the permission of access to clients in NFS export file **/etc/exports**
```bash=
sudo vi /etc/exports
```
```
/mnt/sharefolder IP(rw,sync,no_subtree_check)
```
> `IP`: host's IP address
> `rw`: means read/write
4. Make the setting effective
```bash=
sudo exportfs -a
sudo systemctl restart nfs-kernel-server
```
:::warning
- Check if server's firewall deny the access on NFS ports from others
:::
#### Client
1. Install **`nfs-common`**
```bash=
sudo apt-get install nfs-common
```
2. Prepare a directory for mounting the remote files.
ex. `/home/angela/mnt/clientfolder`
3. Mount the remote directory
```bash=
sudo mount server_IP_addr:/mnt/sharefolder /home/angela/mnt/clientfolder
```
> About the permission for non-root user using `mount` command, see [this](https://serverfault.com/questions/825246/mount-an-nfs-share-as-non-root-user-in-cli).
### NFS on FreeBSD
- [FreeBSD doc - NFS](https://docs.freebsd.org/en/books/handbook/network-servers/#network-nfs)
#### Server
1. `/etc/rc.conf`, add:
```
rpcbind_enable="YES"
nfs_server_enable="YES"
mountd_enable="YES"
```
2. `/etc/exports`, add:
```
/path/to/sharefolder [options] shared_host
```
3. Start `nfsd`
```shell=
service nfsd start
service mountd reload
```
#### Client
1. `/etc/rc.conf`, add:
```
nfs_client_enable="YES"
```
2. Start `nfsclient`
```shell=
service nfsclient start
```
3. Mount to where u want
```shell=
mount -t nfs ServerIPorName:/path/to/sharefolder /path/to/whereUwant
```
### NFS on OpenBSD
- [OpenBSD doc - NFS](https://www.openbsdhandbook.com/services/nfs)
#### Server
1. Enable required services
```shell=
rcctl enable portmap mountd nfsd
```
2. `/etc/exports`, add:
```
/path/to/sharefolder [options] shared_host
```
3. Start/reload the services
```shell=
rcctl start portmap mountd nfsd
rcctl reload mountd
```
#### Client
- Just mount!
```shell=
mount -t nfs ServerIPorName:/path/to/sharefolder /path/to/whereUwant
```
### NFS on NetBSD
- [NetBSD doc - how to set up nfs](https://wiki.netbsd.org/tutorials/how_to_set_up_nfs_and_nis/#index6h1)
#### Server
1. `/etc/rc.conf`, add:
```
nfs_server=yes
rpcbind=yes
mountd=${nfs_server}
lockd=${nfs_server}
statd=${nfs_server}
```
2. `/etc/exports`, add:
```
/path/to/sharefolder [options] shared_host
```
3. Start the required services
```shell=
/etc/rc.d/rpcbind start
/etc/rc.d/mountd start
/etc/rc.d/nfsd start
/etc/rc.d/nfslocking start
```
#### Client
- Just mount!
```shell=
mount -t nfs ServerIPorName:/path/to/sharefolder /path/to/whereUwant
```
## Reference
### Official
1. [rfc 7530 - Network File System (NFS) Version 4 Protocol](https://datatracker.ietf.org/doc/html/rfc7530)
2. [Wikipedia - Network File System](https://en.wikipedia.org/wiki/Network_File_System)
### Articles
1. [Install NFS Server and Client on Ubuntu](https://vitux.com/install-nfs-server-and-client-on-ubuntu/)
2. [鳥哥的 Linux 私房菜 - 檔案伺服器之一:NFS 伺服器](http://linux.vbird.org/linux_server/0330nfs.php#What_NFS_0)
3. [Linux Questions - NFS Mount from different subnet Access Denied](https://www.linuxquestions.org/questions/linux-server-73/nfs-mount-from-different-subnet-access-denied-4175488547/)
### Videos
1. [NetApp NFS Tutorial](https://www.youtube.com/watch?v=AtXV2f7zv_E&ab_channel=Flackbox)
2. [How to Share Files Using NFS](https://www.youtube.com/watch?v=c3dL0ULEH-s&ab_channel=soundtraining.net)