# Mount a Network File System Volume on Debian-based Systems

## Overview

This tutorial will guide you through mounting a Network File System (NFS) volume on a Linux-based system.

Although the package installation commands are specific to Debian-based systems, they can be adapted to the distribution of your choice by substituting the appropriate package management calls and package names.

{% hint style="info" %}
**Prerequisites:** Ensure that:

* you have the appropriate access rights to mount NFS volumes.
* you have the following details: `IONOS_TOKEN` and `NFS_CLUSTER_UUID`. You can retrieve them from the DCD or using the API.
  {% endhint %}

## Target audience

This tutorial is intended for administrators who want to learn how to mount this volume.

## What you will learn

By the end of this tutorial, you will learn to mount an NFS volume on Debian-based systems for efficient and scalable file storage solutions.

## Procedure

{% stepper %}
{% step %}

#### Retrieve the IP address and NFS share path

Retrieve the IP address and NFS share path of an NFS cluster from the IONOS Cloud API and export it as an environment variable:

The following example assumes that your cluster is located in Berlin, Germany. You must use the appropriate [<mark style="color:blue;">endpoint</mark>](https://docs.ionos.com/cloud/storage-and-backup/network-file-storage/api-how-tos) if the cluster is located elsewhere.

1\. Execute the following command to retrieve the IP address of an NFS cluster:

{% hint style="info" %}
**Note:** Remember to replace `IONOS_TOKEN` and `NFS_CLUSTER_UUID` with your authentication token and the NFS Cluster UUID, respectively.
{% endhint %}

```bash
export NFS_CLUSTER_IP=`curl -s -X GET -H 'accept: application/json' -H "Authorization: Bearer ${IONOS_TOKEN}" https://nfs.de-txl.ionos.com/clusters/${NFS_CLUSTER_UUID} | jq .properties.connections[].ipAddress | cut -c2- | cut -d'/' -f 1` 
```

| **Command**                                                 | **Description**                                                                                                                                                            |
| ----------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `NFS_CLUSTER_IP`                                            | This is the name of the environment variable being set.                                                                                                                    |
| `-H 'accept: application/json'`                             | This option specifies the Accept header to be used in the request, which is set to application/json to indicate that the client expects the response to be in JSON format. |
| `-H "Authorization: Bearer ${IONOS_TOKEN}"`                 | Specifies the Authorization header to be used in the request, which includes the IONOS Cloud API token.                                                                    |
| `https://nfs.de-txl.ionos.com/clusters/${NFS_CLUSTER_UUID}` | The URL of the IONOS Cloud API endpoint that provides information about the NFS cluster.                                                                                   |
| `jq .properties.connections[].ipAddress`                    | The command-line JSON processor `jq` that is used to parse the JSON response from the IONOS Cloud API and extract the IP address of the NFS cluster.                       |

{% hint style="success" %}
**Expected result:** Upon execution, the command makes an HTTP request to the IONOS Cloud API, retrieves the IP address of the NFS cluster, and exports it as an environment variable named `NFS_CLUSTER_IP`, which can then be used in subsequent commands to mount the NFS cluster.
{% endhint %}

2\. Execute the following command to retrieve the first NFS share path of an NFS cluster:

{% hint style="info" %}
**Note:** Remember to replace `IONOS_TOKEN` and `NFS_CLUSTER_UUID` with your authentication token and the NFS Cluster UUID, respectively.
{% endhint %}

```bash
export NFS_SHARE=`curl -s -X GET -H 'accept: application/json' -H "Authorization: Bearer ${IONOS_TOKEN}" https://nfs.de-txl.ionos.com/clusters/${NFS_CLUSTER_UUID}/shares | jq .items[0].metadata.nfsPath | cut -d'"' -f2`
```

| **Command**                                                        | **Description**                                                                                                  |
| ------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------- |
| `NFS_SHARE`                                                        | The name of the environment variable being set.                                                                  |
| `https://nfs.de-txl.ionos.com/clusters/${NFS_CLUSTER_UUID}/shares` | The URL of the IONOS Cloud API endpoint that provides information about the NFS shares of the specified cluster. |

{% hint style="success" %}
**Expected result:** Upon execution, the command makes an HTTP request to the IONOS Cloud API, retrieves the NFS share path of the first share, and exports it as an environment variable named `NFS_SHARE`.
{% endhint %}
{% endstep %}

{% step %}

#### Install and configure the NFS client and the necessary packages

1\. On the NFS client, open a command window, and install and configure the NFS client `nfs-common`, and the necessary packages for an NFS client on a Linux system, specifically on Debian-based distributions, such as Ubuntu:

`apt-get update && apt-get -y install nfs-common autofs`

| **Command**                            | **Description**                                                                                                                                               |
| -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `apt-get update`                       | The command updates the package list on the system, ensuring that the package manager has the latest information about available packages and their versions. |
| `apt-get -y install nfs-common autofs` | The command installs new packages or updates existing ones.                                                                                                   |
| `-y`                                   | The flag automatically answers "yes" to any prompts that might appear during the installation.                                                                |
| `nfs-common`                           | The package provides common files and programs for NFS, including the `mount.nfs` command.                                                                    |
| `autofs`                               | The package provides a tool for automatically mounting and unmounting file systems when they are accessed.                                                    |

2\. Create directories that can be used as mount points for an NFS on a Linux client machine using the following command:

`mkdir -p /mnt/nfs-cluster /data/nfs-cluster`

| **Command**         | **Description**                                                                                                       |
| ------------------- | --------------------------------------------------------------------------------------------------------------------- |
| `mkdir`             | A command-line tool used to create new directories.                                                                   |
| `-p`                | This option stands for "parents" and indicates `mkdir` to create the parent directories if they do not already exist. |
| `/mnt/nfs-cluster`  | This is the first directory created in the `/mnt` directory, a common location for mounting file systems.             |
| `/data/nfs-cluster` | This is the second directory that will be created in the `/data` directory.                                           |

{% hint style="success" %}
**Expected result:** The command creates the following directories: `/mnt` , `/mnt/nfs-cluster` , `/data` , and `/data/nfs-cluster` .
{% endhint %}

3\. Mount an NFS on a Linux client machine:

`mount -t nfs ${NFS_CLUSTER_IP}:${NFS_SHARE} /mnt/nfs-cluster`

| **Command**         | **Description**                                                                                                                |
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
| `mount`             | A command-line tool used to attach a file system to a directory on a Linux system.                                             |
| `t nfs`             | This option specifies the type of file system to be mounted, which in this case is an NFS.                                     |
| `${NFS_CLUSTER_IP}` | This is the IP address of the NFS server or cluster exporting the file system. Remember to replace it with a valid IP address. |
| `${NFS_SHARE}`      | This is the name of the exported file system or share you want to mount. Remember to mention the appropriate share.            |
| `/mnt/nfs-cluster`  | This is the mount point on the client machine where the NFS will be attached.                                                  |

{% hint style="success" %}
**Expected result:** Upon execution, the NFS exported by the server at `${NFS_CLUSTER_IP}` is mounted to the `/mnt/nfs-cluster` directory on the client machine.
{% endhint %}
{% endstep %}

{% step %}

#### Optionally, mount the share(s) using `autofs`

Alternatively, use `autofs` to mount the share(s) on-demand:

1\. The following command adds an entry to the `/etc/auto.master` file, the main configuration file for the `autofs` service. The entry `/ - /etc/auto.nfs` tells `autofs` to use the `/etc/auto.nfs` file as the configuration file for the mount point:

`echo "/- /etc/auto.nfs" >> /etc/auto.master`

{% hint style="success" %}
**Expected result:** When you `/ - /etc/auto.nfs` entry to the `/etc/auto.master` file, `autofs` uses the `/etc/auto.nfs` file as the configuration file for the / mount point, which allows `autofs` to mount the NFS share automatically when the `/mnt/nfs-cluster` directory is accessed.
{% endhint %}

2\. Execute the following command to create a new file called `/etc/auto.nfs` with a single entry that defines an NFS share to be mounted under the `/data/nfs-cluster` directory:

{% hint style="info" %}
**Note:** Remember to replace the `${NFS_CLUSTER_IP}` and `${NFS_SHARE}` with the IP address and share UUID of the NFS server.
{% endhint %}

`echo "/data/nfs-cluster ${NFS_CLUSTER_IP}:${NFS_SHARE}" > /etc/auto.nfs`

The entry `"/data/nfs-cluster ${NFS_CLUSTER_IP}:${NFS_SHARE}"` defines an NFS share to be mounted under the `/data/nfs-cluster` directory. The `${NFS_CLUSTER_IP}:${NFS_SHARE}` specifies the IP address and share name of the NFS server.

This command is typically used with the `autofs` service, which is a Linux service that automatically mounts and unmounts file systems based on the contents of the `/etc/auto.master` and `/etc/auto.nfs` files.

{% hint style="success" %}
**Expected result:** The entry in the `/etc/auto.nfs` file indicates the `autofs` to mount the NFS share at `${NFS_CLUSTER_IP}:${NFS_SHARE}` under the `/data/nfs-cluster` directory. When the `/data/nfs-cluster` directory is accessed, `autofs` automatically mounts the NFS share.
{% endhint %}

3\. Restart the `autofs` service on a Linux system using the following command:

`service autofs restart`

{% hint style="info" %}
**Note:** The exact command to restart the `autofs` service may vary depending on the Linux distribution being used.
{% endhint %}

Restarting the `autofs` service is typically necessary after making changes to the configuration files, such as adding or removing entries, or updating the IP address or share name of an NFS server. It re-reads the configuration files (`/etc/auto.master` and `/etc/auto.nfs`) and refreshes the list of automatically mounted file systems to ensure that the changes take effect and that the automatically mounted file systems are updated accordingly.
{% endstep %}
{% endstepper %}

### Final result

After following this tutorial, your Debian-based system should have the NFS volume mounted and accessible at the specified mount points. You can now read and write files to the NFS share, enabling efficient and scalable network file storage for your applications and users.

## Conclusion

In this tutorial, you have learned how to successfully mount an NFS volume and share it on Debian-based Systems.
