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

## Overview

This tutorial will guide you through the process of mounting an Network File System (NFS) volume on Debian-based systems. It is intended for administrators who want to learn how to mount this type of volume. The configuration in the tutorial is specific to self-managed NFS 4.2 servers.

By the end of this tutorial, you will be able to mount an NFS volume on Debian-based Systems.

## Pre-requisites

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.

## Procedure

{% stepper %}
{% step %}

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

After your cluster has been set up, retrieve the IP address and NFS share path of an NFS cluster from the IONOS API and export it as an environment variable.

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

```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` 
```

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

| **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 API token.                                                                          |
| `https://nfs.de-txl.ionos.com/clusters/${NFS_CLUSTER_UUID}` | The URL of the IONOS 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 API and extract the IP address of the NFS cluster.                             |

{% hint style="success" %}
**Result:** Upon execution, the command makes an HTTP request to the IONOS 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 %}

1.2. Execute the following command to retrieve the NFS share path of an NFS cluster:

```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[].metadata.nfsPath | cut -d'"' -f2` 
```

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

| **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 API endpoint that provides information about the NFS shares of the specified cluster. |

{% hint style="success" %}
**Result:** Upon execution, the command makes an HTTP request to the IONOS 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

2.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.2. To determine which file systems are available for mounting from an NFS server, execute the following command:

`showmount -e ${NFS_CLUSTER_IP}`

| **Command**         | **Description**                                                                                                                       |
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| `showmount`         | A command-line tool used to query an NFS server for its exported file systems.                                                        |
| `-e`                | This option stands for "exports". It tells `showmount` to display the list of exported file systems from the specified server.        |
| `${NFS_CLUSTER_IP}` | The variable that represents the IP address of the NFS server or the cluster. Remember to replace it with the appropriate IP address. |

{% hint style="success" %}
**Result:** It displays a list of exported file systems, allowing you to choose which one to mount on your client machine.
{% endhint %}

2.3. 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, which is 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" %}
**Result:** The command creates the following directories: `/mnt` , `/mnt/nfs-cluster` , `/data` , and `/data/nfs-cluster` .
{% endhint %}

2.4. 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 that is 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 that 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" %}
**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 %}

#### Mount the share(s)

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

3.1. The following command is used to add an entry to the `/etc/auto.master` file, which is 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" %}
**Result:** By adding the `/ - /etc/auto.nfs` entry to the `/etc/auto.master` file, you are telling autofs to use 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 %}

3.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:

`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.

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

This command is typically used in conjunction 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" %}
**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.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 %}

## Summary

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ionos.com/cloud/~/revisions/UIHN5vc23LyGCaurOi2f/storage-and-backup/network-file-storage/tutorial/mount-nfs-volume.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
