Creating a file server (Samba)

Exclusive offer: get 50% off this eBook here
Raspberry Pi Networking Cookbook

Raspberry Pi Networking Cookbook — Save 50%

A practical collection of awesome Raspberry Pi recipes that help you learn about the Internet of Things with this book and ebook

$20.99    $10.50
by Rick Golden | April 2013 | Cookbooks

This article by Rick Golden, author of Raspberry Pi Networking Cookbook, shows how the Raspberry Pi can be configured to become a file server on the local network.

(For more resources related to this topic, see here.)

The Raspberry Pi with attached file storage functions well as a file server. Such a file server could be used as a central location for sharing files and documents, for storing backups of other computers, and for storing large media files such as photo, music, and video files. This recipe installs and configures samba and samba-common-bin. The Samba software distribution package, samba, contains a server for the SMB (CIFS) protocol used by Windows computers for setting up 'shared drives' or 'shared folders'. The samba-common-bin package contains a small collection of utilities for managing access to shared files.

The recipe includes setting a file-sharing password for the user pi and providing read/write access to the files in pi user's home directory. However, it does not set up a new file share or show how to share a USB disk. The next recipe shows how to do that.

After completing this recipe, other computers can exchange files with the default user pi.

Getting ready

The following are the ingredients:

  • A Raspberry Pi with a 5V power supply

  • An installed and configured "official" Raspbian Linux SD card

  • A network connection

  • A client PC connected to the same network as the Raspberry Pi

This recipe does not require the desktop GUI and could either be run from the text-based console or from within an LXTerminals.

If the Raspberry Pi's Secure Shell server is running and it has a network connection, this recipe can be remotely completed using a Secure Shell client.

How to do it...

