Tutorials

Raspberry Pi NAS and DLNA UPnP Server

Overview

External hard drives can be used to store, movies, music, pictures and any other files that we would like to add to our digital libraries. Unfortunately a USB stick or a USB external hard drive can only be used by a single device at a time, providing the device accessing it has a USB port.

These days we need to access our media files from any device: phones, tablets, computers, smart TV's and in many cases different files from different devices at the same time.

There are several protocols that can be used to achieve these results. In this tutorial we will focus in how to setup a Raspberry Pi to run Samba and UPnP services to share our files and media using our home network.

You can buy a commercial NAS that will allow you to achieve the same results, but it would never be as fun and flexible as setting it up yourself. Also one of the advantages of sharing your files using a Raspberry Pi is that you can still connect your external hard drive straight to your computer, and providing both your hard drive and your computer have USB 3 ports, the file transfer will be a lot faster than transferring files over the network, this is specially useful when we need to transfer high definition movies which are usually several Gigabytes.

The parts you will need

  • A router with an available Ethernet port to connect your Raspberry Pi to and share its files to other devices connected to your home network. It would be great if it is a WiFi router so your wireless devices such as phones, tablets and even your TV (if it has a wireless connection) can connect to the router as well. Note: you can connect the Raspberry Pi to the router through the WiFi network, but I would not recommend it. The files that it might be sharing could be huge video files so you're better off connecting your Raspberry Pi straight to the wired Ethernet ports of your router so the file transfers are a lot faster.
  • An external 2.5" hard drive. Since the files you might end up storing in the external hard drive could be huge you would be better off getting a high capacity one of at least 1TB. Connecting an external hard drive to a Raspberry Pi can be as simple as connecting the drive to one of the USB ports of your Raspberry Pi. But if you happen to have a drive that uses a lot of power (over 1A) you can check this tutorial: Power Hungry USB Devices and the Raspberry Pi to be able to use your hard drive with the Raspberry Pi. The preferred filesystem for the hard drive should be NTFS (see below). It doesn't really matter if you get a FAT or a hfs+ (for Macs) filesystem hard drive, you can always reformat it to your preferred filesystem.
  • A Raspbery Pi with a USB Type A to Micro B cable, an Ethernet Cable and a Power Supply that should be able to supply 5V and at least 2A to be able to feed the Raspberry Pi and the external hard drive.
  • A micro SD card with Noobs, Raspbian or the Open Media Server pre-programmed micro SD card.
  • If you want to follow this tutorial, you will need a USB keyboard. Alternatively, if you get the Open Media Server micro SD card, all you need to do is connect all the pieces together and your NAS/UPnP server will be ready to go.
  • Optional: some self adhesive velcro to attach the Raspberry Pi to the hard drive.

You can buy most of the parts you need on this website except the external hard drive and the router. Chances are you already have both.

If you want to buy the parts as a kit, just select one of the following items:

Raspberry Pi Nas Kit Black (Open Media Server)

The perfect companion for your Open media Centre, allows you to store and share movies, music and other media files across devices at home, including your Open Media Centre, mobiles and tablets. With this kit you will be able to turn your external hard drive into a NAS / DLNA / UPnP server. Just connect all the pieces together and add an external hard drive. Follow the easy tutorial Raspberry Pi Nas and DLNA UPnP to learn how to put it together and howto organise your files. Kit includes: Raspberry Pi 2 Model B, Open Media Server Nas & UPnP 8GB micro SD card with Adapter, Raspberry Pi black enclosure, USB power adapter - 5V 2A, USB type A to micro B 1m cable and Network RJ45 1m cable. External hard drive is not included. External hard drive must be formatted in NTFS.

$89.30 In stock.
Raspberry Pi Nas Kit Frosted (Open Media Server)

The perfect companion for your Open media Centre, allows you to store and share movies, music and other media files across devices at home, including your Open Media Centre, mobiles and tablets.

