Sharing a Raspberry Pi directory on a local area network

Sharing a Raspberry Pi Directory on a Local Area Network
Published: by
Last updated:
Categories
Raspberry Pi

This is a short, somewhat introduction, post on how to share a Raspberry Pi directory on a local area network (LAN) and will conclude with how to connect to this directory using Linux-based and Windows-based operating systems. Sharing will be done by using the Linux package Samba, which can be easily installed onto a Raspberry Pi running Raspbian.

The shared directory can be any accessible directory on the Raspberry Pi itself (e.g. a local directory, USB mount, etc.). In order to make this process somewhat more usable (especially between different operating systems), the share directory will be without privileges and will be fully accessible to all users on the LAN.

Assumptions & requirements

fully working, networked Raspberry Pi with the latest/updated version of Raspbian and sudo rights. Everything here will be explained by using the terminal. Although a static IP is not required to share the directory to Windows, it is recommended to gain access from another Raspberry Pi (Linux). If not connected to a screen with a keyboard and mouse, this process can also be done by using PuTTY.

The shared directory

The shared directory can be any directory on the Raspberry Pi itself. Here the /home/pi/share directory will be used.

To create this directory, use the mkdir command:

mkdir /home/pi/share

where /home/pi is the working directory and share is the shared directory.

User permissions

User permissions (or user rights) add extra security to files and directories. Directories can be made readable, writable and/or executable. For the shared directory these permissions can be changed according to need.

To makes things easy and give full privileges to the shared directory use:

sudo chmod -R 777 /home/pi/share

This will also set the GID for the directory. Similarly, use:

sudo chmod 777 /home/pi/file-name.sh

to change permissions for a specific file.

Workgroup

A workgroup is a group of computers on a local network that can access each other’s directories. Samba is perfect for managing this (see below). A new workgroup can be created, or the name of an already created workgroup can be used. The default workgroup created by Microsoft Windows is usually WORKGROUP, so this one will be used.

Samba

To share a directory from a Raspberry Pi to a LAN, Samba will need to be installed and properly configured. Samba contains the SMB protocol, support for the Windows naming service (WINS) and support for joining Windows workgroups.

To download Samba:

sudo apt-get install samba
sudo apt-get install samba-common-bin

It is recommended to make a backup of the original samba configutation file:

sudo cp -p /etc/samba/smb.conf /etc/samba/smb.conf.original

To edit and configure the Samba configuration file:

sudo nano /etc/samba/smb.conf

The ==Global Settings== section ([global]) is divided into various subsections.

In the ## Browsing/Identification ### section, change and activate the following lines (remove # where needed and add):

workgroup=WORKGROUP
wins support=yes
netbios name=hostname

Explanations

workgroup=WORKGROUP sets the workgroup of the users that are allowed access.

wins support=yes (default no) allows the Raspberry Pi to be accessed by using the netbios name from windows based systems.

netbios name=hostname is the name the shared directory will be broadcasted as on Windows systems. The hostname can be obtained by using hostname in the terminal.

Then way down at the bottom of this file, in the ==Share Definitions== section, shared directories can be added as follows:

[PiShare]
comment=Raspberry Pi Share Directory
path=/home/pi/share
browseable=yes
guest ok=yes
read only=no
create mask=0777
force create mode=0777
directory mask=0777
force directory mode=02777
force user=pi

Explanations

[PiShare] is the share directory mounting name, this will share this directory as 192.168.1.xx/PiShare or \hostname\PiShare where 192.168.1.xx is the IP address of the sharing Raspberry Pi.

comment=Raspberry Pi Share Directory is the description of the share directory.

path=/home/pi/share is the absolute path to the local shared directory

browseable=yes (yes/no) makes the shared directory visible using net view.

guest ok=yes (yes/no) allow guest (anonymous) access without authentication.

read only=no (yes/no) allow users to create or edit files.

create mask=0777 -> default is 0755
force create mode=0777
directory mask=0777 -> default is 0755
force directory mode=02777
force user=pi

These 5 lines will enable everyone in the workgroup to create directories and files in the shared directory. It will also set the authors of these files and directories to ‘pi’.

Save on exit (Ctrl + X then Y).

Reboot the Raspberry Pi ( sudo reboot).

See the official configuration file for the Samba suite for more information.

Testing, start and/or stop Samba

To see what Samba is doing use:

service samba status

If Samba is not running, it can be activated by:

sudo service samba start

or to stop Samba use:

sudo service samba stop

