#Introduction
I have a large capacity mechanical hard drive that is currently unused, and I want to utilize it during the weekend. The requirements are:
-
I have a Raspberry Pi at home, which is running OpenWRT
as a standalone router. -
I now need to connect the mechanical hard drive to the Raspberry Pi and share it as a home network file. -
The ultimate goal is that as long as the device connects to the home WIFI
, it can access the mechanical hard drive via thesmb
protocol.
SMB
(Server Message Block
) is a network file sharing protocol primarily used for sharing files, printers, serial ports, and communication resources over a local area network. It allows applications or users to read and write files and request services between computers on the network.SMB
was originally developed byIBM
and later extended and promoted by Microsoft, widely used across major operating systems.
#1. Installing Samba
My main environment here is
OpenWrt
(Linux
), usingSamba
to achieve network sharing.
Access the OpenWrt
backend page and go to the software package section.

First, we need to update the package list.

After updating the package, search for samba4
in the search bar and select luci-app-samba4
for installation.

When installing luci-app-samba4
, it will actually install all the required packages, as shown in the following image, with four packages installed.

After installation, refreshing the page will show a network sharing menu in the services section.

#2. Creating Samba User
When sharing over the network, we should avoid using the root
user to log in to the samba
server. Therefore, we need to create a separate user for accessing the Samba
server and assign it access permissions to the folder.
Next, we need to perform operations on OpenWrt
, so we need to access it via SSH first.
ssh [email protected]
Before we begin, we can install two software packages in advance:
shadow-useradd
: Provides the useradd
command, which can be used to add users.shadow-userdel
: Provides the userdel
command, which can be used to delete users (if needed).
opkg update# Update the packageopkg install shadow-useradd# First install shadow-useradd packageopkg install shadow-userdel# First install shadow-userdel package, provides userdel command
Next, we will try to add a new user test
; later we will control permissions for the shared directory (only the test
user can access the shared directory).
useradd test# Use useradd command to create test user smbpasswd -a test# Create Samba service password for user test, will prompt for setting and confirming password

Note: If the created user is no longer needed, you can delete it using the following command:
smbpasswd -x test# Delete user test password userdel test# Delete user
#3. Creating Shared Directory
Next, create the shared file directory /share
and add a test file test.txt
mkdir -p /share cd /share echo "string" > test.txt

After creating the directory, we also need to set permissions for the shared directory user.
# Change the 'owner' and 'group' of /share directory and all its subdirectories and files to test, replace with your set username chown -R test:test /share

#4. Samba Configuration
Access the OpenWrt
backend management page and go to the network sharing page, as shown in the following image.

First, set the interface.

Add a new entry and fill in the relevant information (see the image for specific description), then save and apply.

#5. Connecting from Mac
After configuring above, the entire network shared directory has actually been set up, now let’s start testing:
On Mac
, find Go
in the menu bar ->Connect to Server

A pop-up window will appear, enter the Smb
server address, formatted as follows:smb://192.168.0.111/test
IP
is the address of my Raspberry Pi, test
is the name set during the new configuration above.

Then click connect, select Registered User
, the name is the Samba username we set at the beginning, the password is the corresponding user password, and finally click connect.

If everything goes well, you should be able to access the shared file directory, as shown in the following image:

We can also try dragging a file in here.

Then on the Raspberry Pi, you can synchronize and see the file.

#6. Mounting the Hard Drive
Above, we created a shared directory on the OpenWrt
host machine, which is just a demonstration; our actual goal is to set the external hard drive as a shared directory. However, we need to mount the hard drive to OpenWrt
first.
Before starting, use lsblk
to check the hard drive information: the purpose is to find the path of the hard drive to be mounted.
lsblk -l
From the result below, determine that the path to mount the hard drive is /dev/sdb2
(determined by the size of the hard drive).

Note: In Unix-like operating systems (such as
Linux
), the path before the hard drive/dev
representsdevice
; this is because in these operating systems, all hardware devices (including hard drives, optical drives,USB
devices, etc.) are represented as files in the filesystem, and these files are usually located in the/dev
directory.
Having determined the hard drive device path, we also need to create a directory to serve as the mount point.
mkdir -p my-data

Since this directory will later serve as the shared directory, we need to set the owner and group of the directory and files (set to Samba
user test
).
chown -R test:test my-data

Now we can start mounting the hard drive.
mount /dev/sdb2 my-data

Encountered issue: Since my hard drive is formatted as
Exfat
, the mount failed, and the reason for the failure is that by default,OpenWrt
does not supportExfat
file formats. To solve this problem, I separately installedkmod-fs-exfat
to add support forExfat
format.

#7. Sharing the Hard Drive
After mounting the hard drive, we need to modify the network sharing settings we configured above, changing the shared file path to the mounted path of the hard drive, and finally click save and apply.

Testing:Reconnect from Mac
to the Smb
server.

#8. References
-
How to add users on a Samba server -
Raspberry Pi Diary: Setting Up a Home Shared Directory