Turn your external hard drive into a NAS / DLNA / UPnP server. Just put all the pieces together and add an external hard drive. Follow the easy tutorial Raspberry Pi Nas and DLNA UPnP to learn how to put it together and howto organise your files.

Kit includes: Raspberry Pi 2 Model B, Open Media Server Nas & UPnP 8GB micro SD card with Adapter, Raspberry Pi black enclosure, USB power adapter - 5V 2A, USB type A to micro B 1m cable and Network RJ45 1m cable. External hard drive is not included. External hard drive must be formatted in NTFS.

$89.30 In stock.

Assembling your NAS Kit

Go through the images to find out how to assemble your kit. You can click to enlarge images.

 

 

Insert the Raspberry Pi into its case
SD card contacts should be facing up
Push micro SD card until it clicks
Connect hard drive to Raspberry Pi USB port
Connect Ethernet cable to router
Connect micro USB cable from Raspberry Pi to power adapter

Server Name

The host name is openmediaserver. In the tutorial How to change your Raspberry Pi Hostname, we explain how to change the hostname of the Raspberry Pi in case you want to use a different name.

This server has Zeroconf (Bonjour) installed, so if you want to access it from any Bonjour compatible device, the address is openmediaserver.local.

If you want to learn a bit more about Bonjour, you can check the tutorial What is Bonjour?

Open Media Server settings

Default User Name and Password

The default user name for ssh and samba shares is:

  • User: pi
  • Password: raspberry

Samba Shares

There are only 2 samba shares by default:

  • \\openmediaserver.local\media
  • \\openmediaserver.local\pi

You are normally just going to be accessing the "media" share, as that is the share connected to the external hard drive. The "pi" share is just there in case you need to change any configuration files for the pi user.

Hard Drive File System

If you want to be able to to disconnect the drive from the Raspberry Pi and connect it to a Windows computer the hard drive should be formatted in NTFS. NTFS allows files of over 4GB and most devices can read NTFS formatted drives. Unfortunately Mac computers with OS-X can't officially write NTFS filesystems, can only read them. However there are hacks that would allow it a Mac computer with OS-X to write to hard drives with NTFS partitions. This is an unsupported feature though.  The other options you can have with a Mac are: format the external hard drive in FAT or just transfer files to the NAS through the network. If you format the drive in FAT, you can only have files smaller than 4GB. You will have to after the file /etc/fstab in the Open Media Server image if you want to use a FAT formatted drive.

Folder Structure

Organising media files is like organising drawers. We have drawers for sucks, underwear, T-Shirts and so on.

The Open Media Server image has a script that will write the following file structure into your external hard drive when the Raspberry Pi boots. Be careful not to connect several drives at the same time as the first drive the Raspberry Pi finds is the one that will be written with the this file structure. If you want to connect another drive, do it after the Raspberry Pi boots.

/media/HDD01/
           /media/
                /mixed
                /movies
                /music
                /pictures
                /tv_series

The folder names are pretty self explanatory. The mixed folder can be used if you want to store images and videos in the same folder.

Transferring files to your NAS

If you need to transfer files to your NAS, you have to connect to it from another computer.

Connecting from Windows

Open a Windows Explorer window from a computer that is on the same LAN as the Open Media Server Raspberry Pi.

Then in the address bar type:

\\openmediaserver.local\media

When prompted for the username and password, type in pi and raspberry

If that doesn't work, you'll have to find the IP address of the Raspberry Pi. The tutorial Network Setup for a Raspberry Pi Running Raspbian explains several ways of finding the IP address of the Raspberry Pi.

Once you find out what's the IP address of your pi, you can connect to it by typing in the address bar of Windows Explorer:

\\ip_address\media

When prompted for the username and password, type in pi and raspberry

Connecting from Mac

Open finder and either hit CMD+k or go to Go -> Connect to server, then type:

smb://openmediaserver.local/media

When prompted for the username and password, type in pi and raspberry

If that doesn't work, you'll have to find the IP address of the Raspberry Pi. The tutorial Network Setup for a Raspberry Pi Running Raspbian explains several ways of finding the IP address of the Raspberry Pi.

Once you find out what's the IP address of your pi, you can connect to it by typing in the address bar of Windows Explorer:

smb://ip_address/media

When prompted for the username and password, type in pi and raspberry

Naming your Media Files

It is important that you name your media files properly, as most media servers (including XBMC Kodi) follow the following convetions:

    Movies should be named as follows:
        [Movie_Name (Release_Year)]
        e.g.: " movies\Avatar (2009).mp4"
    TV shows should include season and episode numbers in the name:
        [Show Name - sXXeYY]
        e.g.: "Dexter s01e01.mp4"
    Each TV Show episode file should be stored in a set of folders as follows:
        tv_series\Show Name\Season\episodes
        e.g. tv_series\Dexter\Season 01\Dexter s01e01.mp4
        For TV Shows, the folder structure is important. Make sure show folders contain season folders that contain the actual episode files
    Music content should be stored in a set of folders as follows:
        \Music\Artist\Album\tracks
        e.g. music\U2\Joshua Tree\09 - One Tree Hill.mp3

For more information about naming conventions visit the XBMC Kodi - Naming Video Files page.

 

 

Accessing your Media though UPnP

There are millions of ways of accessing UPnP from different platforms:

From XBMC Kodi:

XBMC Kodi runs in almost any operating system. You might already have a Raspberry Pi media centre, like the one explained in the tutorial How to Build a Raspberry Pi Media Centre.

Just go to Videos -> Files -> Add Videos

Click on Browse and select UPnP devices. Open Media Server should appear there automatically if the device running XBMC Kodi is on the same network or LAN as the Open Media Server.

From Android:

You can have a look to a list of UPnP list of clients for Android. One of my favorites is Bubble UPnP.

From iOS:

You can have a look to a list of UPnP list of clients for iOS.

From your TV:

If you have a smart TV is very likely it can browse UPnP devices. Just look for an App called Media Server, or UPnP Media Server or something similar.

If you don't have a smart TV, you can convert it into a smart TV with the tutorial How to Build a Raspberry Pi Media Centre.

Building the Raspberry Pi NAS SD Card Yourself

In case you don't want to use the Open Media Server SD card, you can build your own NAS SD card on top of Raspbian.

There are several ways you can install Raspbian:

  • You can buy a NOOBS micro SD card and select Raspbian as the operating system to install.
  • You can buy a micro SD card with Raspbian pre-installed.
  • You can create a Raspbian micro SD card from a blank SD card following this tutorial: Writing an Image into an SD Card.

Once your Raspberry Pi boots with Raspbian, you have to do some configurations. The tutorial First Boot Configuration for a Raspbeery Pi Running Raspbian, explains how to do it.

Once Raspbian is installed and configured the first you should do to keep your system up to date is the following:

sudo apt-get update

Change the Hostname of the Raspberry Pi

The host name is OPENMEDIASERVER. In the tutorial How to change your Raspberry Pi Hostname, we explain how to change the hostname of the Raspberry Pi.

Install External Hard Drive

If you want to use less power and would like your NAS not to be so cluttered and full of cables it would be better to get a 2.5" USB hard drive, but you can use a 3.5" external hard drive if you need more storage.

We will be using a NTFS formatted hard drive for the reasons explained above, but you could use a ext4 formatted drive if you wanted to.

To mount this drive at boot time we added a couple of lines to the /etc/fstab file, this is the way it looks:

proc            /proc           proc    defaults          0       0
/dev/mmcblk0p1  /boot           vfat    defaults          0       2
/dev/mmcblk0p2  /               ext4    defaults,noatime  0       1
/dev/sda1 /media/HDD01 ntfs-3g uid=1000,gid=1000,umask=000 0 0
# a swapfile is not a swap partition, so no using swapon|off from here on, use  dphys-swapfile swap[on|off]  for that

