CIFS Share via Systemd
This tutorial is based on using Plex and connecting shares to media sources. There are several ways that this can be done. If your files reside on a Network Attached Storage (NAS), it is common to access them via the SMB/CIFS or NFS protocols.
Mounting an SMB/CIFS share
The SMB/CIFS protocol provides support for cross-platform file sharing with Microsoft Windows, OS X, and other Unix systems.
Installing cifs-utils
Let’s return to the container’s console and install the cifs-utils package.
1
apt install cifs-utils -y
Creating CIFS Credentials
Once cifs-utils have been downloaded, create an empty text file and place it under /root/.cifs_credentials. We prefer nano, but use whichever text editor you are comfortable with.
1
nano /root/.cifs_credentials
Inside the file, add your SMB/CIFS username=
Tip: (Ctrl+o saves the file and Ctrl+x exits the editor.)
1
2
username=<username>
password=<password>
Optional: Increase security by adjusting the read/write permissions on the file to the current owner only.
1
chmod 600 /root/.cifs_credentials
Creating the Mount Directory
Create a directory in which we will mount our SMB/CIFS share. As we are using Plex, let’s give the directory the appropriate name;/mnt/plex.
1
mkdir <directory>
Retrieving Plex UID and GID
This step is necessary if you want to allow deletion of media files from within Plex Media Server.
When a directory is mounted it will belong to the user who first mounted it. In our example, the owner is root, while Plex Media Server runs as user plex. It will therefore not be able to modify any files belonging to root unless ownership is changed during the mount process.
In order to do so we need to first retrieve the User ID (uid) and Group ID (gid) of user plex. Execute the following command and write these numbers down because they will be used in the next section:
1
id -u plex && id -g plex
Systemd
We will mount our network share with systemd, rather than fstab.
Under /etc/systemd/system/, create a new unit configuration file (.mount) that inherits its name from the full path to your mounted directory, with the quirk that all forward slashes / are replaced with a hyphen -.
Example 1: Your plex directory resides under /mnt/plex. Your unit configuration file should therefore be called /etc/systemd/system/mnt-plex.mount.
Example 2: A randomly shared folder under /home/geek/Public/important_files must be called /etc/systemd/system/home-geek-Public-important_files.mount and nothing else!
Armed with this information, let’s go ahead and create the file:
1
nano /etc/systemd/system/<directory-with-hyphen>.mount
Add the following information and replace server_path, share, mount_point, uid and gid as necessary.
1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
Description=Plex Media Server files
Requires=network-online.target
After=network-online.service
[Mount]
What=//<server_path>/<share>
Where=/<mount_point>
Options=credentials=/root/.cifs_credentials,uid=<uid>,gid=<gid>
Type=cifs
[Install]
WantedBy=multi-user.target
Save and close the file.
Enable the unit configuration file:
1
systemctl enable <directory-with-hyphen>.mount
Start the directory service to automatically mount the folder:
1
systemctl start <directory-with-hyphen>.mount
Verify that the files are now seen in the mounted directory.
1
ls -l <mount-path>
Great, the files are there, and we can now go ahead and add this folder to Plex Media Server when completing the setup.