How to create a Samba share using a Raspberry Pi and an External Hard Drive

Last updated: 14/07/2020

Nothing better than network accessable storage to quickly share files within your home network, especially when you have multiple machines and a server with a couple of VMs! To make it easier on myself, I figured it was time to dust off my Raspberry Pi 2 and make this into a simple Samba share for my home network.

In this article, I am going to talk through the steps required to create a Samba share on a Raspberry Pi and extend the storage by using an external hard drive. Additionally, I will talk through the steps of setting up SSH on the Raspberry Pi, should you need to administrate it remotely.

This guide assumes a basic understanding of terminal commands and basics of networking.

Things you will need:

  • Raspberry Pi with an SD Card
  • Keyboard
  • Mouse
  • Monitor
  • HDMI and Ethernet Cable
  • External Hard Drive (Optional)

Setting up the Raspberry Pi

Download NOOBS here and unzip it and extract the files. We are going to be putting these files on the SD Card. You can simply extract the files straight to the SD card, there is no further configuration required. Make sure the SD Card is formatted to FAT32.

Once this is complete, connect the Pi up to your monitor, mouse and keyboard and insert the SD Card into the it. You will be prompted with a wizard to install various operating systems, for this we will be using Raspbian.

Installing the Samba service

Now that we have a functional Raspbian OS to play with, the first this we are going to do is make sure everything is up to date. Open a terminal and run the following commands to update the package list and upgrade any packages we have.

sudo apt-get update

sudo apt-get upgrade

After these commands have completed, our Raspbian OS should be up to date. Now we can install the Samba package by running the following command:

sudo apt-get install samba samba-common-bin

Once this command completes, we will have the Samba service installed.

Before we go into the configuration of Samba, we need to create a folder on the Pi that we are going to share (please note you can share multiple folders as multiple shares). In this case, I am going to use my external hard drive.

Formatting an External HDD for Samba

The first time I initially did this setup, I tried to use an External Hard Drive that was formatted to NTFS, however this caused a number of issues when it came to writing to the share remotely. The NTFS filesystem was treated as Read-Only. After several attempted workarounds, I gave up and formatted the drive to Ext4 which solved the issue. For this section of the guide, I will walk through formatting a drive as Ext4 and creating a folder on the drive which we will share via the Samba service.

First, find the drives path by running the following command:

sudo fdisk -l

you should be able to recognise your drive by the size, the path we are looking for will look something like this: /dev/sda1

Once you have identified your external hard drive, you can use fdisk to format the partition. If you are formatting a hard drive with existing partitions and want the delete these then you can use the following commands:

sudo fdisk /dev/sda1

The above command will run fdisk against the chosen drive, in this case mine was /dev/sda1

Next, we hit:

d

Hitting ‘d’ and <ENTER> this will delete the partitions, if you have multiple onea, then you will need to run this command multiple times and select the partition number.

Once you have deleted the partitions, you will then need to write the changes to the disk. We can do this by typing ‘w’ and hitting <ENTER>.

w

This will write the changes to the partition table.

Next, we need to create the new partition.

sudo fdisk /dev/sda1

n

p

1

w

To explain, ‘n’ will navigate you to the new options. ‘p’ will select partition. ‘1’ will specify the partition number. Finally, ‘w’ will write the changes. 

We have created a new partition, now we need to create the filesystem and format the partition. You can do this by running the following command:

sudo mkfs -t ext4 /dev/sda1

Now we need to mount the file system. First, create a folder on the Pi that the device will mount to. In this case, I created a folder in the root called ‘exhdd’. This folder will be used as the mount point. To create this folder, you can run the following command:

sudo mkdir /exhdd

Now that we have the folder to mount to, we just need to mount the drive and map it to the folder.

sudo mount /dev/sda1 /exhdd

If we then navigate to /exhdd, this is now our external hard drive. If you want, you can create a folder in here which will be used as the share.

sudo mkdir /exhdd/share

