OpenWrt and Samba: A Guide to Home Network File Sharing

#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 the smb 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 by IBM and later extended and promoted by Microsoft, widely used across major operating systems.

#1. Installing Samba

My main environment here is OpenWrt (Linux), using Samba to achieve network sharing.

Access the OpenWrt backend page and go to the software package section.

OpenWrt and Samba: A Guide to Home Network File Sharing

First, we need to update the package list.

OpenWrt and Samba: A Guide to Home Network File Sharing

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

OpenWrt and Samba: A Guide to Home Network File Sharing

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

OpenWrt and Samba: A Guide to Home Network File Sharing

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

OpenWrt and Samba: A Guide to Home Network File Sharing

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

sh
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).
sh
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).

sh
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
OpenWrt and Samba: A Guide to Home Network File Sharing

Note: If the created user is no longer needed, you can delete it using the following command:

sh
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

sh
mkdir -p /share cd /share echo "string" > test.txt
OpenWrt and Samba: A Guide to Home Network File Sharing

After creating the directory, we also need to set permissions for the shared directory user.

sh
# 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
OpenWrt and Samba: A Guide to Home Network File Sharing

#4. Samba Configuration

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

OpenWrt and Samba: A Guide to Home Network File Sharing

First, set the interface.

OpenWrt and Samba: A Guide to Home Network File Sharing

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

OpenWrt and Samba: A Guide to Home Network File Sharing

#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

OpenWrt and Samba: A Guide to Home Network File Sharing

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

OpenWrt and Samba: A Guide to Home Network File Sharing

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.

OpenWrt and Samba: A Guide to Home Network File Sharing

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

OpenWrt and Samba: A Guide to Home Network File Sharing

We can also try dragging a file in here.

OpenWrt and Samba: A Guide to Home Network File Sharing

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

OpenWrt and Samba: A Guide to Home Network File Sharing

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

sh
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).

OpenWrt and Samba: A Guide to Home Network File Sharing

Note: In Unix-like operating systems (such as Linux), the path before the hard drive /dev represents device; 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.

sh
mkdir -p my-data
OpenWrt and Samba: A Guide to Home Network File Sharing

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

sh
chown -R test:test my-data
OpenWrt and Samba: A Guide to Home Network File Sharing

Now we can start mounting the hard drive.

sh
mount /dev/sdb2 my-data
OpenWrt and Samba: A Guide to Home Network File Sharing

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 support Exfat file formats. To solve this problem, I separately installed kmod-fs-exfat to add support for Exfat format.

OpenWrt and Samba: A Guide to Home Network File Sharing

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

OpenWrt and Samba: A Guide to Home Network File Sharing

Testing:Reconnect from Mac to the Smb server.

OpenWrt and Samba: A Guide to Home Network File Sharing

#8. References

  • How to add users on a Samba server
  • Raspberry Pi Diary: Setting Up a Home Shared Directory

Leave a Comment

×