Kodi server part 1: Installation and Configuration

This is the first part of a multi-part tutorial describing how to configure the "perfect" Kodi media centre running on top of ubuntu server.

Other parts of the tutorial may be found here:

This part of the tutorial deals with basic Kodi installation and network configuration for the server. It sets out some basic configuration to get the system to load Kodi straight away when it boots up, make the media database available to other Kodi clients on the network, enabling the power menu within Kodi's interface, and making sure automatic updates are enabled.

Prerequisites

Install ubuntu server 16.04 (later versions will probably work fine too, but I haven't tested them).

During the installation, choose the username kodi (or add an additional user called kodi afterwards).

Whether you create the user kodi during installation or afterwards, you need to add the user to a couple of groups to ensure kodi has access to audio and video:

sudo usermod -aG audio,video kodi

Configuring networking

To keep things simple, we will configure wifi or ethernet in /etc/network/interfaces instead of using networkmanager.

If your server is connected to the internet with an ethernet cable, open /etc/network/interfaces and add the following to the end of the file:

# ethernet connection automatically brought up when cable is plugged in
# with IP address retrieved from the router
allow-hotplug enp3s0
iface enp3s0 inet dhcp

My NUC has a built in WiFi card, and the signal is good enough to stream media files over the network, so I configured WiFi instead. If you have WiFi that is protected with a WPA passphrase, you need to generate a hash that will be used for authentication with the router. First, install the utility that will be used to generate the hash:

sudo apt-get update
sudo apt-get install wpasupplicant

Now generate the hash (substitute your router's SSID and passphrase):

wpa_passphrase SSID passphrase

Since this is sensitive information, the file should only be read by root. Make the file private:

sudo chmod 600 /etc/network/interfaces

Now add the following lines to the end of the file. Substitute your_SSID for your router's SSID, and copy the hash generated by wpa_passphrase into the wpa-psk field:

# WiFi connection that automatically connects when available
# with IP address retrieved from the router
auto wlp2s0
iface wlp2s0 inet dhcp
        wpa-ssid "your_SSID"
        wpa-psk hash_from_wpa_passphrase

Now reload networking:

sudo service networking restart

If you're having problems with this, give me a shout. Things could be different on your network if you have multiple wifi cards etc.

One more step: on your router's admin page, give your device a static IP address. This will be important later because this server and other instances of kodi will need the media database and media files to be in a consistent location on the network.

Installing Kodi

Kodi is included in the standard repository, but it's a relatively old version. To ensure we are running the latest version of Kodi, we will add the Kodi development team's PPA. The add-apt-repositories script is in the software-properties-common package, so we need to install that first:

sudo apt-get update
sudo apt-get install software-properties-common

Now add the PPA:

sudo add-apt-repository ppa:team-xbmc/ppa

And install Kodi:

sudo apt-get update
sudo apt-get install kodi

We also need a few dependencies for various purposes:

  • xorg and xserver-xorg-legacy provide the display server needed to run kodi
  • dbus-x11 provides dbus-launch (used to launch kodi)
  • alsa-utils for sound
sudo apt-get install xorg xserver-xorg-legacy dbus-x11 alsa-utils

Starting Kodi automatically

The following systemd unit file will launch kodi automatically. Create it using sudo nano /etc/systemd/system/kodi.service (ctrl + x to exit, save when prompted):

[Unit]
Description = Kodi Media Center

After = systemd-user-sessions.service sound.target

[Service]
User = kodi
Group = kodi
Type = simple
PAMName = login
ExecStart = /usr/bin/xinit /usr/bin/dbus-launch --exit-with-session /usr/bin/kodi-standalone -- :0 -nolisten tcp vt7
Restart = on-abort
RestartSec = 5

[Install]
WantedBy = multi-user.target

To make this "service" start automatically boot, we need to enable it:

sudo systemctl enable kodi

You should now find that when you boot up, kodi is launched automatically.

Allow kodi to power to suspend/reboot/shut down the system

Polkit is a tool that enables unprivileged users (i.e. non-root users) to carry out certain actions that affect the whole system, without needing "sudo". We will use this to let kodi suspend/reboot/shut down the system. Create a new rule file using sudo nano /etc/polkit-1/localauthority/50-local.d/kodi.pkla and paste the following information:

[kodi user]
Identity=unix-user:kodi
Action=org.freedesktop.login1.*
ResultAny=yes
ResultInactive=no
ResultActive=yes

Now reload the polkit daemon so the rule file is read:

sudo service polkitd reload

And then test that you can reboot, suspend, power off etc. the box using the power menu within Kodi.

Allowing auto updates to Kodi

I allow unattended upgrades on my box so that I don't have do as much manual administration.

If you didn't allow unattended upgrades during installation of the OS, you can install unattended upgrades like so:

sudo apt-get update
sudo apt-get install unattended-upgrades

If it's already installed, you can re-run the configuration script using:

sudo dpkg-reconfigure unattended-upgrades

This will create a config file at /etc/apt/apt.conf.d/20auto-upgrades with the contents:

APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";

By default, only packages in the ubuntu archive are auto updated, but since we installed Kodi from the developers' PPA we won't receive auto updates unless we make a change. To allow auto-updates to kodi, open /etc/apt/apt.conf.d/50unattended-upgrades and edit the Unattended-Upgrade::Allowed-Origins block at the top of the file so it looks like this:

// Automatically upgrade packages from these (origin:archive) pairs
Unattended-Upgrade::Allowed-Origins {
        "${distro_id}:${distro_codename}";
        "${distro_id}:${distro_codename}-security";
        // Extended Security Maintenance; doesn't necessarily exist for
        // every release and this system may not have it installed, but if
        // available, the policy for updates is such that unattended-upgrades
        // should also install from here by default.
        "${distro_id}ESM:${distro_codename}";
//      "${distro_id}:${distro_codename}-updates";
//      "${distro_id}:${distro_codename}-proposed";
//      "${distro_id}:${distro_codename}-backports";
        // manually added so that auto updates include updates to Kodi
        "LP-PPA-team-xbmc:${distro_codename}";
};

Sharing the media database with other kodi machines on your network

All of Kodi's settings can be stored in a mysql database that is made available over the network to other machines. This lets you share your media library and do things like start watching a video on one device, pause it, move rooms and pick up where you left off on the new device.

First we need to set up a mysql server:

sudo apt-get update
sudo apt-get install mysql-server

During installation you will be asked to set a root password for database administration. Don't forget it!

The next step is to start a mysql shell and create a user and database for kodi. This command will start a shell as the mysql root user (type the password you created during installation to authenticate):

mysql -u root -p

This will give you a mysql shell that looks like this:

mysql >

I've kept the prompt in the commands that follow, to distinguish them from normal shell commands. First, create a database username for kodi:

mysql > CREATE USER 'kodi' IDENTIFIED BY 'kodi';

In the command above, the part IDENTIFIED BY "kodi" is setting the password "kodi" for kodi; choose a different password if you like.

Next, give the new user kodi the privileges it needs to create tables and edit them:

mysql > GRANT ALL ON *.* TO 'kodi';

And make the changes take effect:

mysql > flush privileges;

When you are done, type quit and hit enter to exit the mysql shell.

Now, to tell Kodi to use the newly created database create the file ~/.kodi/userdata/advancedsettings.xml with contents:

<advancedsettings>
  <videodatabase>
    <type>mysql</type>
    <host>localhost</host>
    <port>3306</port>
    <user>kodi</user>
    <pass>kodi</pass>
  </videodatabase>

  <musicdatabase>
    <type>mysql</type>
    <host>localhost</host>
    <port>3306</port>
    <user>kodi</user>
    <pass>kodi</pass>
  </musicdatabase>

  <videolibrary>
    <importwatchedstate>true</importwatchedstate>
    <importresumepoint>true</importresumepoint>
  </videolibrary>
</advancedsettings>

If you chose a different password in the previous step, make sure to change the <pass>kodi</pass> parts.

At the moment, this database is only available on the local machine. To make it accessible to other clients on the local network, open /etc/mysql/mysql.conf.d/mysqld.cnf and find the following line under the [mysqld] section:

bind-address = 127.0.0.1

Change it (or comment it and add a new line) to this:

bind-address = 0.0.0.0

And restart mysql:

sudo service mysql restart

To verify that mysql is listening for remote connections, try connecting from a different machine using telnet (replace the IP address with the IP of your kodi box):

telnet 192.168.X.X 3306

Now all you need to do to use this kodi library on other boxes is to put the same advancedsettings.xml file we used earlier on the second machine, but replace localhost with the IP address of the kodi box.

Stuck? Give me a shout in the comments below. If you're done, move on to the next part of the tutorial.

Type: