Code Done Right!

Samba server (NAS)

www.samba.org

Samba server allows you to set up a NAS – Network Attached Storage. This is basically a hard drive (or drives) on your local network that can be accessed by any device connected to your router. You can use it as any other drive or folder on your PC. You can even set up your torrent client to download files there directly! Maybe you swapped HDD in your laptop for an SSD and had no idea what to do with the drive? Set up a samba share on it and give it a second life. Why throw it away if it can be reused?

Unless specified directly, all commands should be executed on your Raspberry server.

Installing Samba

Installing Samba server is a pretty easy task, just run the following command

sudo apt install samba

As usual, a lot of dependancies will be installed as well, not only the samba package.

When asked Modify smb.conf to use WINS settings from DHCP? just say No.

Opening ports

If you have UFW installed, which you should if your Raspberry is open to the internet, you need to open the following ports on your Pi in order to access your resources

  • 139
  • 445

Check back here for a refresher on blocking and opening ports on your server.

Preparing Samba share folder

In order to share anything, you need to have a folder for files. You can create your share folder anywhere in your system. Root forder / or /var/ are a good place to make a directory for sharing. I have chosen the root folder for my shares. Run the following commands

sudo mkdir /samba
sudo chmod 777 -R /samba

You can reuse an existing folder as well, but bear in mind that you will need to chmod it to 777 in order to be able to read and write using other devices.

Mounting an external disk

If you want to utilize an external drive you can do so as well. This actually should be the main reason for setting up a Samba share. Remember that some disks require external power supply. The easiest way is to use a 2.5″ disk in a case. With a proper one you should not need any additional power, be it for an SSD or HDD. Unfortunately you have to test it yourself.

Check out Mounting external drives tutorial to learn how to mount a drive. Substitute the default folder in tutorial with /sabma folder used above, or any other that you have chosen

Modify Samba config file

You will find the config in the following location

/etc/samba/smb.conf

At the very bottom add the following code

[RaspberryPiSamba]
Comment = Your Samba share
Path = /samba
Browseable = yes
Writeable = Yes
only guest = no
create mask = 0777
directory mask = 0777
Public = yes
Guest ok = yes

The above code will add a share named RaspberryPiSamba, modify Path variable in accordance with the place where you mounted your external disk in the above step.

If you want to add another share just add another block like the above, modifying the name in square brackets (it has to be a unique entry) and the path to your share.

CAUTION! The name in brackets will be used as part of the path to your files. It is best to use lowercase letters, numbers and underscores only.

CAUTION! The name in brackets is the share_name variable used later in the tutorial.

Rebooting Samba server

After making any changes you need to reboot Samba, use the following code

sudo service smbd restart

Your share should be ready and possible to access via local network.

Enabling shared file discovery on Windows

In order to find your Samba share on your Windows 10 you need to enable SMB/CIFS protocol.

Navigate to Windows Settings, click on Applications and functions, now find Programs and features in the upper right corner. A new window will be opened, you might know it as Add and remove software from older Windows versions. Left hand side contains the following entry Turn Windows Features On or Off, click on it and another window will open.

Look for SMB 1.0/CIFS File Sharing Support, if you have a sumbenu with different components then it is enough to enable client only. Tick the box, click on OK and reboot your PC.

Finding your shared folder

The last step is finding your share. Open This PC, and navigate to Network. Or just open Network if present on your desktop. After a brief moment you should be able to find your Raspberry server there. It might require a folder refresh or two.

If you are unsure about the name of your device run the following commands on your Raspberry

hostname
hostname -s

It will most likely be visible under one of the names produced by the above command.

Mounting your share permanently in Windows

It is also possible to mount the share, so it is accessible just like any other disk or partition on your system. In order to do so

  • Right-click on This PC icon
  • Select Map network drive…
  • Select drive letter you wish to use for your share
  • Click on Browse button to navigate to your share on your local network
  • Tick Reconnect at sign-in box
  • Click on Finish

Caution! Windows 10 will most likely prompt you for credentials, even if your server allows connecting as guest. In such a case put guest as username and leave password box empty. It should connect without an issue and you will not be prompted for credentials anymore.

Now you should be able to see your share mounted every time you open Windows! Bear in mind that every PC user will have to mount the share if they wish to use it. This is, however, a one-time only procedure.

Windows is not showing network devices?

Sometimes it happens that even properly configured Windows does not show devices on local network. In case you have nothing in your local network, or just your PC, you can add the share by providing a direct path.

Instead of clicking on Browse just supply Windows with the path following this example

\\local.ip.of.device\share_name

You can test the path in Windows Explorer to make sure it is correct or even use an internet browser. In browser it will show up as download only page, but if you see your files there then you know that the path is correct. If you have a website running on the server it may happen that your browser will change your local IP address to your domain and you will not be able to check the path in such a way.

Accessing share with other devices or software

Your share can be accessed not only by your Windows PC. Your phone or tablet can use it as well but it will require some additional software. Browse Play Store to find something you like, search for samba client.

VLC on the other hand can utilize network shares without the need for any additional software. You can store movies or music on your network drive and open it in VLC directly. Just browse Local network instead of folders!

Using Samba share with Mac is possible as well, but since I do not own, nor do I plan to, a Mac there will be no guide for it.

Mounting samba on Linux

On distros like Ubuntu, your share should be discovered by file manager automatically. Just click on it and either log in as guest or submit credentials if your NAS is password protected.

If you want to mount your NAS, so that it is accessible from ~/ or some other place in the system, here’s what you need to do

First of all make sure that you have cifs-utils installed by running

apt list --installed | grep cifs-utils

If you do not have it installed run the following

sudo apt install cifs-utils

Now create a directory where you will mount your share. It can be anywhere in the system, but it is a good idea to create said folder in ~/ for easy access. Run

mkdir /home/$USER/nas

The above will create a directory in your Home folder named nas, pretty self-explanatory, but we will need a mount point for the NAS. Remember the path file as you will need to add it to fstab file

Now add the following entry in your fstab file, this configuration assumes that you set up your NAS to be accessed by everyone in your local network with read and write permissions, like in the example above. Substitute /mount_point with filepath to your nas folder, and $UID with your UID

//local.ip.of.device/share_name /mount_point cifs uid=$UID,guest,rw,users,nofail 0 0

Now test your config with the following command

sudo mount -a

This will mount everything that is in fstab file.

If you are asked for password and your NAS is not password protected make sure you put the correct $UID, which is your user ID.

First user usually has UID=1000, but you can check that with the command

echo $UID

CAUTION! Remember to add the share name at the end of the first part of the line you are adding in fstab! In the example config it is the text inside of square brackets “RaspberryPiSamba

Adding password to a samba share

If for some reason you want to add a password for samba shares use the following command

sudo smbpasswd -a $USER

Substitute $USER with a desired username for your shares. Afterwards you will be prompted to set up a password.

Now restart your samba service.

Samba user is different from your system user. Set up whatever you like. You can even reuse your system password and username if security is of no concern due to your server being open to local network only.

Conclusion

Creating a Samba share is quite easy and useful if you want to set up a backup for your files, or just want a remote place to store media. Remember that you can set up as many shares as you want. Mounting ten 1TB disks is quite possible, provided you have a USB hub and some additional power flowing to those disks.

Sharing users’ home folders and printers is also possible. Look around for a tutorial on that if you are interested. As I am not, there will be no guide for that here.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.