Now that we have the folder to mount to, we just need to mount the drive and map it to the folder.

sudo mount /dev/sda1 /exhdd

If we then navigate to /exhdd, this is now our external hard drive. If you want, you can create a folder in here which will be used as the share.

sudo mkdir /exhdd/share

So now we have our share folder on our external hard drive, which has now been mounted.

We can now configure Samba to share this folder as a network share.

Configuring the Samba service

By this point, you should have Samba installed and a folder in which you want to share, in this example I will use the folder created on my external hard drive. To configure Samba, we need to modify the samba config file. For this we will use Nano which is a command line text editor, you could also use mousepad or leafpad if you have installed them.

sudo nano /etc/samba/smb.conf

Within this config file, scroll write down to the bottom and add the following text:

[Master Share]

path= /exhdd/share

writable = yes

public=no

create mask=0777

directory mask=0777

Let’s talk through this line by line.

[Master Share] – This is the name of the share. The brackets are required but will not be visible as part of the share.

Path – This is the folder that we are sharing on the Pi, in this case it’s a folder that is located at root which is the mount point for my External Hard Drive.

Writable – With this set to yes, users will be able to write data to the share. Setting this to no will make the share read-only.

Public – This setting adjusts anonymous access to the share. Setting this to no requires a valid user to authenticate to gain access to the share.

Create Mask – These numbers define the permissions for the files and folders within the share. By setting this to 0777, authenticated users will be allowed to read, write and execute.

As this share is not public (i.e. Users will need to authenticate), we will need to create an identity to access the Samba service. You can do this by running the following command:

sudo smbpasswd -a root

The next line will allow you to define a password. Please note, this is different to the raspberry pi user account and is used for authenticating to the Samba share.

With these steps done, we just need to restart the Samba service, you can run the following command:

sudo service smbd restart

Getting the Hostname and Enabling SSH

Before you log off the pi, there are a few things we should do. We need to find the hostname of the pi so we can locate the pi on our network and we should enable SSH so we can remote access the pi without a monitor, mouse and keyboard.

To find the hostname, you can run:

hostname

If you want the IP address, you can run:

hostname -I

However, the IP address will change if its assigned by DHCP, so make sure you reserve this address on your DHCP server/Router if you are connecting via IP!

To enable SSH, run the following command:

sudo raspi-config

Select ‘Interface Options’

Select SSH and change this to enabled

Finish and quit out.

You can now run this Pi as a headless device (No monitor or peripherals). You can manage and administrate it by using Putty to establish an SSH session.

Mounting the share on a windows machine.

Moment of truth time!

Open file explorer and type in the following:

\\”Hostname or IP of Raspberry PI”\”Share Name”\

For me, it was:

\\192.168.0.105\master\

You will then be prompted for authentication, use the Samba account and password that you created, you will then be able to access the share!

You can also map this drive to your ‘My Computer’, for easy access. To do this, navigate to the hostname/IP of the Pi and right click the share and select ‘Map Network Drive’, example below:

Select a Drive Letter to mount it to and make sure to tick the ‘Use different credentials’ so it doesn’t use your windows account to authenticate. This will then map the network drive to ‘My Computer’ for easy access.

You can now start saving and sharing files over your network! You can even point things like backups to your network share if needed.

In terms of speeds for transfer, I wasn’t getting a particularly high throughput when copying files to and from the share. Both my computer and the Pi are connected by Ethernet and not wireless. While the External Hard Drive I have is USB 3.0, the Raspberry Pi is not, this is likely the bottleneck. If possible, I would recommend getting one of the newer Pi’s which have USB 3.0 and an External Hard Drive which is also USB 3.0.

And there you have it! A guide to setup Samba on a Raspberry Pi to share files across your network!

I am sure there is a command that I am missing, so please do let me know if anything is missing or needs to be corrected! Happy Reading!

Leave a Reply

Your email address will not be published. Required fields are marked *