Raspberry Pi Home Server v2: Sharing Files with Samba

Note: This post is part of a series. Each post builds on the previous ones. If you are just trying to add one thing to an existing system that was not built following this series, then I cannot promise that these instructions will work for you, although they probably will. If you’ve started from something other than a non-NOOBS Raspbian image, then you’ll probably need to adjust for that.

Please refer to the series Introduction for a list of all the different posts in the series.

Self-Promotion: I have recorded this series as a screencast for Pluralsight:
(http://www.pluralsight.com/courses/raspberry-pi-home-server)
If you have a Pluralsight subscription, please consider watching it. Reading the instructions is one thing, but watching it done demystifies the whole process.

Thank you!


Now that the Raspberry Pi has the space to hold your stuff, it’s about time it started doing something useful around the house. Its first task will be to share files from the hard drive. In the previous post we added a hard drive with two partitions, one ext4 partition for the operating system, and one NTFS partition to hold data. If you decided to format your data partition using the ext4 filesystem, then you’ll need to keep that in mind as we proceed.

Prerequisites

  • A Raspberry Pi
  • A mounted, USB hard drive with space for the stuff you want to share
    Mine is the second hard drive partition, mounted at /mnt/data

Install Samba

The “Samba” package creates network file shares in a way that Mac, Windows, and other Linux computers on the network will understand. Install Samba as follows:

sudo apt-get install samba samba-common-bin

That’s all there is to the installation, but there’s a bit of configuration before there will be anything to see over the network.

Set up shares

In the previous version of this article, I used Webmin to do the heavy lifting of setting up the shares. In all the time I’ve been running my Raspberry Pi Home Server, I’ve found that this is the only thing I ever really used Webmin for, though. As a result, this version of the series will set up the shares manually, and the Webmin article has been moved to the end as an optional step.

To create a share, you first create a folder that you want to share, tell Samba to share it, and set up the permissions correctly so that users connecting to the share through Samba will be able to write to it.

You can get into all kinds of detail about who can and can’t write what and where, but I’ll leave that up to you to explore on your own. What I’ll be setting up is a simple public share to be used by the family members in my house. The permissions will be wide open on this share.

Create the folder to be shared

First, create a folder to be shared. I’ll call mine “public”, and put it in the root of the hard drive I mounted in a previous post.

mkdir /mnt/data/public

Take a look at the permissions for the newly-created folder

ls -l /mnt/data

You’ll see that the new folder belongs to the “root” user and group even though we created it without using “sudo”. This is because my filesystem is NTFS, and it doesn’t support Linux’s file system permission system. If you made your data partition ext4, then this is going to cause problems for the Samba user. Assign the folder’s ownership to the “nobody” user and the “nogroup” group.

sudo chown nobody:nogroup /mnt/data/public

Check out the result

ls -l /mnt/data

If your filesystem is NTFS, this will have had no effect. If your filesystem is ext4 though, you should see that the public folder is now owned by “nobody” and “nogroup”. Now let’s share this folder with the network.

Open the Samba configuration file using nano

sudo nano /etc/samba/smb.conf

This file is pretty well documented, and will explain most things that you need inline. We’ll be making a number of changes.

Enable WINS support

Windows Internet Name Service (WINS) is kind of like the Domain Name System (DNS) that turns web addresses into IP addresses when you use a browser. WINS does this inside of your own network so that instead of having to remember your server’s IP address, you can refer to it by name.

Scroll down in the file a little until you see the “Global Settings” section. If your home network is using a different workgroup name than “WORKGROUP”, you’ll want to change that here. If you don’t know what that means, then you probably haven’t changed your workgroup name, so you can just leave it alone.

Just below that is a commented-out line that says “wins support = no”. Uncomment this by removing the pound sign from the beginning of the line, and change “no” to “yes”.

Scroll down a little, and just before the Networking section, add a line that says

guest account = nobody

Scroll down to the bottom of the file, and add a new section that defines the new public share.

[public]
   path = /mnt/data/public
   public = yes
   writeable = yes

Close and save the file (Ctrl-X,Y), and then restart the Samba service

sudo service smbd restart

That should do it. You should now be able to see the public share from the other computers on your network. You should also be able to refer to your server by name now instead of by address, and that includes when connecting via RealVNC.

Give it a try now. Open a file manager, and navigate to “\\rphs\public”. You should be able to create and edit files in this share without any restrictions.

Disclaimer

This is a wide-open file share. Anyone on the local network can add, edit, or delete the files it contains. I’m more interested in having a place to put stuff than in controlling access to it. Controlling access to the drive contents is beyond the scope of this post, but may be a subject for a future post.

Make some folders

Create some file folders in the Public share to hold your stuff, and copy it in. Music, Pictures, Videos, anything you want. For this server, I’ve created the following folder structure.

  • Documents
  • Downloads
  • Music
  • Pictures
  • Videos

You don’t have to use the same structure as me, of course, but this is the one I’ll be using in future posts, so if you structure your differently, just remember to adjust any paths in future posts accordingly.

Load it up

When it comes to copying the files in, you can save some time, and skip the network bottleneck by moving the external hard drive to the computer that currently has the data. Because the data partition was formatted as NTFS in the previous post, when you plug it in to your Windows or Mac computer, you’ll just see a drive called “Data”, or whatever you named it.

Remember to shut down the Raspberry Pi before removing the hard drive, of course. Just put it back when you’re done copying files, and start up the Raspberry Pi as usual.

Wrapping up

You now have a public share that exposes the data partition on the hard drive attached to the Raspberry Pi Home Server. You can use this to store your media and documents on the network so that they are accessible to your other computers and devices.

You’ve finished another article, so it’s time to shut down the Pi, and take a backup of the SD card.

What’s next?

In the next post, we’ll get a media server up and running so that you can stream music and movies all over your house from the hard drive share.

Advertisement
This entry was posted in Computers and Internet, Home Server, Raspberry Pi and tagged , , , . Bookmark the permalink.

36 Responses to Raspberry Pi Home Server v2: Sharing Files with Samba

  1. Hi Mel,
    I seem to be having some issues here which is surprising with how simple the directions are. I’ve done the apt-get which seems to have gong smoothly. It created the /etc/samba/ directory & I’m able to edit the smb.conf but after all that I can’t restart samba. Using the command “sudo service smbd restart” I get the error “failed to load: no such file or directory.” I’ve tried to restart the pi & run the apt-get a second time but when I check /etc/init.d/ I don’t see the smbd directory. What am I missing?

    Thanks in advance,
    Christian

  2. renierjvr says:

    Hi there

    this is a very nice series especialy if your a PI noob like me. I have been succesfull all the way up to here .I did restart the service and all but on the pi when I browse to \\rphs\public…it just goes to “/” am I missing something ?

    • Mel Grubb says:

      Start by just navigating to \\rphs and see if there are any shares at all. Maybe the name came out wrong. If you’re saying that the share is there, but it’s showing you the wrong thing, then check the samba config for that share and make sure it’s pointing to the right place (e.g. /mnt/data/public)

      • renierjvr says:

        Thx…I decided to follow your webmin instalation and the share is there. I assumed I will be able to browse to the share from the pi. but turns out I can only see the share from my windows drive. No matter it is working. thats the important part. Super thx for the quick response.

  3. Hi Mel,
    I was able to setup samba successfully & can move files from my computer to pi but it seems the permissions on my /public folder is stopping me from moving the file around on my pi’s desktop. I keep getting “Error moving file: Permission denied.” The only workaround I’ve found is doing a sudo cp or sudo mv. How can I fix this?

    Thanks

    • Mel Grubb says:

      You’ll need to examine two things here. Who are you logged into the desktop as (I usually use the Pi user), and who owns the folder and/or files you’re trying to manipulate. If you use an NTFS filesystem on the public share, then Linux doesn’t really understand the security system, so everything stays public, pretty much. That’s one of the reasons I use ntfs on my external drive. It also makes the drive more portable. If you’ve used ext4, then you need to start worrying more about who owns the share, and the files within it, and things get a little more complicated. Another thing to consider is where the files came from. If they were copied in over the nestwork through the share, then they should all be public to begin with. If they were copied/moved in from elsewhwere on the Pi’s filesystem, then they would retain their original permissions (on an ext4 filesystem, that is). You can change the ownership or permissions using chown and chmod if they aren’t what you’re expecting.

  4. Ed says:

    Hi Mel,
    I’m really thankfull for your guide, I’m a Linux and Raspberry Pi newbie and was having quite a trouble trying to set up samba between two Pis at home and finally it worked! I was able to see the shared folder from one Pi on the other. But I had a problem after I shutted them off. When I turned them on again, I couldn’t see the shared folder on the client Pi anymore. All that is shown is an error message “Message did not receive a reply. The location is not mounted.”. Do you know what might have happened?

    • Ed says:

      PS: My windows notebook can still see the shared folder, just the other Pi that doesn’t see it.

    • Mel Grubb says:

      From the message, it sounds like maybe you were trying to mount the network share on the client. Maybe the share wasn’t ready yet when the client started up. I’ve never tried that myself. If that’s not what you were doing, then I’m probably off base. Make sure the server is fully up and running, and then try starting the client and see if it makes a difference.

  5. mpskierbg says:

    Hello. Your guide has been really insightful, I was wondering about security of the server. I know you wrote controlling access is outside the scope of this article, but I was wondering if you had any recommendations on where to look? Also does your guide include ways to access the server from outside the local network?

    • Mel Grubb says:

      I use my server as an inside-the-house thing. I don’t expose anything to the outside other than the VPN and the remote access that the built-in VNC server provides. If I need to see something on the home network when I’m away, I either remote to the Pi’s desktop, or I connect the VPN and look around the network directly. If you were wanting to expose something like, say, a WordPress instance to the outside in order to run a blog from your house, then the security concerns have more to do with WordPress than with the Pi specifically. Similarly, if you wanted to run OwnCloud on the Pi, then you need to keep up to date on any OwnCloud updates or security alerts. Basically, as soon as you open a port to the outside world, you need to do the research on what’s listening. It’s not something I can give blanket advice on.

      • mpskierbg says:

        thanks I think remote into the pi is the best for my solution. How do you do that? is it just ssh? or can you use VNC outside the local network?

      • Mel Grubb says:

        The VNC server that’s built into current raspbian images can be used remotely once you sign up for a free account. SSH would require you to set up a VPN solution like OpenVPN and connect through that. The built in VNC is by far the easiest solution though.

  6. I can use Putty and VNC to get the the Pi from my Windows 10 Pro machine and arp /a shows the pi’s static address.

    But \\192.168.1.222\public doesn’t work. When I try to map the network drive and hit the Browse, the only machine on the network is the PC – not the Pi or its share.

    I followed the above and I got the workgroup name right (PERSICOWG).

    Is there anything in Windows10 with no support for smb 1.0 that could be causing this?

  7. Robert Sexton says:

    As far as I can tell, samba is broken. I have been tearing my hair out trying to get file sharing to work. I follow all the steps, the pi is visible on my Windows PC but I get either an “access denied” or “can’t find file” error message when I try to access the shared folder. I did finally realize that it is the filename in brackets rather than a declaration of public that is required. That was how I was bale to see the file at all. When I browse for this problem I get a lot of bad advice or multi-hundred page manuals full of arcane jargon that require a lifetime to absorb. Bottom line? Samba is broke*, Windows works. Why do we pursue Linux at all?
    *apparently the nobody:nogroup method is deprecated, pointing to yet another problem with Linux. It changes so fast that six-month old manuals are out of date.

    Any suggestions?

    • Robert Sexton says:

      As if that is not enough, I get a network login for which no combination of user name or password has any effect. Sometimes this does not occur but the only thing I can ascribe that to is the weather. Is there any good reason for all this?

    • Robert Sexton says:

      To add a little info, yes, I am trying to do this on Stretch. I finally found your comments on that which confirmed what i learned the hard way. At this point, it means my current project is dead in the water until proper fixes show up Who knows how long that will take. In any case, the Pi I am currently building can access my network and all the other computers on it (I have several) as well as my current Pi server which is pre-Jessie. But I cannot access it from other computers. When I try, I get a login window and no user/password combination I have ever used will work with it.

      I hope the fix(es) come soon. This is a good lesson in the difference between the Windows world and Linux. One can draw many conclusions here but that is something for a different forum.

      We will be waiting…

  8. Mel Grubb says:

    I’d have to build one from scratch again to be sure, but I have definitely built a working system with shares from a Stretch image. I build and rebuild servers more than the average user because of the blog and because of the Pluralsight course, which I’m in the middle of updating. As part of that, I started from a raw Stretch image, and I don’t recall having any problems with Samba.

    • Robert Sexton says:

      Well, you actually did have a problem that you solved by resorting to the distro upgrade, something I haven’t tried as yet. At this point, I am not sure what all the problems are but something is dratically wrong somewhere. I am not criticizing your server solution but I am becoming disenchanted with Linux. This update makes me all the more frustrated with it. I think it is in the nature of the beast, maybe too many cooks. I seem to have both a samba problem and a network access problem and it is hard to pin down. I will try the distro upgrade to see how far that gets me and I will let you know what I find. Thanks for the blog and all. I am still a fan…

  9. Mel Grubb says:

    dist-upgrade can be dangerous, and it has caused me problems in the past. Just make sure you take a backup of your card or HD partition before performing the upgrade.

  10. Robert Sexton says:

    As you suggested, I tried the dist-upgrade with no success. When invoked, it responded with no changes, suggesting that it is too soon to expect any revisions. I then decided to fall back on a previous version as I have images I saved based on Jessie. My server has been chugging away for some time with no problems on that platform. No reason it shouldn’t work again. After burning the SD card, I decided that updates were in order, but this resulted in a whole slew of failures that ultimately stopped the process and locked up the pi.

    On rebooting, I attempted to recover from this but had to do several dpkg repair steps first. Ultimately, that resolved the update problem so I then tried samba but was now getting server file not found when I attempted a restart, thus another round of update install, etc. Ultimately, I was able to get the pi back in order and make samba work. All this leaves me with the feeling that something fell apart in the samba camp and that may be the source of the problems. That, however, is well beyond my pay grade and I will leave it to the appropriate responsible adults to resolve.

    Bottom line is that Stretch is still an unknown but one must either not update at all, or regularly. The year long gap between my Jessie image and current files seems to have been a bit too much. I will be interested in knowing what ultimately shows up. Thanks, Mel, this has been quite educational.

  11. Alex Kuriatnyk says:

    Hi Mel,
    Have you heard of anyone trying to log on with their network credentials and try to map to the shared drive but not gain access? it tells me my credentials are not correct and I don’t have permission to access the drive. I tried changing my password on the pi but still no luck. I am quite certain I am using the correct user name and password. Is there something going on with Samba that is not allowing me to share my files? I actually see the drive on the network tree but it just won’t allow me to access it.

    • Mel Grubb says:

      I’ve really only set up public shares since it’s my internal network, and kinda the whole point of the thing for me. Are you trying to set up a public share or a private one that’s only accessible by the pi user?

      • Alex says:

        I am trying to set-up a public share like you did. I found a you-tube video and it’s about 6 months old and shows setting a Samba password even if you are using the same password to access the pi. Is this possibly something new that Samba is requiring for security purposes? I have not been able to try it yet, but I will when I get home in the next couple of days and let you know. Like I said, when you try and access the shared folder from my internal network, Samba requires your username and password and for some reason Samba won’t recognize the password. Thanks Alex

  12. rdsexton41 says:

    Are there any plans to update to an RPi with the newest Debian, Buster? I have been trying this and run into a brick wall with file sharing. Buster seems to have changed the way network access is handled and Samba does not quite correlate to what worked with Jessie. I suspect something is broken or missing but it seems too soon to find out. Incidentally, I have not updated my server to Buster and it runs right along with no problem at all. A Pi4 with Buster runs great, does much, but won’t play nice when it comes to network or file sharing. Any suggestions or insight will be greatly appreciated.

  13. Irlandos says:

    Anyone know how to edit the Samba config file to get it to support SMB2 as well as 1?
    My home (Linux) boxes can connect to my RPHS just fine but my (work) laptop with with Win10 throws an error and says SMB1 is unsafe and no longer supported 😦

  14. grbrken says:

    just to follow up, what worked for me was to make these changes to the [global] section of smb.conf:
    min protocol = NT1
    max protocol = SMB2
    then, because my work pc is pretty ‘locked down’ and does not allow unauthenticated access to samba shares, I set up a new user on the RPHS and then added this user to samba. Then at the end of smb.conf I added these lines.
    [library]
    writeable = yes
    comment = shared library folder
    valid users =
    public = no
    path = /mnt/data
    After this i could connect from both my Linux clients and my Win10 work laptop with no problem

  15. I replaced this step with OpenMediaVault, it’s also very awesome !

  16. For the people who have no access from Windows it’s because guest access was disabled by Microsoft. This registry key gives you back the option: reg add HKLM\SYSTEM\CurrentControlSet\Services\LanmanWorkstation\Parameters /v AllowInsecureGuestAuth /t reg_dword /d 00000001 /f

    Regarding the claim I made about OpenMediaVault, scratch that. It totally locked up the pi and I had to recover a backup. I guess the PI 2B cannot handle it pretty well (at least in my case).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s