In order to complement last post about the NFS remote file system, in this post we will cover SAMBA. The main focus of this series is to document the possibilities of NextCloudPi, but it aims to be a general introduction to the technology.

Like NFS, SAMBA provides a remote file system that allows us to mount locally folders that are in reality in another computer.

SAMBA is a free and open source implementation of the SMB/CIFS (Server Message Block/Common Internet File System) by MicroSoft that was started in 1992.

SMB/CIFS is the protocol used in Windows networks to share files and printers, so SAMBA allows us to host files and printers that can be accessed from a Windows computer, as well as it allows a Linux computer to access files and printers shared in a Windows system.

Actually, it is so popular that Mac computers can also access SMB/CIFS remote filesystems, which makes SAMBA a good choice for “mixed” networks where we want to serve files to Linux, Windows, Mac computers… and even some Android applications are able to mount SMB/CIFS folders over the network.

Features

This is a highlight of its features

The SAMBA server is implemented in userspace.

It is supported by multiple platforms

Its traffic is not encrypted, although it is in the roadmap of features

It provides authenticated access.

It can also share printers, combined with the CUPS printing server.

While the beauty of NFS its it’s simplicity, low resources requirements and the fact that it is implemented in the kernel, SAMBA is way more flexible, featureful and more appropriate for many setups except the most simple ones.

We have more control over the setup, as we can define different permissions for different users and the access is authenticated via username/password. Public shares can also be defined.

While installing the nfs-kernel-server only takes up 300 KB of our space, the smbd server takes around 40 MB.

Installation

Just install the appropriate package for your distribution, normally just samba.

In the case of NextCloudPi, just update to the latest version with

sudo ncp-update

As usual, the generic installer can be used on any Debian based running server to install and configure through SSH, or on a Raspbian image through QEMU.

git clone https://github.com/nextcloud/nextcloudpi.git ./installer.sh samba.sh 192.168.0.128

Default configuration (NextCloudPi only)

In the specific case of NextCloudPi, we usually want to share the data folder on the local network, so select samba in

sudo nextcloudpi-config

DIR is the directory to share. The default will be /var/www/nextcloud/data/admin/files for user admin on a fresh installation. If you have moved the data folder to an external drive, then it might be more similar to the default /media/USBdrive/ncdata/admin/files. Note that this is the path for the files belonging to the user admin.

If you would like a different setup, read the next section.

Manual configuration

The main configuration file is located at /etc/samba/smb.conf. There are many resources online about the configuration of this file, but the comments inside it are pretty much self-explanatory.

A share is defined by a configuration block that can be as simple as this

[NextCloudPi] path = /media/USBdrive/mysharedfolder writeable = yes ; browseable = yes valid users = pi

If you want to be able to modify and delete files and folders from both Nextcloud and SAMBA mounts, you can add the following to the share

force group = www-data create mask = 0770 directory mask = 0771 force create mode = 0660 force directory mode = 0770

Users are controlled by smbpasswd, and smb.conf can be configured to synchronize their credentials with the Linux user credentials.

In order to add the user pi, type

sudo smbpasswd -a pi

Make sure that the shared folder can be accessed by allowed users through the regular system permissions system.

Usage

Once the share is in place, we can browse it from Windows, Mac, and such.

In order to browse shares in Linux, we make use of smbclient.

$ smbclient -L 192.168.0.128 -U pi Enter pi's password: Domain=[WORKGROUP] OS=[Windows 6.1] Server=[Samba 4.2.14-Debian] Sharename Type Comment --------- ---- ------- print$ Disk Printer Drivers NextCloudPi Disk IPC$ IPC IPC Service (Samba 4.2.14-Debian) Domain=[WORKGROUP] OS=[Windows 6.1] Server=[Samba 4.2.14-Debian] Server Comment --------- ------- RASPBERRYPI Samba 4.2.14-Debian Workgroup Master --------- ------- WORKGROUP

We can see the NextCloudPi share defined earlier. We can use smbclient to login to a terminal interface that allows us to browse, push and pull files.

However, this is not very comfortable to use. It is better to mount the remote share.

$ sudo mount -t cifs //192.168.0.128/NextCloudPi /mnt -o username=pi,password=raspberry

, or at boot from /etc/fstab

//192.168.0.128/NextCloudPi /mnt cifs username=pi,password=raspberry,rw,user 0 0

That is not ideal though, as the password will be shown in our history for the mount command, and fstab is visible to all users.

It is better to save our credentials in a read-only file and use the option credentials.

cat > ~/.smbfile <<EOF username=pi password=raspberry EOF chmod 600 ~/.smbfile

//192.168.0.128/NextCloudPi /mnt cifs credentials=/home/pi/.smbfile,rw,user 0 0

The folder in the server at location /media/USBdrive/mysharedfolder will now be available in /mnt with those access permissions of user pi on that server.

Code

#!/bin/bash # SAMBA server for Raspbian # Tested with 2017-03-02-raspbian-jessie-lite.img # # Copyleft 2017 by Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com> # GPL licensed (see end of file) * Use at your own risk! # # Usage: # # ./installer.sh samba.sh <IP> (<img>) # # See installer.sh instructions for details # More at: https://ownyourbits.com # DIR_=/media/USBdrive/ncdata/admin/files USER_=pi PWD_=raspberry DESCRIPTION="SMB/CIFS file server (for Mac/Linux/Windows)" install() { apt-get update apt-get install --no-install-recommends -y samba update-rc.d smbd disable # the directory needs to be recreated if we are using nc-ramlogs grep -q mkdir /etc/init.d/smbd || sed -i "/\<start)/amkdir -p /var/log/samba" /etc/init.d/smbd } configure() { # INFO ################################ whiptail --msgbox \ --backtitle "NextCloudPi configuration" \ --title "Instructions for external synchronization" \ "If we intend to modify the data folder through SAMBA, then we have to synchronize NextCloud to make it aware of the changes.

This can be done manually or automatically using 'nc-scan' and 'nc-scan-auto' from 'nextcloudpi-config'" \ 20 90 # CHECKS ################################ [ -d "$DIR_" ] || { echo -e "INFO: directory $DIR_ does not exist. Creating"; mkdir -p "$DIR_"; } [[ $( stat -fc%d / ) == $( stat -fc%d $DIR_ ) ]] && \ echo -e "INFO: mounting a in the SD card

If you want to use an external mount, make sure it is properly set up" # CONFIG ################################ sed -i '/\[NextCloudPi\]/,+5d' /etc/samba/smb.conf cat >> /etc/samba/smb.conf <<EOF [NextCloudPi] path = $DIR_ writeable = yes ; browseable = yes valid users = $USER_ EOF update-rc.d smbd defaults update-rc.d smbd enable service smbd start usermod -aG www-data $USER_ echo -e "$PWD_

$PWD_" | smbpasswd -s -a $USER_ sudo chmod g+w $DIR_ } cleanup() { apt-get autoremove -y apt-get clean rm /var/lib/apt/lists/* -r rm -f /home/pi/.bash_history systemctl disable ssh } # License # # This script is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This script is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this script; if not, write to the # Free Software Foundation, Inc., 59 Temple Place, Suite 330, # Boston, MA 02111-1307 USA

References

https://www.samba.org/samba/docs/using_samba/ch06.html

https://www.samba.org/samba/docs/man/manpages-3/smb.conf.5.html