# NFS

Setup guide for sharing files over a local network using NFS between Linux and Unix-like systems.

---

## Server

1.  ### Install

    Install the NFS server package to enable file sharing.

    ```sh
    sudo apt update && sudo apt install nfs-kernel-server -y
    ```

2.  ### Add export

    Define the directory to share and specify client access in the NFS exports file.

    ```sh
    echo "{{NFS_SHARE_VAR}} {{CLIENT_IP_VAR}}(rw,sync,no_subtree_check)" | sudo tee -a /etc/exports
    ```

3.  ### Apply export

    Reload the NFS exports to apply the changes.

    ```sh
    sudo exportfs -ra
    ```

4.  ### Allow through firewall (optional)

    Open the NFS port to allow access from the specified client IP.

    ```sh
    sudo ufw allow from {{CLIENT_IP_VAR}} to any port nfs
    ```

## Client

1.  ### Install

    Install the NFS client package to enable mounting remote shares.

    ```sh
    sudo apt update && sudo apt install nfs-common -y
    ```

2.  ### Create mount point

    Make a local directory where the shared files will be mounted.

    ```sh
    mkdir -p {{NFS_MOUNT_VAR}}
    ```

3.  ### Manual mount

    Mount the shared directory manually from the server to the local mount point.

    ```sh
    sudo mount {{SERVER_IP_VAR}}:{{NFS_SHARE_VAR}} {{NFS_MOUNT_VAR}}
    ```

4.  ### Automount

    Add an entry to `/etc/fstab` to mount the share automatically on boot.

    ```sh
    sudo nano /etc/fstab
    ```

    ```sh
    {SERVER_IP_VAR}:{{NFS_SHARE_VAR}}	{{NFS_MOUNT_VAR}}	nfs	noauto,x-systemd.automount,_netdev,nofail,timeo=5,retrans=2	0	0
    ```

5.  ### Unmount

    Unmount the shared directory. The `-l` (lazy) flag defers unmounting until the directory is no longer in use.

    ```sh
    sudo umount -l {{NFS_MOUNT_VAR}}
    ```
