Mount an SMB Share in Linux

Installation

The LinuxCIFS utils package provides the tools needed to connect to a share and manage mounts on a Linux system. You use it to help create and manage a connection to a Windows, macOS, or Linux share.

Update the list of available packages using the below command:

sudo nala update && sudo nala upgrade

Debian

Install the both the LinuxCIFS utils package (needed to mount SMB shares) and the psmisc package (needed to gain access to the fuser command, which shows you which users are using the various mounts on your server).

sudo nala install cifs-utils psmisc

Verify that LinuxCIFS is available using the following command:

mount -t cifs

No error or output message is expected as there are no CIFS connections set up.

Mount a File System on Linux

Create an empty directory to be used as the mount point. This directory can be located wherever you wish, though it’s common to use the /mnt directory.

Enter the following command to mount the SMB share, replacing [server-ip] with the IP address of your SMB server, [share-path] with the file path to your SMB share on that server, and [mount-point] with the new directory you just created.

mount -t cifs //[server-ip]/[share-path] /[mount-point]


If the connection is successful, you should see the remote share mounted on the mount point directory you created. To verify this, type the following command:

mount -t cifs

The command above lists all mounted SMB shares. Among this list, you should see the share you just mounted.

You should now be able to access the files as if they were on a local drive. In the command below, replace [mount-point] with the directory you have created (such as /mnt/smb_share).

From here, you can run the ls command to view your files and you can interact with the files as you would any other files on your system.

Create a Credentials File

You don’t want to have to type in your credentials every time you access a share. On the other hand, putting the credentials where everyone can see is not a good idea. The following steps help you create a credentials file to automate the process of logging in.

Use your preferred text editor such as vi or nano to create a file to store the credentials. You can name the file anything you want, but using a period before the filename will hide it from view. For example, you can create a file named .credentials using the following command:

nano ~/.credentials

Add the necessary credentials to the file in the following format:

username=target_user_name
password=target_user_password
domain=domain

If the domain is not required (except on Windows systems), you can omit that entry. Replace the target_user_name and target_user_password with the actual credentials you need to use to access the SMB share. Save and close the file.

Set ownership of the credentials file to the current user by running the following command:

sudo chown <User Name>:<Credentials Filename>

Replace with your username and with the name of your credentials file.

Set the file permissions to 600 to ensure that only the owner has read and write access:

sudo chmod 600 <Credentials Filename>

To mount the share using the credentials file, run the following command:

sudo mount -t cifs -o credentials=<Credentials Filename> //<IP Address of Server>/<Share on Server> /<Mount Point>

Replace with the IP address of the server hosting the share, with the name of the share you want to mount, and with the local mount point where you want to access the share. You aren’t asked for credentials this time because mount uses the credentials file instead.

Verify that the share has been successfully mounted using the following command:

mount -t cifs

This should show you the share information as output, confirming that the share has been successfully mounted using the credentials file.

Mount a Share Automatically At Boot

Remounting the SMB share every time you restart the server can be tedious. You can instead set your server up to automatically remount the share every time you restart it using the following steps. Before starting these steps, make sure that the share is currently unmounted.

Open the /etc/fstab file in your preferred text editor. This file contains configurations that the server uses on reboot to reconnect to shares (among other things). There are columns for the file system, mount point, type, and options.

Enter the information below in each of the columns:

<file system>	<mount point>	<type>	<options>

//192.168.1.20/Music  /mnt/music    cifs    uid=0,credentials=/home/user/.credentials,iocharset=utf8,vers=3.0,noperm 0 0

From the file above, replace with the IP address of the server hosting the share, with the name of the share you want to mount, with the local mount point where you want to access the share, with the name of your credentials file,

Save the file so the share is available next time you reboot the server.

Verify that the share is mounted correctly using the as an identifier because the mount is reading the /etc/fstab file.

Unmount a Share

You may need to unmount a share at some point. To unmount an SMB share that has been mounted using the mount command, you can use the umount command followed by the mount point of the share. The correct command is umount, not unmount.

So to unmount an SMB share at the mount point , run the following command:

umount -t cifs /

The share should not appear in the output of this command.

Conclusion

You now have an understanding of SMB (and CIFS), what an SMB share is, and what a mount point is. These pieces of information allow you to share remote data in a way that’s transparent to users. From the user’s perspective, the resource is local to the server that they’re accessing. This guide also shows you how to use the mount and umount commands in a basic way to create and delete shares, how to create and use a credentials file to automate the sharing process to some extent, and how to automatically remount the share after a reboot.