Central data storage for your home with a file server or a so-called NAS (Network Attached Storage) is a real classic and currently the number one use case for most. Not only does having a file server make access and sharing much easier, but it also makes it much easier to perform backups, avoid redundancies, and to protect important files. The data can be centrally stored on the server and everyone with access can easily access the files. This article covers how to create file server functionality with Samba (SMB) on Linux Ubuntu Raspberry Pi 4.

Using Samba you can use your Raspberry Pi 4 based home server as a file server and also provide print server functionality.

What is Samba (SMB)?

Samba is a re-implementation of the SMB networking protocol and very popular to connect an SMB client and server to allow non-Windows operating systems, such as Linux operating systems, to interoperate with Windows. Samba provides file and print services for various Microsoft Windows clients and can integrate with a Microsoft Windows Server domain, either as a Domain Controller (DC) or as a domain member. As of version 4, it supports Active Directory and Microsoft Windows NT domains.

Install Samba on your Raspberry Pi 4 Ubuntu Home Server

Starting point is an installed Raspberry Pi 4 running with Ubuntu. The installation of a ready to install system is covered in our article Home Server Raspberry Pi 4 with Ubuntu Server Operating System.

As a file server we want to have our system providing lots of storage capacity. Therefore, we attach an external USB hard drive to the Raspberry Pi 4 server. Details how to do that are covered in our article Attach USB hard drive storage to your Raspberry Pi on Ubuntu.

Now, having the Ubuntu based Raspberry Pi 4 home server up and running and the USB hard drive attached, the Samba can be installed:

sudo apt update

sudo apt install samba

We can check if the installation was successful by running:

whereis samba

You will get the folders containing installed Samba components as a result, such as:

samba: /usr/sbin/samba /usr/lib/aarch64-linux-gnu/samba /etc/samba /usr/share/samba /usr/share/man/man7/samba.7.gz /usr/share/man/man8/samba.8.gz

Now we need to configure Samba.

Configure Samba 

Samba's configuration file is located at /etc/samba/smb.conf

As described in our article Attach USB hard drive storage to your Raspberry Pi on Ubuntu we have prepared folder /mnt/share to store the data. 

Based on this, we would like to create two Samba shares, one for media files and the other one for documents, i.e. we create two sub-folders accordingly:

sudo mkdir /mnt/share/media

sudo mkdir /mnt/share/documents

To add the new directories as shares, we edit the file by running:

sudo nano /etc/samba/smb.conf

Our Windows systems run in a workgroup named AIRIX, therefore we would like to make the Samba instance also part of this workgroup and change the entry in smb.conf accordingly:

# Change this to the workgroup/NT-domain name your Samba server will part of
   workgroup = AIRIX

At the bottom of the file, add the following lines:

For share /mnt/share/media:

# Define the Share for the AIRIX.NET Home Server
 comment = media share on AIRIX01
 path     = /mnt/share/media
 read only   = no
 browseable = yes
 guest ok   = no
   create mask = 0770
   force user  = media
   force group = media

For share /mnt/share/documents:

   comment     = document share on AIRIX01
   path        = /mnt/share/documents
   read only   = no
   browseable  = yes
   guest ok    = no
   create mask = 0770
force group = docs

Whenever you modify this file you should run the command "testparm" to check that you have not made any basic syntactic errors.

The media share /mnt/share/media we would like to make available for a general user we will name "media" and the documents share /mnt/share/documents we will create individual users (our family members) and grant access to them.

Create user media

sudo adduser media

and set an appropriate password.

When the system creates the user media, it does create a group media, too.

Create users for the document share

sudo adduser username

We will do this for each user we would like to create by replacing username accordingly.

Again, the system will create the user and a group with the same name.

Add a separate user group

To manage the access for the documents share, we create a group named docs (we have already assigned the group as default for the documents share by setting force group = docs, see above).

sudo groupadd docs

Add an existing group to an existing user

Now we add the group media (as we would like to grant access to media to each family user, too) and the newly created group docs to each family user:

sudo usermod -aG media username

sudo usermod -aG docs username

Show which groups a Linux user belongs to

To verify that everything has been created correctly, you may want to double check:

sudo groups <username>

does show all the groups the user <username> belongs too.

Another option is to list all the groups in the system with their assigned users:

sudo less /etc/group 

You can scroll through the list or end with "q".

Assign folder ownership

Assign ownership of folder /mnt/share/media to user media and group media:

sudo chown -R media:media /mnt/share/media

Assign ownership of folder /mnt/share/documents to group docs (and we leave the user unchanged):

sudo chown -R :docs /mnt/share/documents

Set passwords for Samba users

Since Samba doesn’t use the system account password, we need to set up a Samba password for our user account:

sudo smbpasswd -a username

Restart Samba service

Now that we have our new share configured, save it and restart Samba for it to take effect:

sudo service smbd restart

When everything worked fine, your Samba server on your Raspberry Pi home server is now up and running and properly configured.

Further Reading & Related Topics

Connect from Windows to your Samba SMB share

Home Server Raspberry Pi 4 with Ubuntu Server Operating System

Attach USB hard drive storage to your Raspberry Pi on Ubuntu