Easy steps to share an external USB drive on your Raspberry Pi
By: Perminder Singh
Tags: Raspberry Pi, External Hard Drive, Home Projects, Samba Server, Fun

This guide is tested on all versions of Raspberry Pi available.

In order to share a drive you need to perform the following steps:

Connect the drive and find its UUID and Type Create a folder to mount the drive Change ownership of the new folder Mount the drive to the folder Test the mounted drive Install Samba server Change Samba configuration to add a new shared folder Accessing the shared drive Change Raspberry Pi configuration to auto mount the drive when Raspberry Pi restarts

Connect the drive and find its UUID and Type

Insert the drive on the USB Port on your Raspberry Pi

Connecting the external drive to Raspberry Pi

Go to Raspberry Pi console and type this command

blkid

Output of the command will be something like this

pi@hostname:~ $ blkid /dev/mmcblk0p1: LABEL_FATBOOT="boot" LABEL="boot" UUID="<some-uuid>" TYPE="vfat" PARTUUID="<some-part-id>" /dev/mmcblk0p2: LABEL="rootfs" UUID="<some-uuid>" TYPE="<TYPE>" PARTUUID="<some-part-id>" /dev/sda1: LABEL_FATBOOT="EFI" LABEL="EFI" UUID="<some-uuid>" TYPE="<TYPE>" PARTLABEL="EFI System Partition" PARTUUID="<some-part-id>" <Drive Path>: LABEL_FATBOOT="<External Drive Label>" LABEL="<External Drive Label>" UUID="<USB-DRIVE-ID>" TYPE="<TYPE>" PARTUUID="<USB-DRIVE-PART-ID>"

Note the part in bold which will be the the drive details. This means that the drive is successfully connected and accessible. Copy the following details from the output as they be used in next steps (Note: the values I have mentioned is for representation purpose only and will be different for you)

Drive path: <Drive Path>

UUID: <USB-DRIVE-ID>

Type: <Type> (Most commonly it is vfat)

Create a folder to mount the drive

Create a folder by running this command

sudo mkdir /media/usbpendrv

In the command, part in the bold is the folder name. You can choose any name you want.

Change ownership of the new folder

Change ownership of the newly created folder. This step is necessary as without that the contents of the drive will not be accessible to the pi user.

Run this command

sudo chown -R pi:pi /media/usbpendrv

Note pi:pi in the command. Here first pi is the user name and second pi is the group name.

Mount the drive to the folder

Run this command to mount the drive to the created folder

sudo mount <Drive Path> /media/usbpendrv -o uid=pi,gid=pi

Path in bold is the drive path copied from step 1

Test the mounted drive

Go to the folder where the drive is mounted now and test if you are able to access and write to it. You can use the following commands to test. These commands create and delete a folder named “testfolder”.

cd /media/usbpendrv mkdir testfolder cd testfolder cd /media/usbpendrv rm -rf testfolder

Now, we have already tested the mounted drive. It it time to create a shared folder on Raspberry Pi and access it on the network.

Install Samba server (Skip this step if running RetroPi as it already has it pre-installed)

Samba server is a tool to share a folder on the network. It is one of the oldest and greatest tool to do the job. Also, it installs the script to add a system service during its installation to auto run itself when Raspberry Pi restarts.

Before installing, update Raspberry Pi with these commands

sudo apt update sudo apt upgrade

Refer Raspberry Pi site for more advanced commands

After update is done, install Samba server with this command

sudo apt-get install samba samba-common-bin

Change Samba configuration to add a new shared folder

To add the configuration, take backup of existing samba configuration (/etc/samba/smb.conf)

sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.old

Open samba configuration

sudo nano /etc/samba/smb.conf

Add the following to the samba configuration, save and exit from it

[shared]

comment = Shared with all

path = /media/usbpendrv

browseable = yes

writable = yes

read only = no

guest ok = yes

create mask = 0777

directory mask = 0777

force user = pi

directory mask = 0777

force user = pi

If in the external drive, you want to share only a specific folder rather than whole drive then instead of giving the mount point of the shared drive /media/usbpendrv use the folder path instead /media/usbpendrv/<path>

Restart the samba server

sudo systemctl restart smbd

This step completes sharing the drive on the network, so let us test it.

Accessing the shared drive

On the computer where you want to access the shared network drive, use the following steps:

On Mac:

Open Finder

Go to Go → Connect to server…

Enter the Name/IP Address of the Raspberry Pi on the network using the below syntax.

if the name of the Raspberry Pi is “raspberrypi” then path will be smb://raspberrypi if the IP Address is “192.168.0.32” then path will be smb://192.168.0.32

Entering path for the Raspberry Pi

Press “Connect”

Choose “Guest”

Selecting user credentials

Press “Connect”

In the next screen, you will see “shared” as selected

Selecting shared drive folder

Press “OK” and you will see a drive mounted in the finder

Mounted shared folder

On Windows

Open up File Explorer and then right-click on This PC (in the left pane). From the resulting context menu, select “ Add a network location ”

” A new wizard will open, one that will walk you through the process of creating a shortcut for a new network location within File Explorer.

Enter the path in the following syntax //<IP Address>/shared

Follow next steps adding name for the share and you are good to access the shared drive.

On Linux

To access the shared folder you need the following command

smbclient //<IP Address>/shared

or

mount -t cifs //<IP Address>/shared <mount point>

Change Raspberry Pi configuration to auto mount the drive

The last steps is to change the Raspberry Pi configuration to auto mount the external drive every time Raspberry Pi restarts. To do so, follow the following steps:

Open /etc/fstab in a editor

sudo nano /etc/fstab

Add the following configuration, save and exit

UUID=<UUID from Step1> /media/usbpendrv <Type from Step1> auto,nofail,noatime,users,rw,uid=pi,gid=pi 0 0

Note the following:

Enter UUID and Type copied from Step 1 Ensure that the text is added in a single line. The text shown above is wrapped by the page but is in single line.

Restart Raspberry Pi

sudo reboot

After Raspberry Pi is rebooted, you will have the shared folder accessible across network and ready to use.