Build a Samba file server

By Rob Zwetsloot. Posted

Use Samba/CIFS to share a Raspberry Pi directory with other computers on a network

It’s easy to use a Raspberry Pi as a Samba file server where you can store backups and share files from all the other computers on your network.

Samba is the Linux implementation of the SMB/CIFS file-sharing standard used by Windows PCs and Apple computers, and widely supported by media streamers, games consoles, and mobile apps.

This article first appeared in The MagPi 80 and was written by Lucy Hattersley

With Samba activated, you can quickly copy files from a computer on your network to a Raspberry using wireless LAN (or a direct Ethernet connection).

This tutorial assumes that you’ll use a keyboard, mouse, and monitor to set up your file server; alternatively, you can enable SSH and connect to it remotely from another computer on your local network.

We also assume you’re using a 32GB (or smaller) microSD card, which provides a reasonable amount of storage space without requiring any extra steps to make it accessible. However, if you need extra storage, it’s possible to mount a large external USB drive and create a Samba entry for it.

Alternatively, if you want to keep things compact, you can install Raspbian on microSD cards of up to 256GB, although we think it’s wiser to install Samba on an external drive and use a smaller microSD to boot Raspbian and connect to it.

We suggest checking non-working SD cards online before you buy, to make sure you get one that’s fully compatible with the Raspberry Pi.

Once set up, you can mount your home file server on all the other computers on your network, and use it as a convenient place to store everything from music files you want to share with your housemates, to backups of important documents and save-game files you’d like to share between computers.

We recommend using a wired Ethernet connection for stability and fast transfer speeds. The project will still work if you connect your Raspberry Pi via WiFi, although performance will be affected, particularly when it comes to copying over large files.

Install Raspbian

Download the latest version of NOOBS (or the latest Raspbian Stretch with Desktop image file) and install it to a microSD card. See our Quickstart Guide if you’re unsure on how to set up a Raspberry Pi for the first time. Insert the microSD card into your Raspberry Pi and connect the keyboard, mouse, and HDMI cable. We suggest you connect an Ethernet cable between your Raspberry Pi and modem/router (otherwise use the wireless LAN icon to connect to a wireless network when booted up). Attach the power cable to boot up the Raspberry Pi.

Install Samba

Samba is available in Raspbian’s standard software repositories. We’re going to update our repository index, make sure our operating system is fully updated, and install Samba using APT. Open a Terminal and type:

sudo apt update
sudo apt upgrade
sudo apt install samba samba-common-bin

Answer Y when prompted.

Create shared directory

Now we’re going to create a dedicated shared directory in our home directory.

sudo mkdir /home/pi/shared

You can put it anywhere, but ours is in the home directory (it makes it easier to access without adjusting permissions). You’ll be able to access this directory from other computers on your network. Files you drop in here are accessible on other computers, and files you drop into the directory from other computers will appear on your Pi.

Configure Samba

Now we need to edit Samba’s configuration file to make the shared directory visible to other computers on the network. First, let’s make a backup just in case you want to look at the original.

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

Now let’s edit the Samba configuration file. Enter:

sudo nano /etc/samba/smb.conf

The smb.conf file is packed with detailed information on the Samba configuration (it’s a good idea to read through it all). Scroll to the end of the file and add the configuration code from the smb-pi.conf listing. Press CTRL+O to save the file, and CTRL+X to exit nano.

Looking at the configuration file

The Samba configuration file uses the same format as Windows .ini files, with sections surrounded with square brackets. The commands are case-insensitive and ignore white space. Let’s take a closer look at some of the lines in the file…

The location of the shared folder:

path = /home/pi/shared

Set this to ‘no’ to turn off the service:

available = yes

This is the list of users allowed to log in to the service:

valid users = pi

Whether this share is seen in the list of available shares in a net view and in the browse list:

browsable = yes

Ensures that users can write to files (the default is no):

writable = yes

Take a look at the Samba.org documentation for more information on all the configuration options available in smb.conf. Test out the Samba configuration file with testparm. Enter the following:

testparm

…and you should see a series of ‘Processing section’ tests. At the end it should say ‘Loaded services file OK’. If not, head back to the start of this step and check you have entered the additional setup text correctly.

Create a password

Before we start the server, you’ll want to set a Samba password. Enter:

sudo smbpasswd -a pi

Enter a simple and memorable password for testing (you can create a more secure one later by rerunning the command at any time).
Now restart your Samba server:

sudo service smbd restart

The Samba service will restart and the shared drive will now be available on the network.

Turn on SMB in Windows

Samba used to be installed by default in Windows, but it has recently become an optional installation.

In Windows 10, click on the Search bar and type ‘Control Panel’. Click on Control Panel in the search results. Now click ‘Programs’ and ‘Turn Windows features on or off’. Scroll down to find ‘SMB 1.0/CIFS File Sharing Support’ and click the ‘+’ expand icon to reveal its options. Place a check in the box marked ‘SMB 1.0/CIFS Client’. Click OK. This will enable Samba client support on your Windows 10 PC so it can access the Raspberry Pi.

Find your Pi on the network

You’ll now be able to find your Raspberry Pi file server (named RASPBERRYPI by default) from any device on your local network.

Click on the File Explorer icon in Windows and choose the Network option in the sidebar. You will see RASPBERRYPI in the list of devices. Click it and the ‘Enter network credentials’ window will appear. Enter your username pi and the password you created earlier. Click ‘Remember my credentials’ if you want to save the password, and then click OK.

The File Explorer will now display the shared directory. You can drag files and folders from Windows to the shared folder and they will be copied to your Raspberry Pi.

Find the Pi on a Mac

Samba works across many different devices. And it’s perfectly possible to access the shared folder from a Mac computer, or even an iOS or Android device (with the right app). Open a new Finder window on a Mac and choose Network in the sidebar. Click on ‘raspberrypi’ and ‘shared’ to access the directory. As with Windows, you can drag files directly to and from the Raspberry Pi.

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.