Make sure the folder /media/HDD01 exists, if it doesn't just create it:

sudo mkdir /media/HDD01

The line that starts with /dev/sda1 is the one that mounts the external hard drive. It assigns the user id 1000 and the group id 1000 which are the default user id and group id for the user pi in Raspbian.

Once the hard drive is mounted, you can create the folder structure we need for this project. To do so create a file called /usr/local/bin/check_folders_hdd01 and add the following content:

#!/bin/bash
FS="/media/HDD01"
if cat /proc/mounts | grep -F " $FS " > /dev/null; then
        if [[ ! -e $FS/media ]]; then
            mkdir $FS/media
        fi

        if [[ ! -e $FS/media/movies ]]; then
            mkdir $FS/media/movies
        fi

        if [[ ! -e $FS/media/tv_series ]]; then
            mkdir $FS/media/tv_series
        fi

        if [[ ! -e $FS/media/music ]]; then
            mkdir $FS/media/music
        fi

        if [[ ! -e $FS/media/pictures ]]; then
            mkdir $FS/media/pictures
        fi

        if [[ ! -e $FS/media/mixed ]]; then
            mkdir $FS/media/mixed
        fi

        if [[ ! -e $FS/media/torrents ]]; then
            mkdir $FS/media/torrents
        fi

        if [[ ! -e $FS/system ]]; then
            mkdir $FS/system
        fi

        if [[ ! -e $FS/system/minidlna_db ]]; then
            mkdir $FS/system/minidlna_db
        fi
fi

Then make the script executable:

sudo chmod 755 /usr/local/bin/check_folders_hdd01

And finally run it to create the folders we need in the external hard drive.

sudo /usr/local/bin/check_folders_hdd01

Installing MiniDLNA (UPnP)

The first service we are going to install is MiniDLNA.

MiniDLNA is a fully complient uPnP (Universal Plug and Play) digital media server, which allows you to stream video and audio, and to access pictures, from your  Raspberry Pi NAS server over your network from any UPnP compliant player. There are a few links above on this page for UPnP for different operating systems.

Installing MiniDLNA:

sudo apt-get install minidlna

Open /etc/minidlna.conf in your favorite text editor, as superuser:

sudo nano /etc/minidlna.conf

Tell MiniDLNA where to look for your media, following the directory names explained above, these are the folders we are using in our server. You can change this to suit your needs:

media_dir=V,/media/HDD01/media/movies
media_dir=V,/media/HDD01/media/tv_series
media_dir=A,/media/HDD01/media/music
media_dir=P,/media/HDD01/media/pictures
media_dir=PVA,/media/HDD01/media/mixed

Set the name for our media server that will show up on clients:

friendly_name=Open Media Server

You can also specify the directory where you want MiniDLNA to store its database and album art cache:

db_dir=/media/HDD01/system/minidlna_db

Changing the inotify kernel parameter

Whenever you add files to your library, it should ideally rescan itself. Some people have created solutions setting up a cron job to rebuild the whole of the database at some specific times but hardly any of them address the fix the real issue: inotify.

Edit sysctl.conf (A file for defining kernel parameters)

sudo nano /etc/sysctl.conf

Find and change the kernel parameter fs.inotify.max_user_watches as explained below. If the parameter doesn't exist in the /etc/sysctl.conf file, add it at the bottom of the file:

fs.inotify.max_user_watches=100000

Save the file Ctrl + X and Y(es)

Now run the following command, you should be able to see the values you have set:

sudo /sbin/sysctl -p

Make sure you have inotify enabled and a notify interval is set as per your requirement in /etc/minidlna.conf

inotify=yes
notify_interval=300

To ensure that the minidlna user has the privilege to read from the directories under the pi user, add minidlna to the pi user group with:

sudo usermod -a -G pi minidlna

Finally restart MiniDLNA service and things should be working:

sudo service minidlna stop
sudo service minidlna force-reload
sudo service minidlna start

Get test Media

Download a video to make sure it works:

