Installing & Configuring Samba Shares on a Raspberry Pi 3B+

by In Linux Find Truth
Installing & Configuring Samba Shares on a Raspberry Pi 3B+

Project:

Share files and folders between Win10 Pro and RaspbianOS on the Raspberry Pi (Rasbpi).

My objective in undertaking this project was to perform a basic configuration of Samba server on RaspbianOS which is essentially Debian 9 Stretch Linux to provide access to Samba shares via my main PC running MS Windows 10 Pro Version 1903.

This project assumes that you have a Rasbpi Model 3 or 3B+ (like I do) and that you’re running RaspbianOS on the Pi. Another assumption is that you have a user pi with standard access to the Debian 9 Linux distro and that you know how to elevate your privileges either as su or through the use of sudo at the Command Line in the Terminal.

For the purposes of this project, it was necessary for me to install both the Samba server and a Samba client on the Rasbpi. But, before we begin our installation of necessary packages, it is important that we ensure our system is up to date. To do this, run the following command in the Terminal:

sudo apt update && apt upgrade

Both of these commands can be run simultaneously with the && Boolean operator separating them because it is a compound command where && stands for AND. The significance of the use of && is that it tells the Linux system to attempt to perform the first part of this command and, if and only if the first part of the command runs successfully, then attempt to run the second half of the command. That is to say, if Linux is unable to update the system, then it will not attempt to upgrade it. This is different from the use of the || Boolean operator separating both parts of this compound command since this command would instruct Linux to perform the second part of the command regardless of whether the first part was performed successfully or not. This is not desirable when updating your Debian system since we want to ensure that we update the system prior to upgrading it.

After we have updated our RaspbianOS system successfully, it is time to turn our attention to installing our Samba server and client packages. To perform this, we can issue the following command in the Terminal:

sudo apt-get install samba && apt-get install smb-client

An alternative command we could run here is this command:

sudo apt-get install samba-common-bin

The latter command essentially performs the installation of the server and the client at the same time for Samba.

Here is the scenario and pre-configured requirements for this project:

Server and MS Windows client are located on the same network and no firewall is blocking any communication between the two MS Windows client can resolve samba server by hostname samba-server (if not, then the IP address will need to be substituted) MS Windows client's Workgroup domain is WORKGROUP.

At this point, you should have a running Samba server and client on your Linux system. You can verify this for the Server by running the System Control command:

sudo systemctl status smbd

If your server is running, you should see the following in the Terminal:

The results show that the Samba server daemon (smbd, which is the name that Linux recognizes as the server portion of Samba) is active (running) and the smbd.service is enabled. This last part is critical since if the Samba server was disabled we would need to enable it. Enabling the Samba server is important since we want the smbd.service to start automatically on boot up or on a reboot of the System. Otherwise, if we shutdown our Rasbpi or reboot it, then the Samba server would be stopped and we would have to restart it. Enabling the Samba server is easy to perform. You can do this by rerunning the systemctl command again like so:

sudo systemctl enable smbd

Now that we have our Samba server up and running, let’s turn our attention to configuring it for our purposes. The main Samba configuration file is, unsurprisingly, located at /etc/samba/smb.conf. This is not surprising since most all configuration files for packages in Linux are located under the /etc/ directory and typically in a subdirectory of the same or similar name to the package being installed. There are exceptions to this rule and you need to take note of this. When in doubt, you can always run the command:

sudo whereis <package name>

and this will return the location of the package usually in several places, /etc/ being one of them. Now, before we get into the /etc/samba/smb.conf file and look around, I always encourage users to make a copy of the file you’re intending to edit first just in case you make an error and the application won’t launch or, worse yet, the System won’t launch. To backup this file, perform the following:

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

If you do mess up the file, you can always restore the file from the .orig-extended file even if you need to perform this in emergency mode in Linux if the System fails to reboot. If you wish to scale down the smb.conf file so that it displays only relevant information removing many of the commented out lines to remove clutter in the file, you can optionally elect to run this command before editing the resulting /etc/samba/smb.conf file. The command is:

grep -v -E "^#|^;" /etc/samba/smb.conf.orig | grep . > /etc/samba/smb.conf

This will strip out the unnecessary content and write out the new content to the existing smb.conf file which you backed up earlier. This step is not mandatory, and if you feel uncomfortable running this command in the Terminal, it can be skipped.

