Creating a file server (Samba)

Computer expert or enthusiast, this cookbook will help you use your Raspberry Pi to enhance your existing network. From sharing media across devices to deploying your own web portal, you’ll be amazed at what can be achieved.

(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:

Books to Consider

comments powered by Disqus