After activation, Samba should start automatically after every reboot. To remove Samba from the boot sequence use:

sudo update-rc.d -f samba remove

This will not stop Samba, but will prevent it from running on reboot. To add Samba back to the boot sequence use:

sudo update-rc.d samba defaults

To view the Samba shares for a user, use:

smbstatus

Connecting to the shared directory

Once the Samba service has started, the shared directory can be accessed from other computers on the LAN.

Microsoft Windows

If used correctly, the Raspberry Pi share should show up under the networked systems on the LAN. \\hostname\sharename can also be used in a files explorer (internet browsers will probably not work) to access the shared directory directly. The hostname should be similar to the netbios name used in the Samba configuration file.

Linux (incl. Raspbian)

A list of the shared directories on the LAN can be obtained from another system by using:

smbclient -L hostname -U username

where hostname is the hostname or IP of the sharing Raspberry Pi and username is the username of the connecting Raspberry Pi. By using the settings above, anyone in the workgroup will have access to the share.

If the command does not work, smbclient can be installed by using:

sudo apt-get install smbclient

From a Linux file manager try smb://hostname/sharename.

To mount a shared directory through the terminal the sharing Raspberry Pi’s IP address will be required and CIFS utilities needs to be installed. A static IP address will be very handy for continual mounts. To configure a static IP see set up a static IP address on the Raspberry Pi or set up a static IP address on the Raspberry Pi using Wi-Fi.

To install CIFS utilities use:

sudo apt-get install cifs-utils

To get the sharing Raspberry Pi’s IP address(es) use:

hostname -I

The mount command is:

sudo mount -t cifs //192.168.1.xx/PiShare /mnt/share -o password=

where 192.168.1.xx is the IP for the sharing Raspberry Pi, PiShare is the share name configured earlier, and /mnt/share is the mounting directory on the host Raspberry Pi.

To unmount the share from user side use umount /mnt/share.

sudo umount /mnt/share

Mount at boot

To automatically mount a shared folder during the boot sequence of Raspbian, the mounting command can either be added to Crontab or the rc.local file.

To add the mount command to the rc.local file use:

sudo nano /etc/rc.local

and add the mount command to the bottom of the file.

For Crontab, use:

crontab -e

and add:

@sudo mount -t cifs //192.168.1.xx/share /mnt/share-folder -o password=

Errors & solutions

Host is down error

Lately, while trying to use the Samba mount command, the following error is displayed:

mount error(112): Host is down

The Host is down error is likely to occur after a certain package update which needed the version to be defined. Defining an earlier version, (vers 1.0 in this case), will solve this problem:

sudo mount -t cifs //192.168.1.xx/share /mnt/share-folder -o vers=1.0,password=

Note the comma between the options.

Further reading

Looking at this post, we’ve barely scratched the surface of the configuration options of Samba. See the Samba site and reading more about the configuration options available in smb.conf for more.

About the author
Renier busies himself with improving his English writing, creative web design and his websites, photoshopping, micro-electronics, multiple genres of music, superhero movies and badass series.
Behind the Scenes is a free, informative website. If you find value in any of our content, please consider making a donation to our cause.
Donate via PayPal

Save, share & Disqus

Use the buttons below, on the left or the bottom of this page to share this post. Your comment is important, but don't be a knob. Keep it constructive and polite.

Comment via Disqus

Disqus is a worldwide comment hosting service for web sites and online communities. This secure platform ensures a pleasant commenting environment which is manageable from one account. Use the Login button to sign up.

More home network related posts

Communicating between 2 Raspberry Pi's using sshpass
Communicating between 2 Raspberry Pis using sshpass
10 September 2016 | Updated 24 October 2018
sshpass is a Linux software package that can be used to establish a direct (secure) connection between two Linux operated computers. This connection allows ‘remote’ access to a ‘local’ computer to run commands ‘locally’. Communicating between 2 Raspberry Pi’s using sshpass will show you how. More…
Categories
Raspberry Pi
Understanding common network protocols
Understanding some common network protocols
7 September 2016 | Updated 29 February 2020
Deeper exploration into the world of inter-computer communications will reveal more and more types of communications (e.g. radio waves, Ethernet, Bluetooth, etc.), their interfaces and the network protocols they use. More…
Using the Raspberry Pi as a home web server
Using the Raspberry Pi as a home web server
18 January 2016 | Updated 19 November 2017
A web server is a computer that can send and receive data via a web browser. It uses, the very popular, Hypertext Transfer Protocol (HTTP), the basic network protocol used to distribute information on the internet. More…