Media Server using Raspberry Pi — with Samba and Kodi

Media server using Raspberry Pi, the idea is to create a media server so that devices on the same network can play movies, music, and other type of media from a central location.

Table of Contents

Media Server using Raspberry Pi — with Samba and Kodi
Media Server using Raspberry Pi — with Samba and Kodi


The goal is very simple, which is to create a fast and ready to stream across the home network easily. This reduces any hassle of loading of media files in every device. With the help of a media server, all the family members can enjoy their favorite shows at the same time while using a home server of their own.

This is something similar to a NAS (Network Attached Storage) [1] and we will use Kodi [2] which is an open-source media server software, a media player. On our Raspberry Pi 4, we will set up NAS by creating a Samba server [3]. We will make all our USB devices that are plugged into Raspberry Pi to mount in a common location. And share that location using the Samba server. Then installing Kodi on other devices of the home network and adding our samba server on the Raspberry Pi will do the work. This article will cover how to setup our Raspberry Pi for the job and also demonstrates how to play the media from the server on a Windows machine.

Media Server Using Raspberry Pi — Component List

  1. Raspberry Pi 4 Model B x1
  2. Micro SD card 16GB x1
  3. USB 3.0 cable x1
  4. Ethernet cable x1

Media Server Using Raspberry Pi — Implementation Process


We will first set up a headless Raspberry Pi [4], which means without any monitor using an ethernet connection. And later we will enable Wi-Fi so the Raspberry Pi can connect to the home network automatically.

We will execute a script to watch for new USB device insertion and the script will mount the devices to a common location. For our case, we will create a directory named `smb-devices` and the script will mount a USB device to ‘smb-devices/storage0’. If a new device is added then the new devices will be mounted on ‘smb-devices/storage1’. The script will keep records of each mounted device. And once there’s at least one mounted device, the script will check the records and will automatically unmount an inactive mount-point. Because users will just plug and unplug their USB devices, pulling out the device without prior unmounting will make that device’s mount location busy. So, the script will if there’s any mount location busy but not actually used, it will free up that mount-point and mount any newly plugged device to that location.

More from the blog: Singleton Design Pattern Explained – A peep into OOP Singleton Design Pattern is a part of one of the core concepts of Object Oriented Programming, Design Patterns — must needed for successful project.

Implementation on the Raspberry Pi

Let’s see how to set up things in our Raspberry Pi,

Make sure you do not switch to other accounts; setup procedure must be done within the default ‘ubuntu’ user as our python script has absolute paths of different directories for the sake of consistency.

Getting the Raspberry Pi Ready
  1. At first please download the latest version of Ubuntu server LTS for Raspberry Pi. I am using Ubuntu 20.04.1 LTS.
  2. Then please download Raspberry Pi Imager and write the Ubuntu image to a microSD card (16GB recommended).
  3. After it’s done, please remove the microSD card reader from your computer and plug it again.
  4. Now once it’s ready, create an empty file named ‘ssh’ with no file extension. [5]
  5. Then eject the microSD card, insert it in your Raspberry Pi 4 and connect the Pi to your router using an ethernet cable.
  6. Now, please connect Raspberry Pi to a power source and it will boot up.
  7. Please go to your router settings from your web browser and check the IP address of your Raspberry Pi. [6]
  8. Now please open a command prompt or terminal depending on your OS and execute like this,
ssh ubuntu@<your_ip>
Example, ssh ubuntu@

(demonstrated on Windows 10),

Media Server using Raspberry Pi — ssh login on windows
SSH Login on Windows

Please note that you have to enter ‘yes’ for the first time, it will add the device to your ‘known_hosts’ and won’t ask for confirmation later.

Also, the default account in the ubuntu server is ubuntu and the password is also ‘ubuntu’. [7]

Or you can also use third-party tools like Putty,

Putty login step 1
Putty login step 2
Putty login step 3

After that you login with your password.

  1. Once you log in, you will be asked to change the default password, please change it to whatever you like.
  2. After you have changed your password, the server will automatically log you out. Please log in again.
More from the blog: GitHub Tutorial – Learn GitHub Basics New to software development? Read my GitHub tutorial and learn GitHub basics — keep track of your work from creating repositories to publishing your work!
Fixing Time on the Ubuntu Server
  1. Now before doing anything on the system, please check your time and fix if it doesn’t match with your current time. Execute ‘date’ to see the current time and date. If wrong, please execute,
sudo dpkg-reconfigure tzdata

And select your appropriate region. If it still doesn’t solve your issue, please execute,

sudo apt install ntp
sudo service ntp stop
sudo ntpd -gq 
sudo service ntp start

This will install `ntp` [8] and fix your time synchronize your time with the Internet. Incorrect time will cause a problem later.

If the time is wrong, your release file will be invalid, and you won’t be able to update your system.

