Sharing a Raspberry Pi directory on a local area network

Sharing a Raspberry Pi directory

By sharing a Raspberry Pi directory on a local area network, it can be accessed by other devices. Considerations and potential problems are also mentioned.

Introduction to sharing a Raspberry Pi directory on a local area network

A shared directory (or folder) on a Raspberry Pi will give it access to other devices on the same local area network (LAN) — Linux- and Windows-based operating systems are discussed. Its contents can add value to integrated projects or its hard disk can add storage space.

The network sharing service and configuration can be done using Samba — a free, open-source software package easy installed using apt-get.

Although Samba is Linux-based, it can also communicate with Windows devices. Samba provides its sharing service by employing the Common Internet File System (CIFS).

In the spirit of personal tinkering, security considerations will be kept to a minimum.

Requirements and basic setup of the Raspberry Pi

The first thing that is needed is a fully configured Raspberry Pi running, preferably the latest release of the Raspberry Pi OS (formerly known as Raspbian). The installation and configuration will be done using terminal commands, so either the Lite or the Full Version can be used. Sudo privileges will be required. The default pi was used as the username and raspberry was used as the password.

A connection to the internet will be required to install the required packages. To be able to connect over the local area network, the Raspberry Pi should also have a static and/or known IP address.

Hardware and software used

The setup used while writing this post was a Raspberry Pi 3 Model B with the Full version of Raspbain Stretch as OS. An 8Gb class 10 microSD card was used. The Raspberry Pi was configured locally using a keyboard and mouse.

A static IP address and SSH were enabled and a connection to the LAN and the internet was via Wi-Fi. After the initial configuration was done, a Windows 10 computer with PuTTY was used to connect to the CLI of the Raspberry Pi.

The shared directory on the Raspberry Pi

The shared directory on the Raspberry Pi can be any directory on the Raspbain directory structure. This includes a newly-created directory, an existing local directory and a directory where another directory has been mounted to  (e.g. a USB mount).

Creating a new directory

To create a new directory and move the cursor to that directory, the mkdir and cd terminal commands can be used. In this case, the /home/pi/share directory was used:

mkdir /home/pi/share
cd /home/pi/share

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

Creating a new file

Nano can be used to create new files. To be able to save a file, it needs to contain some text. New files can be created by using the following terminal command:

nano /home/pi/share/textfile.txt

where /home/pi/share/ is the directory where the file is to be created and the textfile.txt is the filename and its extension that is to be created.

With Nano, the file can be saved by pressing Ctrl + x and then y and Enter on the keyboard.

To see if textfile.txt has been created successfully, the dir terminal command can be used while the cursor is inside the /share directory:

cd /home/pi/share
dir

User permissions

For a degree of network security, the current user and group permissions of the shared directory also need to be considered. As mentioned earlier, this post will not be focused on network security and permissions.

To make the process as simple and as usable as possible (especially between different operating systems), the shared directory will be without privilege restrictions and will be fully accessible to all users on the LAN.

File and directory permissions can be seen by using the ls terminal command:

ls -al /home/pi/share

By default the /share directory only allows its Owner (pi) to have read, write and execute privileges (octal 755).

Full user privileges can be given to a directory by using the chmod (changemode) terminal command. By using chmod with the -R (recursive) flag, all the directories and files inside the directory will get the same privileges. The chmod command needs to be used with sudo privileges:

sudo chmod -R 777 /home/pi/share

where 777 refers to the new octal of the directory. If the recursive flag was omitted, then the permissions of the files (and other directories) within the directory need to be set separately. To set the permissions for a file (e.g. the textfile.txt, use:

sudo chmod 777 /home/pi/share/textfile.txt

Workgroup

A workgroup is a group of computers on a local network that can access each other’s directories. Samba is perfect for managing workgroups (see later). 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, properly configured and running. Samba contains the SMB protocol, support for the Windows naming service (WINS) and support for joining Windows workgroups.

The following terminal commands will download and install Samba and its dependencies on the Raspberry Pi:

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

It is recommended to make a backup of the original Samba configuration file:

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

To edit and configure the Samba configuration file using Nano, the following terminal command can be used:

sudo nano /etc/samba/smb.conf

While inside the Samba configuration file, you’ll see that 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

where:

  • 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. It is also the hostname of the Raspberry Pi. The hostname of a Raspberry Pi can be obtained by using hostname terminal command.

At the bottom of the samba configuration 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

where:

  • [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 Raspberry Pi
  • comment=Raspberry Pi Share Directory is the description of the share-directory
  • path=/home/pi/share is the absolute path to the shared directory on the Raspberry Pi
  • 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 11 lines will enable everyone in the workgroup to create directories and files within the shared directory. It will also set the authors of these files and directories to pi.

After everything has been added and changed, the Samba config file can be saved by pressing the Ctrl + x , and then the y and Enter keys on the keyboard.

For the settings to take effect, the Raspberry Pi needs to be rebooted:

sudo reboot

Testing, start and/or stop Samba

After the Raspberry Pi has been rebooted, Samba can be tested, started, stopped and added/removed from update-rc.d by using the following terminal commands:

To see what Samba is doing use:

service smbd status

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

sudo service smbd start

or to stop Samba use:

sudo service smbd stop

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

sudo c -f smbd remove

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

sudo update-rc.d smbd defaults

To view the Samba shares for a user, use:

sudo smbstatus

Connecting to the shared directory

Once the Samba service has started, the shared directory can be accessed from other devices on the same local area network.

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 the Windows files explorer 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 Raspberry Pi’s shares on the local area network can be obtained from another Linux device by using the smbclient terminal command:

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

To list the shares, smbclient will prompt for the connecting devices’ password. 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.

From a Linux CLI/terminal, the shared-directory can be accessed by mounting it locally first using CIFS utilities. CIFS utilities can be installed using the following terminal command:

sudo apt-get install cifs-utils

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

hostname -I

The cifs mount command is:

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

where 192.168.1.xx is the IP address of the sharing Raspberry Pi, sharename is the share name that was given in the Samba configuration (e.g. PiShare) and /mnt/share is the mounting directory on the connecting Linux device. You can choose your own mounting directory. Note that there should nothing after password=.

To mount the share automatically each time the Linux-device boots up, the cifs mount command can be added to Crontab or the rc.local file.

The share can be unmounted from the connecting Linux device bu using the umount terminal command:

sudo umount /mnt/share

Potential errors

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.

Conclusion

Here we showed how to share a Raspberry Pi directory on a local area network. By sharing directories on a Raspberry Pi, many options and possibilities can be added to networked projects.

Samba is perfect for sharing directories from a Raspberry Pi and if configured correctly, can give access to Windows and other Linux devices.

One comment

Maqsood Alam

ERROR:
[PiShare]
comment=Raspberry Pi Share Directory
path=/home/pi/share
.
.
CORRECTION:
path=/home/PiShare ;should by same as mentioned above in square brackets.

Leave a Reply

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