cd /media/HDD01/media/movies
wget archive.org/download/Debian6.0.4Lxde/Debian6.0.4LXDEPresentation.mp4
cd /media/HDD01/media/music
wget archive.org/download/Free_20s_Jazz_Collection/2to2_64kb.mp3

Restart the MiniDLNA service:

sudo service minidlna restart

Now you should be able to connect to your server from a UPnP client on the same network as your Raspberry Pi, and you should be able to stream the test video and music.

Installing Samba

Samba is an open source network protocol that lets you serve files to Microsoft Windows clients, and to any other operating system that supports this protocol (which is most of them).

Installing Samba Server:

sudo apt-get install samba samba-common-bin

Open /etc/samba/smb.conf in your favorite text editor, as superuser:

sudo nano /etc/samba/smb.conf

Define which folders you want to share and what kind of access they will have, this is the smb.con for the Open Media Server:

[global]
    workgroup = WORKGROUP
    realm = openmediaserver
    netbios name = OPENMEDIASERVER
    #usershare allow guests = yes
    #security=share
    security=user
    follow symlinks = yes
    wide links = no
    unix extensions = no
    lock directory = /var/cache/samba
    name resolve order = lmhosts host wins bcast
    domain master = yes
    local master = yes
    os level = 255
    preferred master = yes
[pi]
    browsable = yes
    read only = no
    #guest ok = yes
    valid users = pi
    path = /home/pi
    #force user = pi (no longer needed)
[media]
    browsable = yes
    read only = no
    #guest ok = yes
    valid users = pi
    path = /media/HDD01/media
    #force user = root

Restart the Samba service:

sudo service samba restart

Create a Samba user/password for accessing the private shares:

sudo smbpasswd -a pi

You should be prompted to enter a new password, this is the password you will use to log into the private share from another computer on your network:

sudo smbpasswd -a pi
New SMB password:
Retype new SMB password:
Added user pi.

To be consistent, use the password raspberry.

Now you should be able to connect to your server from any client computer on the same network as your Raspberry Pi and access files in the public share. The private share will require your username and password to access.

If you need more information, you can check the following guide: elinux.org/R-Pi_NAS

Comments

Pepe, 09-01-16 18:55
Congratulations!!! Best tutorial. Really!!!
Brendan, 14-02-16 09:49
Has anyone done this with the old R Pi B? Does it need the new R Pi 2 B?
Edgar, 15-02-16 16:41
Hi Brendan, yes you can do it with any device that allows you to connect a hard drive and has a network connection (ethernet or wi-fi). If you want to follow this tutorial the easiest thing is to install a debian based distribution, but other distributions will allow you to do it as well.
Just install Raspbian in the old Raspberry Pi and follow the tutorial.
Brendan, 19-02-16 19:23
These instructions suggest adding `/dev/sda1 /media/HDD01 ntfs-3g uid=1000,gid=1000,umask=000 0 0` to `/etc/fstab` to mount your external hard disk on startup.

However this didn't work for me (brand new RPi 2 B, one-year-old 1TB Seagate external HD). I had to replace ntfs-3g with just ntfs to make it work, so `/dev/sda1 /media/HDD01 ntfs uid=1000,gid=1000,umask=000 0 0`.

Hope that helps someone.
Edgar, 21-02-16 08:48
I double checked and ntfs-3g still works for me.
I guess it depends on the distribution and version of the software that you're using.
You can find which distribution and version you have with this command:
cat /etc/*-release
This is the result I got from mine:
PRETTY_NAME="Raspbian GNU/Linux 7 (wheezy)"
NAME="Raspbian GNU/Linux"
VERSION_ID="7"
VERSION="7 (wheezy)"
ID=raspbian
ID_LIKE=debian
ANSI_COLOR="1;31"
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
Add comment

* - required field

*




CAPTCHA image for SPAM prevention If you can't read the word, click here.

*
*
Continue Shoppping View Cart
Continue Shoppping View Wishlist