Configuring User Account
  1. Then please edit the ‘sudoer` file so that executing sudo commands doesn’t ask for a password. This is crucial for the python script (will come later). Follow these steps,
  • ‘sudo visudo’ to open the file
  • add ‘$USER ALL=(ALL) NOPASSWD: ALL’ at the bottom of the file like this where $USER is your username.
  • Please save (ctrl + s) and exit (ctrl + x). Now whatever you execute with sudo, it won’t ask password for your user next time you execute command with ‘sudo’.
  1. Then please update and upgrade your system before we install and configure anything, please execute,
sudo apt update && sudo apt upgrade -y
Installing Required Python libraries
  1. Now, after the successful upgrade, most likely you will have ‘python3’ and ‘tmux’ installed. So, please just install pip (python3-pip) with,
sudo apt install python3-pip

You can verify whether you have a package installed or not with,

apt list --installed | grep '<package>'
Example, apt list --installed | grep 'tmux'

If you have the package installed you will see it appear on terminal. You can also check it with whereis tmux.

  1. After you have installed pip (pip3), which is a package manager for Python, please install ‘pyudev’,
sudo pip3 install pyudev
Configuring Samba Server
  1. Then please create a folder on your home directory named ‘smb-devices’.
  2. Now, please install samba with,
sudo apt install samba
  1. After that please open your samba configuration with,
sudo nano /etc/samba/smb.conf
  1. Now let’s add a new config, please add something similar to this at the end of the file,
    comment = <your_comment>
    path = /home/ubuntu/smb-devices
    read only = yes
    browsable = yes


    comment = SMB on RPi
    path = /home/ubuntu/smb-devices
    read only = yes
    browsable = yes
  1. Then please restart samba service with,
sudo service smbd restart
  1. Now, please let samba password for your user account, execute,
sudo smbpasswd -a ubuntu
Creating Initiating Script
  1. Then please create a script with `sudo nano` and paste the following contents,
tmux new-session -d -s usb_watcher '/usr/bin/python3.8 /home/ubuntu/'

Hosted at GitHub:

  1. After that, please execute the following command to add executable permission to the shell script,
sudo chmod +x
  1. Then please create a python script with ‘sudo nano’ and please copy the contents from
  2. Again, we will have to give this script executable permission, please execute the following command to do so,
sudo chmod +x
  1. Please verify again that both of your script has executable permission, ‘x’ on the very left represents executable permission for owner, group and others,
List home directory of user ‘ubuntu’
  1. Now you can run the script with the following command,
  1. This script will create a tmux session named ‘usb_watcher’, you will see something like this if you execute tmux ls,
usb_watcher: 1 windows (created Wed Sep 16 22:06:47 2020)

You can attach to this session using the following command (however this won’t be necessary),

tmux attach-session -t usb_watcher

Initially, you will see nothing there until you plug any USB. To detach the session or get into your normal terminal, please press ‘ctrl+b’ then press ‘d’.

More from the blog: What Is Technical Breadth? How To Increase Yours? Don’t just stick to one or two technology! Explore stuff you don’t know that exists, increase technical breadth.
Configuring Wi-Fi on Server
  1. Now, let’s set up Wi-Fi so Raspberry Pi can automatically connect to your home network. For that, please execute the following command to check the wireless network interface,
ls /sys/class/net

Your wireless interface will be something similar to ‘wlan0’

  1. After that, please find out your netplan, by executing ‘ls /etc/netplan/’, you will see something like ‘01-network-manager-all.yaml’ or ‘50-cloud-init.yaml’. [9]
  2. Now, please open your net configuration file like this,
sudo nano /etc/netplan/<your_config.yaml>
Example, sudo nano /etc/netplan/50-cloud-init.yaml
  1. Then, please enter your Wi-Fi credentials, like this bellow,
            dhcp4: true
            optional: true
    version: 2
            optional: true
                    password: "PASSWORD-HERE"
            dhcp4: true
  1. Now, please save and close that file with ‘ctrl + s’, followed by ‘ctrl + x’.
  2. After that, to make above changes to persistent on reboot, please create another file with,
sudo nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg

And write the following contents,

network: {config: disabled}
  1. Now please go to your router settings and bind your Raspberry Pi to a specific IP address [10]. So that, each time your Raspberry PI is connected to the network it gets the same IP address. This is very crucial for the later stages. And because the binding procedure can vary from router to router, please search on google how to do it for your system. Don’t forget to do it!
  1. You can now reboot your system.

That’s it! You are done setting up Raspberry Pi!

You will have to run the script (./ manually every time you start or restart your Raspberry Pi. As of now, it has to auto-start function implemented. So, just like we did a while ago, you will have to login to your account using SSH and initiate the script before inserting any USB.

Now, please plug a USB device to the Raspberry Pi and follow us to see how to configure our samba server on a client device and play media.

More from the blog: HSTS Preload — Using TLS / SSL Certificate Properly Learn how to enable HSTS Preload and tell the web browsers to load your site over HTTPS & HTTPS only, use SSL certificate properly!

Implementation on a Client Machine (Windows 10)

Before getting started, Let’s add some movies, plug your USB storage into Raspberry Pi and wait a while (15s). We have set a grace period to give the USB storage some time to get ready. The following steps are demonstrated on Windows 10,

Install Kodi
  1. At first, please install the latest version of Kodi.
  2. Then open Kodi, which will look something like this,
Media Server using Raspberry Pi — kodi home screen
Kodi Home Screen
Add Media Source on Kodi
  1. Then please click ‘Enter files section’ and select ‘Add Videos’, and from there click ‘Browse’.
  2. Now please scroll down till you see ‘Add network location’.
  3. After that, write your Raspberry Pi’s IP address in the ‘Server name’ field which you already know or can check from your router settings, ‘ubuntu’ in the username field, and the smbpasswd in the password field, the on that you set while setting up samba. It will look something like this bellow and be sure the Protocol is ‘Windows network (SMB)’,
Media Server using Raspberry Pi — add network location
Kodi – adding network location
  1. After that, please hit ‘OK’.
  2. Now, please select the network you just added from the list, you will see a directory appear, the directory name should be one you specified as the server name of samba. For our case, it’s ‘my-server’.
Kodi – adding server
  1. Then, after selecting your server, you will see storage 0. Please select that and press ‘OK’.
  2. Rename your storage to be saved as, as you like, and hit ‘OK’ again.
Media Server using Raspberry Pi — add storage
Kodi – adding storage
  1. Please specify what the directory contains, and hit ‘OK’ again.
  2. After that please confirm to refresh your library and your movies will be added!
  3. Now you can watch and enjoy your movies without having to load them in your system.
Adding New Storages

Please make sure to follow this process on each device you want to play media from the media server. You will only have to configure once.

If you plug any new storage device to Raspberry Pi, please click Movies from the main left bar menu and navigate to ‘Files’, then click ‘Add videos’, but this time instead of adding a new network location, select your preadded Raspberry Pi server like this bellow and select the new storage device.

Kodi – re add storage

You will just have to add whatever your storages appear as; you know the scripts will automatically mount them dynamically. For example, if you have two storage mounted one of your storage is mounted as storage0, and one of them gets unplugged and plugged again, then this might get mounted on a new location like storage2.  So, please do not just unplug your storages, if you do, be sure to re add them and remove the old obsolete source. Hence, unnecessary removal and adding of storage is depreciated.

Adding Additional Content on an Existing Storage

If you add additional content to your storage, navigate to the source and click ‘scan for new content’, this will add the new contents.

Kodi – scan for new content

Now it’s time to watch some movies, click whatever you like to play and enjoy!

Media Server Using Raspberry Pi — Implemented Code

  1., used to create ‘tmux’ session that fires up the python script ‘’ within.
tmux new-session -d -s usb_watcher '/usr/bin/python3.8 /home/ubuntu/'

Hosted at GitHub:

  1., watches for new USB devices and mount them in a common location. This script is smart enough to unmount any mount-point that is not used anymore (user unplugs a device).

Hosted at GitHub:


[1]Wikipedia, “Network-attached storage – Wikipedia,” [Online]. Available: [Accessed 17 September 2020].
[2]“Open Source Home Theater Software,” Kodi, [Online]. Available: [Accessed 17 September 2020].
[3]Canonical, “Install and Configure Samba | Ubuntu,” [Online]. Available: [Accessed 17 September 2020].
[4]Raspberry Pi Foundation UK, “Setting up a Raspberry Pi headless – Raspberry Pi Documentation,” [Online]. Available: [Accessed 17 September 2020].
[5]Raspberry Pi Foundation UK, “SSH (Secure Shell) – Raspberry Pi Documentation,” [Online]. Available: [Accessed 17 September 2020].
[6]Raspberry Pi Foundation UK, “IP Address – Raspberry Pi Documentation,” [Online]. Available: [Accessed 17 September 2020].
[7]Canonical, “ARM/Raspberry Pi – Ubuntu Wiki.,” [Online]. Available: [Accessed 17 September 2020].
[8]Wikipedia, “Network Time Protocol – Wikipedia.,” [Online]. Available: [Accessed 17 September 2020].
[9]Canonical, “Ubuntu Manpage: netplan – YAML network configuration abstraction for various backends.,” [Online]. Available: [Accessed 17 September 2020].
[10]NETGEAR, “How do I reserve an IP address on my NETGEAR router? | NETGEAR Support.,” [Online]. Available: [Accessed 17 September 2020].

Disclaimer: This content – Media Server using Raspberry Pi — with Samba and Kodi and everything used in this content are copyrighted materials of S M Mahmudul Hasan, please credit properly if you are using any material — image, paragraph etc.