Codementor Events

Mounting Azure Files on Linux: Step by Step

Published Nov 24, 2019
Mounting Azure Files on Linux: Step by Step

Azure Files is a fully-managed shared file storage service, hosted by Azure in the cloud. Azure File’s reads and writes are done through the Server Message Block (SMB) protocol. There are other options, such as REST API calls and storage client libraries.

There are many benefits to Azure File Storage, but the key advantage is the SMB protocol, which is mostly used for on-premises data centers. If you’re interested in going hybrid and integrating a cloud environment with your on-prem data, then Azure Files could be a great fit for you.

You can use the SMB protocol to easily share data between your on-prem apps and Azure Files. It’ll also be a quick cloud migration—no need to re-architect your data. You just “lift” the data and “shift” it to the cloud. Plus, you can use Azure CLI and PowerShell to automate tasks.

This article provides a quick step-by-step guide for mounting Azure Files on Linux. If you’re interested in mounting Azure Files with Windows, you can try following the instructions here.

What Do You Need to Get Started?

In order to mount an Azure Files share on a Linux machine, make sure you prepare the following:

  • Decide where to locate the mount point for the file share—a common place is under the folder /mnt
  • It is highly recommended to upgrade to a Linux version that supports SMB 3.0. This will allow you to use encryption in transit. You will need Ubuntu 16.4+, RHEL 7.5+, CentOS 7.5+, Debian 10+, openSUSE 42.3+, or SUSE Linux 12 SP3+
  • Install cifs-utils and Azure CLI
  • Open port 445—this is the port used by the SMB protocol

How to Mount an Azure Files Share as Persistent Drive

Follow this procedure to create a persistent mount point for an Azure Files share. This means that the file share will always appear on boot as an additional drive on your Linux machine. Code examples are adapted from Azure documentation.

Step 1. Create the mount point

Create a local directly on the Linux machine, this will be the “mount point” for the Azure Files share. Use the following code, replacing <STORAGE-ACCOUNT-NAME> and <FILE-SHARE-NAME> with the appropriate names from your Azure Files setup.

mntPath="/mnt/<STORAGE-ACCOUNT-NAME>/<FILE-SHARE-NAME>
sudo mkdir -p $mntPath

Step 2. Create a credential file

You will now create a file to store the Azure storage account name and the storage account key, to allow the Linux machine to access the data.

if [ ! -d "/etc/smbcredentials" ]; then
sudo mkdir "/etc/smbcredentials"
fi

storageAccountKey=$(az storage account keys list
--resource-group <RESOURCE-GROUP-NAME>
--account-name <STORAGE-ACCOUNT-NAME>
--query "[0].value" | tr -d '"')

smbCredentialFile="/etc/smbcredentials/<STORAGE-ACCOUNT-NAME>.cred"

Security warning: It’s important to set permissions on the above file, so only the root user can read or modify it. Otherwise just about anyone will have access to your data. The storage account key is a super-admin password to your entire storage account. You can do this with the following command:

sudo chmod 600 $smbCredentialFile

Step 3. Grant permissions

Append a line to /etc/fstab to define permissions for the file share. You can use UNIX numeric notation to define permissions; use the uid and gid mount options to set a user ID and group ID for the mounted file share.

httpEndpoint=(az storage account show \ --resource-group \ --name \ --query "primaryEndpoints.file" | tr -d '"') smbPath=(echo httpEndpointcutc7httpEndpoint | cut -c7-(expr length $httpEndpoint))<FILE-SHARE-NAME>

How to Mount an Azure File Share During a Linux Session

Follow this procedure to mount an Azure file share, which will be available during the current session, but will not be maintained after you reboot the machine.

1. Create a local folder as mount point

Use the code below to create a local mount point, replacing <STORAGE-ACCOUNT-NAME> and <FILE-SHARE-NAME> with the appropriate names from your Azure Files setup.

mntPath="/mnt/<STORAGE-ACCOUNT-NAME>/<FILE-SHARE-NAME>
sudo mkdir -p $mntPath

2. Mount the Azure file share

Use a command like the one below to mount the file share, and set default file and folder permissions (read, write, and execute for the Linux owner defined for the file or directory). As with a persistent file share, you can use UNIX numeric notation to define other permissions.

httpEndpoint=(az storage account show \ --resource-group \ --name \ --query "primaryEndpoints.file" | tr -d '"') smbPath=(echo httpEndpointcutc7httpEndpoint | cut -c7-(expr length $httpEndpoint))<FILE-SHARE-NAME>

storageAccountKey=$(az storage account keys list
--resource-group <RESOURCE-GROUP-NAME>
--account-name <STORAGE-ACCOUNT-NAME>
--query "[0].value" | tr -d '"')

sudo mount -t cifs $smbPath mntPathovers=3.0,username=mntPath -o vers=3.0,username=storageAccountName,password=$storageAccountKey,serverino

And you’re done! You should now be able to access files from the Azure file share within the folder you defined as your mount point.

Conclusion

Mounting Azure Files on Linux is not a difficult task. However, it does take some time and practice to figure out the right process for your workflow and operations. Take the time to experiment with different steps, a variety of resources, and a mix of tools. If possible, create fake environments that mimic your real environment, and then do some testing. You’ll gain some much-needed wisdom from the experience, without risking real data.

Discover and read more posts from Gilad David Maayan
get started
post commentsBe the first to share your opinion
Show more replies