The following are the steps for creating a file server:

  1. Log in to the Raspberry Pi either directly or remotely.

  2. Execute the following command:

    apt-get install samba samba-common-bin

    Download and install samba with the other packages that it depends on. This command needs to be run as a privileged user (use sudo).

    The package management application aptitude could be used as an alternative to the apt-get command utility for downloading and installing samba and samba-common-bin.

    In the preceding screenshot, the apt-get command is used to install the samba and samba-common-bin software distribution packages.

  3. Execute the following command:

    nano /etc/samba/smb.conf

    Edit the samba configuration file. The smb.conf file is protected and needs to be accessed as a privileged user (use sudo).

    The preceding screenshot starts the nano editor to edit the /etc/samba/smb.conf file.

  4. Change the security = user line. Uncomment the line (remove the hash, #, from the beginning of the line).

    The preceding screenshot of the Samba configuration file shows how to change samba security to use the Raspberry Pi's user accounts.

  5. Change the read only = yes line to be read only = no, as shown in the following screenshot:

    The preceding screenshot shows how to change the Samba configuration file to permit adding new files to user shares (read only = no).

  6. Save and exit the nano editor.

  7. Execute the following command:

    /etc/init.d/samba reload

    Tell the Samba server to reload its configuration file. This command is privileged (use sudo).

    In the preceding screenshot, the Samba server's configuration file is reloaded with the /etc/init.d/samba command.

  8. Execute the following command:

    smbpasswd –a pi

    This command needs to be run as a privileged user (use sudo). Enter the password (twice) that will be used for SMB (CIFS) file sharing.

    The preceding screenshot shows how to add an SMB password for the pi user.

  9. The Raspberry Pi is now accessible as a Windows share!

  10. From a Windows computer, use Map network drive to mount the Raspberry Pi as a network disk, as follows:

    The preceding screenshot starts mapping a network drive to the Raspberry Pi on Windows 7.

  11. Enter the UNC address \\raspberrypi\pi as the network folder. Choose an appropriate drive letter. The example uses the Z: drive. Select Connect using different credentials and click on Finish, as shown in the following screenshot:

    The preceding screenshot finishes mapping a network drive to the Raspberry Pi.

  12. Log in using the newly configured SMB (CIFS) password (from step 7).

    In the screenshot, a dialog box is displayed for logging in to the Raspberry Pi with the SMB (CIFS) username and password.

  13. The Raspberry Pi is now accessible as a Windows share!

    Only the home directory of the pi user is accessible at this point. The next recipe configures a USB disk for using it as a shared drive.

How it works...

This recipe begins by installing two software distribution packages – samba and samba-common-bin. This recipe uses the apt-get install command; however, the aptitude package management application could also be used to install software packages.

The Samba package contains an implementation of the Server Message Block (SMB) protocol (also known as the Common Internet File System, CIFS). The SMB protocol is used by Microsoft Windows computers for sharing files and printers.

The samba-common-bin package contains the smbpasswd command. This command is used to set up user passwords exclusively for using them with the SMB protocol.

After the packages are installed, the Samba configuration file /etc/samba/smb.conf is updated. The file is updated to turn on user security and to enable writing files to user home directories.

The smbpasswd command is used to add (-a) the pi user to the list of users authorized to share files with the Raspberry Pi using the SMB protocol. The passwords for file sharing are managed separately from the passwords used to log in to the Raspberry Pi either directly or remotely. The smbpasswd command is used to set the password for Samba file sharing.

After the password has been added for the pi user, the Raspberry Pi should be accessible from any machine on the local network that is configured for the SMB protocol.

The last steps of the recipe configure access to the Raspberry Pi from a Windows 7 PC using a mapped network drive. The UNC name for the file share, \\raspberrypi\pi, could also be used to access the share directly from Windows Explorer.

There's more...

This is a very simple configuration for sharing files. It enables file sharing for users with a login to the Raspberry Pi. However, it only permits the files in the user home directories to be shared. The next recipe describes how to add a new a file share.

In addition to the SMB protocol server, smbd, the Samba software distribution package also contains a NetBIOS name server, nmbd. The NetBIOS name server provides naming services to computers using the SMB protocol. The nmbd server broadcasts the configured name of the Raspberry Pi, raspberrypi, to other computers on the local network.

In addition to file sharing, a Samba server could also be used as a Primary Domain Controller (PDC) — a central network server that is used to provide logins and security for all computers on a LAN. More information on using the Samba package as a PDC can be found on the links given next.

See Also

Resources for Article :


Further resources on this subject:


Raspberry Pi Networking Cookbook A practical collection of awesome Raspberry Pi recipes that help you learn about the Internet of Things with this book and ebook
Published: March 2013
eBook Price: $20.99
Book Price: $34.99
See more
Select your format and quantity:

About the Author :


Rick Golden

Rick Golden sat in the computer lab at SUNY Fredonia and completed his first CAI tutorial for programming in APL. It was the summer of 1972; he was nine years old.

Most of the programming that he has done since then has been in Algol-based languages such as PL/I, FORTRAN, BASIC, Pascal, C, C++, C#, Objective C, and Java. He did occasionally write code in languages such as APL, FORTH, LISP, and Scheme; however, he could not find an employer that would actually pay him to develop solutions using those non-structured languages. In recent years he has had more success introducing organizations to scripting languages such as Python, Perl, TCL, Ruby, Groovy, and Node.js.

He also had the privilege to work in many different domains applying leading technologies through each cutting-edge wave of structured programming, architectural frameworks, and design patterns. He has championed distributed computing, scripting languages, SOA, browser applications, CMS, ESBs, web services, nosql and map-reduce, top-down structured approach, UML, use cases, XP - extreme programming, iterative development, and agile development. And, he is still moving forward.

Now, as he approaches his 40th year as a programmer, software architect, and product manager—a career that has spanned eighty percent of his life. He greatly enjoys guiding and coaching the next generation of programmers and software architects—awakening others to the same joy and passion for computing that he has had for the past 40 years.

Books From Packt


 Network Administration with FreeBSD 7
Network Administration with FreeBSD 7

Arch Linux Environment Setup How-to [Instant]
Arch Linux Environment Setup How-to [Instant]

Learning FreeNAS
Learning FreeNAS

CUPS Administrative Guide
CUPS Administrative Guide

 Raspberry Pi Home Automation with Arduino
Raspberry Pi Home Automation with Arduino

Raspberry Pi for Secret Agents
Raspberry Pi for Secret Agents

 Linux Mint System Administrator’s Beginner's Guide
Linux Mint System Administrator’s Beginner's Guide

Raspberry Pi Media Center
Raspberry Pi Media Center


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
e
h
T
j
Z
u
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software