So, now, your new smb.conf file will look similar to this and can be inspected using the cat command:

# cat /etc/samba/smb.conf


[global]
workgroup = WORKGROUP
dns proxy = no
log file = /var/log/samba/log.%m
max log size = 1000
syslog = 0
panic action = /usr/share/samba/panic-action %d
server role = standalone server
passdb backend = tdbsam
obey pam restrictions = yes
unix password sync = yes
passwd program = /usr/bin/passwd %u
passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuc-
cessfully* .
pam password change = yes
map to guest = bad user
usershare allow guests = yes

[homes]
comment = Home Directories
browseable = no
read only = yes
create mask = 0700
directory mask = 0700
valid users = %S
[printers]
comment = All Printers
browseable = no
path = /var/spool/samba
printable = yes
guest ok = no
read only = yes
create mask = 0700

[print$]
comment = Printer Drivers
path = /var/lib/samba/printers
browseable = yes
read only = yes
guest ok = no

I have highlighted the various stanzas in this file for clarity. Restart your Samba server using the command:

sudo systemctl restart smbd

and use your smb-client to confirm all the exported Samba groups:

# systemctl restart smbd
# smbclient -L localhost

Samba has its own user management system. However, any user existing on the samba user list must also exist within /etc/passwd file. For this reason, create a new user using useradd command before creating any new Samba user.

Once your new system user eg. linuxconfig exits, use smbpasswd command to create a new Samba user:

# smbpasswd -a linuxconfig
New SMB password:

By default all home directories are exported read-only and they are not browseable. To change this default configuration settings change the current homes share definition to:

[homes]
comment = Home Directories
browseable = yes
read only = no
create mask = 0700
directory mask = 0700
valid users = %S

To accomplish this, however, we will need to use a source code/text editor. Next, we need to select an editor that we’re going to use to edit the smb.conf file. My source/text editor of choice is vim, but a more popular editor which is easier for most Linux users to use is nano. So, we will use nano to edit the file. If nano is not installed, you’ll need to install it first by running this command:

sudo apt-get install nano

Most Linux distros, including RaspbianOS, have nano installed by default. To edit the smb.conf file, run this command in the Terminal:

sudo nano /etc/samba/smb.conf

This will open the file in the editor so we can inspect its contents and make necessary changes. Once you’ve made the changes to the file as shown above, you should save the changes by hitting the Ctrl + X keys, followed by Y to save the file under the same name and close the file.

It is important that each time you edit any configuration file--and smb.conf is no exception--you should restart the server. This can be accomplished here by running the command:

sudo systemctl restart /etc/samba/smb.conf or, simply
sudo systemctl restart smb.conf

if you’re already in the /etc/samba directory. Next, if you want to create a public samba share located under /var/samba for anonymous access allowing anyone to read and write data into the samba share, you can accomplish this by creating the samba directory, then changing its access permissions like this:

sudo mkdir /var/samba followed by
sudo chmod 777 /var/samba

Next, edit the /etc/samba/smb.conf file using nano to add the following stanza somewhere within the file:

[public]
comment = public anonymous access
path = /var/samba/
browsable =yes
create mask = 0660
directory mask = 0771
writable = yes
guest ok = yes

Make sure you save and close the file as described above and restart your samba server since we have made changes.

Now, it’s time to turn our attention to the MS Windows 10 Pro PC so we can verify the shares we have created on the Rasbpi in Win10 Pro. In your Win10 Pro PC, type the following into the Search field to the left of the Taskbar:

\\samba-server\

If all goes to plan, you should see the following in Win10 Pro:

Next, map any of the shared directories by right-clicking on the share and selecting a Map Network Drive... option. Tick, Connect using different credentials option and finish the network drive mapping by supplying the user credentials created in previous steps:

That’s all there is to it. If you want to list out all the samba users in the System, run the command:

sudo pdbedit -w -L

and, delete any samba users by running this command:

sudo pdbedit -x -u username

Now, you should be able to open and read the contents and write contents into any shared drive letter you specified when you mapped network drives to the samba shares on your Rasbpi system. If not, you’ll need to return to the /etc/samba/smb.conf file and verify your settings, change them, if necessary, and restart the samba server.

June 24, 2019
by In Linux Find Truth
Blog