HOW-TO:Autostart Kodi for Linux: Difference between revisions

From Official Kodi Wiki
Jump to navigation Jump to search
m (Karellen moved page HOW-TO:HOW-TO:Autostart Kodi for Linux to HOW-TO:Autostart Kodi for Linux without leaving a redirect: Error in page name)
 
(114 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{outdated}}
<section begin="intro" />How to automatically start up in Kodi using various Linux distributions.


<section begin="intro" /><big>Any or all or a combination of these methods can be used to configure a Debian-based X Window System to boot directly to Kodi. This should work on any Linux using the X Window System, but these instructions are particular to Debian derivatives, such as Ubuntu. A server edition is best for running only Kodi, but this page assumes booting into a desktop OS, not a server distro. However, the instructions here can be used for a server distro with even less configuration than for a desktop. Experiment inside VirtualBox before you apply any of these to your system and make a backup.</big><section end="intro" />


<big>'''This how-to has flaws: Never Ever start Kodi without a window manager!''' (FernetMenta: the author of Kodi's X11 windowing system)</big>
__TOC__
 
== Installing the X11 Window System ==
 
Even if you are using something like [https://github.com/graysky2/kodi-standalone-service Kodi Standalone Service], you still need use a WM or a DE that installs the '''x-window-manager''' software package.
 
To ensure you have this installed use the '''which''' command.
 
<pre>
which x-window-manager
</pre>
 
If there is no such package, you can use '''apt''' to search your Linux distributions repository.
 
<pre>
apt-cache search x-window-manager
</pre>
 
This will generate a list of packages containing '''x-window-manager''', which cannot be installed alone. If you are not using a DE, you need to use a WM. And, you can also use a compositing WM like Compiz, for fast 3D effects.
 
[https://wiki.archlinux.org/index.php/Compiz Compiz - ArchWiki]
 
A note on using Kodi Standalone Service, this package runs Kodi as a '''systemd''' service and disables the login for user '''kodi''' and will not work when autologin is enabled. In fact, it will break the login process if you autologin user '''kodi''' and run Kodi Standalone Service at the same time. This package is installed automatically with the standard Kodi package when you install Kodi on Raspbian, but must be configured manually for all other Linux distributions. You can run the '''which''' command to find this package as well.
 
<pre>
which kodi-standalone
</pre>
 
Be advised, Kodi Standalone Service still launches the Kodi GUI and still requires a WM even though it is running as a '''systemd''' service. When you install a WM such as '''xfwm4''' (the WM for XFCE4), the CLI will tell you if it provides the '''x-window-manager''' package.
 
<pre>
Setting up xfwm4 (4.12.4-1) ...
update-alternatives: using /usr/bin/xfwm4 to provide /usr/bin/x-window-manager (x-window-manager) in auto mode
</pre>
 
If you do not see this message at the end of the installation, the WM you selected does not provide this software package and may not actually be a complete WM itself but only part of a package containing a WM.
 
For a great example on exactly how you would do this with '''xfwm4''', see this ArchWiki entry.
 
[https://wiki.archlinux.org/index.php/Xfwm XFWM - ArchWiki]
 
Most systems utilize the client version of the X11 software an Xsession requires.
 
For more X11 features and functionality, you may optionally upgrade to the server software packages for the X Window System.
 
<pre>apt install xauth xorg xinit xserver-xorg-core xserver-xorg xserver-common</pre>
 
The Ubuntu wiki is usually a good source of information.
 
[https://wiki.ubuntu.com/CustomXSession CustomXSession - Ubuntu Wiki]
 
Custom user sessions can be created with the X11 Window System to run scripts, autostart apps, and launch a WM or DE.
 
LightDM is used for autologin.
 
== Customize LightDM to autologin ==
 
You can utilize the ability to customize LightDM data manager by changing the builtin custom configuration file '''50-myconfig.conf''' using '''nano''' or another text editor. If you have no DE or WM you can install '''lightdm''', but should make sure to do a bit of research before you do. Most versions of Linux use LightDM, but many server versions do not. For Ubuntu Server with no DE, for example, installing '''lightdm''' is okay and necessary. Changing the behavior of LightDM is done using a custom '''50-myconfig.conf''' configuration file.
 
To see if you have '''lightdm''' use '''which'''.
 
<pre>
which lightdm
</pre>
 
If not, install it with '''apt'''.
 
<pre>
apt install lightdm
</pre>
 
Configure LightDM for autologin to boot directly to Kodi.
 
<pre>
nano /etc/lightdm/lightdm.conf
</pre>
 
Uncomment the following line.
 
<pre>
#autologin-user=
</pre>
 
And add the user you want to autologin. The user is named '''kodi''' in this example.
 
<pre>
autologin-user=kodi
</pre>
 
And then you can create /etc/lightdm/lightdm.conf.d/50-myconfig.conf, but ensure the settings in /etc/lightdm/lightdm.conf agree.
 
Create a custom configuration file called '''50-myconfig.conf''' using '''nano''' or other CLI text editor.
 
<pre>
mkdir /etc/lightdm/lightdm.conf.d/
nano /etc/lightdm/lightdm.conf.d/50-myconfig.conf
</pre>
 
Inside this new file add the following text.
 
<pre>
[SeatDefaults]
autologin-user=kodi
</pre>


CTRL +O +Enter +X to save and exit from '''nano'''.


The main goal of '''most''' of these methods is to start an Xserver only for Kodi. Most of these methods will '''not work''' if you have a window manager installed (however, it should not be hard to modify the scripts to suit your needs). The lightdm method might work if you need to use a window manager.
== Configure Kodi to autostart ==


Choose '''only one''' method from sections 2 and on based on which distribution and init system you're using.<section end="intro" />
Configure Kodi to autostart.


__TOC__
A)
== Create a user to run Kodi ==
 
For security reasons, it is recommended (but optional) to use a dedicated user to run Kodi. The user needs access to audio and video devices as well as access the internet if you're going to use any features that require internet access. Most methods present here allow to specify which user will start / own the Kodi process.
<pre>
sudo sed -i "1i @kodi" /etc/xdg/lxsession/LXDE-pi/autostart
</pre>
 
B)
 
<pre>
mkdir -p ~/.config/autostart
ln -s /usr/bin/kodi ~/.config/autostart
</pre>
 
This method of autostart requires a desktop environment or DE. This is done a bit differently by each DE. Some helpful links can easily be found in today's Web search using any search engine.
 
[http://edoceo.com/gui/xfce-autostart-apps Autostart Apps in XFCE]
 
And this link shows how to autostart an app in LXDE on Raspbian.
 
[http://www.raspberry-projects.com/pi/pi-operating-systems/raspbian/auto-running-programs-gui Auto running programs LXDE - Raspberry Pi Projects]
 
The Raspberry Pi is a bit more of a tweak, but these are excellent links that will tell you how. The first link is the most helpful to making sure Kodi autostarts in Pi, and utilizes a '''crontab'''. There are many settings on the Pi that control boot, and the first link gives the best most trouble-free way to boot directly to Kodi.
 
[https://www.leowkahman.com/2016/03/05/lamp-stack-on-raspberry-pi-with-kodi-running-at-startup/ LAMP stack on Raspberry Pi with Kodi running at startup]
 
In the above link, the author describes using Crontab (short for "Cron table") to boot directly to Kodi.
 
<pre>
apt install cron
crontab -e
</pre>
 
Cron will ask you to choose a text editor from available options. I choose option 2 for '''nano'''.  
 
In the new Crontab, enter the following text.
 
<pre>
@reboot kodi --standalone
</pre>


Notice the groups might vary from one distro to another. The groups used below are for Debian-based distributions. To create the user (named '''kodi''' here) and give it the necessary permissions, run
Raspbian has a repository package for everything, so kodi-standalone can be run as a package rather than a command argument. You do not have to install '''kodi-standalone''' with Apt.


<pre>
<pre>
# adduser kodi
apt install kodi-standalone
</pre>
</pre>


Another option is to add a loginless and passwordless user. You can do so by running
For instance, if I were to install '''kodi-standalone''' after installing the standard '''kodi''' package, this is the message I get.


<pre>
<pre>
# adduser --disabled-password --disabled-login --gecos "" kodi
root@raspberrypi:/home/pi# apt install kodi-standalone
Reading package lists... Done
Building dependency tree     
Reading state information... Done
Note, selecting 'kodi' instead of 'kodi-standalone'
kodi is already the newest version (2:18.7-1~buster).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
root@raspberrypi:/home/pi#
</pre>
</pre>


Then, assign it to the following groups in order to give it the permissions it needs.
That is because it used to a separate package, but since then it has been made into a command. It is really only a trivial difference, instead of using:


<pre>
<pre>
# usermod -a -G cdrom,audio,video,plugdev,users,dialout,dip,input kodi
@reboot kodi --standalone
</pre>
</pre>


To give it access to the internet, add the group '''netdev''' as well.
You can use,


== Upstart init script ==
<pre>
Works on Ubuntu up to version 14.10 (Utopic Unicorn). Stating with version 15.04 (Vivid Vervet), Ubuntu has switched to systemd and adding a systemd script might be prefereable. Create a '''<code>/etc/init/kodi.conf</code>''' with following contents.
@reboot kodi-standalone
<source lang="xml">
</pre>
# kodi-upstart
# starts Kodi on startup by using xinit.
# by default runs as kodi, to change edit below.
env USER=kodi


description    "Kodi-barebones-upstart-script"
One or the other, not both.
author          "Matt Filetto"


start on (filesystem and stopped udevtrigger)
You could also consider using Crontab to run a script, much like described on this page in the section entitled "Run kodi in a window manager" if you scroll down the page.
stop on runlevel [016]


# tell upstart to respawn the process if abnormal exit
[https://wiki.archlinux.org/index.php/Kodi Kodi - Run kodi in a window manager]
respawn


script
Instead your Crontab could look like this.
  exec su -c "xinit /usr/bin/kodi-standalone -- -nocursor :0" $USER
end script
</source><br />


{{note|<code> -- -nocursor</code> option '''kills all X cursor''' on Kodi startup and does not interfere with mouse use/operation}}
<pre>
@reboot startx
</pre>


You may have to edit '''<code>/etc/X11/Xwrapper.config</code>''' and replace the last line that says:
And create an '''~/.xinitrc''' file.


<source lang="xml">
<pre>
allowed_users=console
nano ~/.xinitrc
</source>
</pre>


to
The '''~/.xinitrc''' file should look like this.


<source lang="xml">
<pre>
allowed_users=anybody
#!/bin/bash
</source><br />
kodi --standalone #(this is proper syntax for most Kodi installs)
#kodi-standalone #(this is the syntax that works on Raspbian, not other Pi operating systems)
sudo chvt 2
sleep 1
sudo chvt 1
</pre>


Kodi will now auto-start on boot and restart/respawn if killed or crashed.
Make sure to make '''~/.xinitrc''' executable by running.


== Modify the inittab ==
<pre>
chmod +rx ~/.xinitrc
</pre>


This was tested on Arch Linux.
To ensure no password is required by '''sudo''' for any user logging in when this script runs, you must edit /etc/sudoers or run the '''visudo''' command if it is installed to edit /etc/sudoers.  


To automatically start xbmc on your system, do the following:
You can install '''visudo''' from your Linux distributions default repository.


<pre>
apt install sudo
</pre>


First you need to make some changes to <code>/etc/inittab</code>. Comment out (add a #) to this line:
To use '''visudo''' simply run the command in the terminal.


<pre>
<pre>
id:3:initdefault
visudo
</pre>
</pre>


to
For any non-root users, create an entry in /etc/sudoers with '''nano'''.


<pre>
<pre>
#id:3:initdefault
nano /etc/sudoers
</pre>
</pre>


and uncomment
Create an entry on any line that is comfortable to you like shown here.


<pre>
<pre>
id:5:initdefault
kodi ALL=NOPASSWD: /usr/bin/chvt
# pi ALL=NOPASSWD: /usr/bin/chvt #(if you do not add user kodi)
</pre>
</pre>


Then add this line to the bottom:
The above example is for users '''pi''' and '''kodi'''.
 
If you decide not use a Cron table, you can always remove it again.


<pre>
<pre>
x:5:wait:login -f <YOUR_XBMC_USERNAME> </dev/tty7 &>/dev/tty7
crontab -r
</pre>
</pre>


Using wait instead of respawn means that you can exit out of xbmc into the console.
This is a quick reference for Crontab.
 
[http://www.adminschoice.com/crontab-quick-reference Crontab Quick Reference]


These linked pages facilitate more tweaks and settings relevant to other uses of the X Window System on Pi.


[http://ozzmaker.com/enable-x-windows-on-piscreen/ Enable X Window System on PiScreen]


* NOTE*: This is a security hole as it autologins a dedicated xbmc user without asking for a password!
Other things to tweak on Raspbian.


[https://yingtongli.me/blog/2016/12/21/splash.html Raspbian splash screen guide - Clean up the boot process]


Now that we have the user logged in we need it to auto start XBMC.
[https://scribles.net/lightning-bolt-under-voltage-warning-on-raspberry-pi/ Under voltage warning and disabling splash screen on Raspbian]
In <code>~/.xinitrc</code> add the following to the end of the file
 
(after removing/commenting any other exec lines that start a windowmanager):
[https://raspberrypi.stackexchange.com/questions/59310/remove-boot-messages-all-text-in-jessie Remove boot messages all text in Raspbian - Raspberry Pi Stack Exchange]
 
And, if you would rather, it is easier to follow the process provided by this link to modify the Raspbian image before installing it onto the microSD card.
 
[https://wiki.debian.org/RaspberryPi/qemu-user-static Modifying Raspbian with qemu-user-static]
 
The above link describes removing the DE from a Raspbian image, though Raspbian Lite is a much better starting point for using Qemu for this purpose.
 
You can also use the X11 Window System to autostart Kodi by placing a startup script in the /etc/X11/Xsession.d/ directory.
 
[https://debian-administration.org/article/50/Running_applications_automatically_when_X_starts Running applications automatically when X starts]


<pre>
<pre>
exec ck-launch-session xbmc
nano /etc/X11/Xsession.d/startup-local
</pre>
</pre>


Insert this text into the new file. Use CTRL + O + Enter + X to save the file.


Add this line to your <code>~/.bash_profile</code>
<pre>
#!/bin/sh
/usr/bin/kodi
</pre>
 
If you have a WM, such as Blackbox you can add it here as well.


<pre>
<pre>
[[ $(tty) = "/dev/tty7" ]] && exec startx </dev/null &>/dev/null
#!/bin/sh
blackbox &
/usr/bin/kodi
</pre>
</pre>


Set permissions to make it executable. Use the ampersand to run each command in the background till the last line or your script will get stuck on the first line.


And create a hushlogin file to suppress login messages.
<pre>
chmod 755 /etc/X11/Xsession.d/startup-local
</pre>
 
If Kodi is not installed at the standard path, find it using the '''which''' command.


<pre>
<pre>
touch ~/.hushlogin
which kodi
</pre>
</pre>


This feature is available on X Window System operating systems most Linux distributions use the X Window System.


Lastly, for the magic sauce that makes this work, add dbus to your daemons in <code>/etc/rc.conf</code>.
== Create custom Xsession script ==
 
To create a custom Xsession script with a text editor we start with an '''xinitrc''' script. For more detail on how to create and use '''xinit''', there is a great guide available here.
 
[https://en.wikibooks.org/wiki/Guide_to_X11/Starting_Sessions Guide to X11 - Starting Sessions - WikiBooks]


<pre>
<pre>
DAEMONS=(... dbus ...)
nano ~/.xinitrc
</pre>
</pre>


To run Kodi full-screen make sure the text reads:


You're finished. Next time you reboot you should be greeted with XBMC.
<pre>
#!/usr/bin/env bash
exec gnome-session &
/usr/bin/kodi -fs
</pre>


== Add a new init script ==
Replace the '''gnome-session''' with the DE or WM command you are using. See the last section for examples.


This method works well under Debian up to version 7 (Wheezy). As of version 8 (Jessie), Debian has switched to systemd and adding a systemd init script might be preferable. The current configuration is a HTPC running Debian Squeeze, with no window manager installed.  
To make '''~/.xinitrc''' executable use.


* Create a new script under '''/etc/init.d/'''. Call it '''kodi'''
<pre>
* Change the rights, in order to allow it to be executable.
chmod +x ~/.xinitrc
<pre># chmod a+x /etc/init.d/kodi</pre>
</pre>
* copy the code under in the file. Modify the variables to suit your configuration:
<pre>#! /bin/sh


### BEGIN INIT INFO
Use '''sudo''' and the '''~/.xinitrc''' file to configure '''~/.xsession'''.
# Provides:          kodi
# Required-Start:    $all
# Required-Stop:    $all
# Default-Start:    2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts instance of Kodi
# Description:      starts instance of Kodi using start-stop-daemon and xinit
### END INIT INFO


############### EDIT ME ##################
<pre>
sudo -H -u kodi bash -c "ln -s ~/.xinitrc /home/kodi/.xsession"
</pre>


# path to xinit exec
Make sure the path goes to user's home directory. In this example, the user is '''kodi'''.
DAEMON=/usr/bin/xinit


# startup args
You need to use '''sudo''' to run this command. If you do not have '''sudo''', install it.
DAEMON_OPTS=" /usr/local/bin/kodi-standalone -- :0"


# script name
<pre>
NAME=kodi
apt install sudo
</pre>


# app name
If you create a symbolic link and want to remove it, simply remove the existing '''.xsession''' file you created.
DESC=Kodi


# user
<pre>
RUN_AS=kodi
rm /home/kodi/.xsession
</pre>


# Path of the PID file
On some systems using a DE such as Gnome, not all systems using Gnome, it may be necessary for '''.xsession''' to be '''.xsessionrc'''. Though this is more likely the case for organization network computers than a personal one, where configurations may vary on a diversity of machines. Most of the time, it will be '''.xsession''' rather than '''.xsessionrc''' that you want to create.  
PID_FILE=/var/run/kodi.pid


############### END EDIT ME ##################
You can also create the '''.xsession''' script directly in the home directory, rather than link it from '''~/.xinitrc'''. This is necessary if you create multiple custom Xsession scripts for multiple users, and you do not in that case link '''~/.xinitrc''' to '''.xsession'''.


test -x $DAEMON || exit 0
To enable multiple displays edit the '''.bashrc''' file.


set -e
<pre>
nano ~/.bashrc
</pre>


case "$1" in
Add the following text.
  start)
        echo "Starting $DESC"
        start-stop-daemon --start -c $RUN_AS --background --pidfile $PID_FILE  --make-pidfile --exec $DAEMON -- $DAEMON_OPTS
        ;;
  stop)
        echo "Stopping $DESC"
        start-stop-daemon --stop --pidfile $PID_FILE
        ;;


  restart|force-reload)
<pre>
        echo "Restarting $DESC"
if [[ ! $DISPLAY && $XDG_VTNR -le 3 ]]; then
        start-stop-daemon --stop --pidfile $PID_FILE
   exec startx
        sleep 5
fi
        start-stop-daemon --start -c $RUN_AS --background --pidfile $PID_FILE  --make-pidfile --exec $DAEMON -- $DAEMON_OPTS
</pre>
        ;;
   *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|force-reload}" >&2
        exit 1
        ;;
esac


exit 0</pre>
For two monitors add the following as shown.
* Test the script by trying to start / stop Kodi with it.
<pre># /etc/init.d/kodi start
........
# /etc/init.d/kodi stop</pre>
* If all is ok, you can add the script to your configuration, by issuing a "update-rc.d"
<pre># update-rc.d kodi defaults</pre>
* If Kodi does not start, you may need to allow X to start from non-consoles. Under Debian/Ubuntu, run:
<pre># dpkg-reconfigure x11-common</pre>
and choose "Anyone".
* You can now reboot the server, Kodi should be started just after the boot sequence.


== Add a new systemd script ==
<pre>
if [[ ! $DISPLAY && $XDG_VTNR -le 2 ]]; then
  exec startx
fi
</pre>


This method is for systems that use systemd as init system such as current versions of Debian (since version 8, Jessie) and Ubuntu (since version 15.04, Vivid Vervet).
For a single monitor use:


* create a new service file under '''/etc/systemd/system''', call it '''kodi.service''' and copy the following code into the file:
<pre>
<pre>[Unit]
if [[ ! $DISPLAY && $XDG_VTNR -eq 1 ]]; then
Description = Kodi Media Center
  exec startx
fi
</pre>


# if you don't need the MySQL DB backend, this should be sufficient
If you wish to remain logged in after quitting X, remove the '''exec''' from the above. Other configurations can be applied, but are irrelevant to autostarting Kodi.
After = systemd-user-sessions.service network.target sound.target


# if you need the MySQL DB backend, use this block instead of the previous
[https://wiki.archlinux.org/index.php/Xinit#Autostart_X_at_login Xinit Autostart X at login]
# After = systemd-user-sessions.service network.target sound.target mysql.service
# Wants = mysql.service


[Service]
If you are using LightDM and you just want to create a new login entry for user '''kodi''', you must create a new file.
User = kodi
Group = kodi
Type = simple
#PAMName = login # you might want to try this one, did not work on all systems
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]
<pre>
WantedBy = multi-user.target
nano /usr/share/xsessions/custom.desktop
</pre>
</pre>


* modify the variables (user, group, paths...) to match your configuration. Save the file.
And insert the following text.
* try to start (and stop) Kodi to make sure the script is working properly
 
<pre># systemctl start kodi
<pre>
........
[Desktop Entry]
# systemctl stop kodi</pre>
Name=Xsession
* If Kodi does not start, you may need to allow X to start from non-consoles. Under Debian/Ubuntu, run:
Exec=/etc/X11/Xsession
<pre># dpkg-reconfigure x11-common</pre>
</pre>
and choose "Anyone".
 
* As of Ubuntu 16.04 (Xenial Xerus), you need to install and reconfigure the package '''xserver-xorg-legacy''' instead:
This will create a new login option for the custom Xsession. Only this one entry is necessary for LightDM and X11 to find the '''~/.xsession''' script in each user's home directory.
<pre># apt-get install xserver-xorg-legacy
 
........
You can use any window manager or desktop enviroment with a custom Xsession script as long as it is installed.
# dpkg-reconfigure xserver-xorg-legacy</pre>
 
and choose "Anyone". You'll also need edit the file '''<code>/etc/X11/Xwrapper.config</code>''' and add the following to a new line at the end of the file:
You do this by creating '''~/.xinitrc''' script that contains the command to start the WM or DE.
 
For instance, if you are using FluxBox.
 
<pre>
<pre>
needs_root_rights=yes
#!/usr/bin/env bash
exec fluxbox &
/usr/bin/kodi -fs
</pre>
</pre>
* if you are using a minimalistic system like Ubuntu Server make sure that xinit and dbus-launch are installed
<pre># sudo apt-get install xorg dbus-x11</pre>
* if you do not experience any issues, make Kodi start automatically
<pre># systemctl enable kodi</pre>
* reboot and you're done


== Use autologin feature of lightdm ==
The <code>-fs</code> option is full-screen.


This works if you have a window manager as well.
To run Gnome in the background, but launch Kodi in the foreground.


* Install lightdm
<pre>
<pre>
sudo apt-get install lightdm
#!/usr/bin/env bash
exec gnome-session &
/usr/bin/kodi -fs
</pre>
</pre>


* Modify '''/etc/lightdm/lightdm.conf''' and set the following settings under section '''[Seat:*]''':
Use the '''~/.xinitrc''' file to configure '''~/.xsession''' to create a custom login for any user, not just Kodi.
 
<pre>
<pre>
[Seat:*]
sudo -H -u [username] bash -c "ln -s ~/.xinitrc ~/.xsession"
autologin-user=kodi
</pre>
autologin-session=kodi
 
You must reconfigure Xserver for any user that is not a privileged user to be able to start a custom Xsession.
 
<pre>
dpkg-reconfigure xserver-xorg-legacy
dpkg-reconfigure xserver-xorg
</pre>
</pre>
* Reboot and you're done
 
When you reconfigure X11 with these commands, choose to allow non-root users (anybody) to start an Xsession. You should see your changes take effect after rebooting.
 
 
[[Category:Linux]]

Latest revision as of 18:49, 26 April 2021

Any or all or a combination of these methods can be used to configure a Debian-based X Window System to boot directly to Kodi. This should work on any Linux using the X Window System, but these instructions are particular to Debian derivatives, such as Ubuntu. A server edition is best for running only Kodi, but this page assumes booting into a desktop OS, not a server distro. However, the instructions here can be used for a server distro with even less configuration than for a desktop. Experiment inside VirtualBox before you apply any of these to your system and make a backup.

Installing the X11 Window System

Even if you are using something like Kodi Standalone Service, you still need use a WM or a DE that installs the x-window-manager software package.

To ensure you have this installed use the which command.

which x-window-manager

If there is no such package, you can use apt to search your Linux distributions repository.

apt-cache search x-window-manager

This will generate a list of packages containing x-window-manager, which cannot be installed alone. If you are not using a DE, you need to use a WM. And, you can also use a compositing WM like Compiz, for fast 3D effects.

Compiz - ArchWiki

A note on using Kodi Standalone Service, this package runs Kodi as a systemd service and disables the login for user kodi and will not work when autologin is enabled. In fact, it will break the login process if you autologin user kodi and run Kodi Standalone Service at the same time. This package is installed automatically with the standard Kodi package when you install Kodi on Raspbian, but must be configured manually for all other Linux distributions. You can run the which command to find this package as well.

which kodi-standalone

Be advised, Kodi Standalone Service still launches the Kodi GUI and still requires a WM even though it is running as a systemd service. When you install a WM such as xfwm4 (the WM for XFCE4), the CLI will tell you if it provides the x-window-manager package.

Setting up xfwm4 (4.12.4-1) ...
update-alternatives: using /usr/bin/xfwm4 to provide /usr/bin/x-window-manager (x-window-manager) in auto mode

If you do not see this message at the end of the installation, the WM you selected does not provide this software package and may not actually be a complete WM itself but only part of a package containing a WM.

For a great example on exactly how you would do this with xfwm4, see this ArchWiki entry.

XFWM - ArchWiki

Most systems utilize the client version of the X11 software an Xsession requires.

For more X11 features and functionality, you may optionally upgrade to the server software packages for the X Window System.

apt install xauth xorg xinit xserver-xorg-core xserver-xorg xserver-common

The Ubuntu wiki is usually a good source of information.

CustomXSession - Ubuntu Wiki

Custom user sessions can be created with the X11 Window System to run scripts, autostart apps, and launch a WM or DE.

LightDM is used for autologin.

Customize LightDM to autologin

You can utilize the ability to customize LightDM data manager by changing the builtin custom configuration file 50-myconfig.conf using nano or another text editor. If you have no DE or WM you can install lightdm, but should make sure to do a bit of research before you do. Most versions of Linux use LightDM, but many server versions do not. For Ubuntu Server with no DE, for example, installing lightdm is okay and necessary. Changing the behavior of LightDM is done using a custom 50-myconfig.conf configuration file.

To see if you have lightdm use which.

which lightdm

If not, install it with apt.

apt install lightdm

Configure LightDM for autologin to boot directly to Kodi.

nano /etc/lightdm/lightdm.conf

Uncomment the following line.

#autologin-user=

And add the user you want to autologin. The user is named kodi in this example.

autologin-user=kodi

And then you can create /etc/lightdm/lightdm.conf.d/50-myconfig.conf, but ensure the settings in /etc/lightdm/lightdm.conf agree.

Create a custom configuration file called 50-myconfig.conf using nano or other CLI text editor.

mkdir /etc/lightdm/lightdm.conf.d/
nano /etc/lightdm/lightdm.conf.d/50-myconfig.conf

Inside this new file add the following text.

[SeatDefaults]
autologin-user=kodi

CTRL +O +Enter +X to save and exit from nano.

Configure Kodi to autostart

Configure Kodi to autostart.

A)

sudo sed -i "1i @kodi" /etc/xdg/lxsession/LXDE-pi/autostart

B)

mkdir -p ~/.config/autostart
ln -s /usr/bin/kodi ~/.config/autostart

This method of autostart requires a desktop environment or DE. This is done a bit differently by each DE. Some helpful links can easily be found in today's Web search using any search engine.

Autostart Apps in XFCE

And this link shows how to autostart an app in LXDE on Raspbian.

Auto running programs LXDE - Raspberry Pi Projects

The Raspberry Pi is a bit more of a tweak, but these are excellent links that will tell you how. The first link is the most helpful to making sure Kodi autostarts in Pi, and utilizes a crontab. There are many settings on the Pi that control boot, and the first link gives the best most trouble-free way to boot directly to Kodi.

LAMP stack on Raspberry Pi with Kodi running at startup

In the above link, the author describes using Crontab (short for "Cron table") to boot directly to Kodi.

apt install cron
crontab -e

Cron will ask you to choose a text editor from available options. I choose option 2 for nano.

In the new Crontab, enter the following text.

@reboot kodi --standalone

Raspbian has a repository package for everything, so kodi-standalone can be run as a package rather than a command argument. You do not have to install kodi-standalone with Apt.

apt install kodi-standalone

For instance, if I were to install kodi-standalone after installing the standard kodi package, this is the message I get.

root@raspberrypi:/home/pi# apt install kodi-standalone
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Note, selecting 'kodi' instead of 'kodi-standalone'
kodi is already the newest version (2:18.7-1~buster).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
root@raspberrypi:/home/pi#

That is because it used to a separate package, but since then it has been made into a command. It is really only a trivial difference, instead of using:

@reboot kodi --standalone

You can use,

@reboot kodi-standalone

One or the other, not both.

You could also consider using Crontab to run a script, much like described on this page in the section entitled "Run kodi in a window manager" if you scroll down the page.

Kodi - Run kodi in a window manager

Instead your Crontab could look like this.

@reboot startx

And create an ~/.xinitrc file.

nano ~/.xinitrc

The ~/.xinitrc file should look like this.

#!/bin/bash
kodi --standalone #(this is proper syntax for most Kodi installs)
#kodi-standalone #(this is the syntax that works on Raspbian, not other Pi operating systems)
sudo chvt 2 
sleep 1
sudo chvt 1

Make sure to make ~/.xinitrc executable by running.

chmod +rx ~/.xinitrc

To ensure no password is required by sudo for any user logging in when this script runs, you must edit /etc/sudoers or run the visudo command if it is installed to edit /etc/sudoers.

You can install visudo from your Linux distributions default repository.

apt install sudo

To use visudo simply run the command in the terminal.

visudo

For any non-root users, create an entry in /etc/sudoers with nano.

nano /etc/sudoers

Create an entry on any line that is comfortable to you like shown here.

kodi ALL=NOPASSWD: /usr/bin/chvt
# pi ALL=NOPASSWD: /usr/bin/chvt #(if you do not add user kodi)

The above example is for users pi and kodi.

If you decide not use a Cron table, you can always remove it again.

crontab -r

This is a quick reference for Crontab.

Crontab Quick Reference

These linked pages facilitate more tweaks and settings relevant to other uses of the X Window System on Pi.

Enable X Window System on PiScreen

Other things to tweak on Raspbian.

Raspbian splash screen guide - Clean up the boot process

Under voltage warning and disabling splash screen on Raspbian

Remove boot messages all text in Raspbian - Raspberry Pi Stack Exchange

And, if you would rather, it is easier to follow the process provided by this link to modify the Raspbian image before installing it onto the microSD card.

Modifying Raspbian with qemu-user-static

The above link describes removing the DE from a Raspbian image, though Raspbian Lite is a much better starting point for using Qemu for this purpose.

You can also use the X11 Window System to autostart Kodi by placing a startup script in the /etc/X11/Xsession.d/ directory.

Running applications automatically when X starts

nano /etc/X11/Xsession.d/startup-local

Insert this text into the new file. Use CTRL + O + Enter + X to save the file.

#!/bin/sh
/usr/bin/kodi 

If you have a WM, such as Blackbox you can add it here as well.

#!/bin/sh
blackbox &
/usr/bin/kodi 

Set permissions to make it executable. Use the ampersand to run each command in the background till the last line or your script will get stuck on the first line.

chmod 755 /etc/X11/Xsession.d/startup-local

If Kodi is not installed at the standard path, find it using the which command.

which kodi

This feature is available on X Window System operating systems most Linux distributions use the X Window System.

Create custom Xsession script

To create a custom Xsession script with a text editor we start with an xinitrc script. For more detail on how to create and use xinit, there is a great guide available here.

Guide to X11 - Starting Sessions - WikiBooks

nano ~/.xinitrc

To run Kodi full-screen make sure the text reads:

#!/usr/bin/env bash
exec gnome-session &
/usr/bin/kodi -fs 

Replace the gnome-session with the DE or WM command you are using. See the last section for examples.

To make ~/.xinitrc executable use.

chmod +x ~/.xinitrc

Use sudo and the ~/.xinitrc file to configure ~/.xsession.

sudo -H -u kodi bash -c "ln -s ~/.xinitrc /home/kodi/.xsession"

Make sure the path goes to user's home directory. In this example, the user is kodi.

You need to use sudo to run this command. If you do not have sudo, install it.

apt install sudo

If you create a symbolic link and want to remove it, simply remove the existing .xsession file you created.

rm /home/kodi/.xsession

On some systems using a DE such as Gnome, not all systems using Gnome, it may be necessary for .xsession to be .xsessionrc. Though this is more likely the case for organization network computers than a personal one, where configurations may vary on a diversity of machines. Most of the time, it will be .xsession rather than .xsessionrc that you want to create.

You can also create the .xsession script directly in the home directory, rather than link it from ~/.xinitrc. This is necessary if you create multiple custom Xsession scripts for multiple users, and you do not in that case link ~/.xinitrc to .xsession.

To enable multiple displays edit the .bashrc file.

nano ~/.bashrc

Add the following text.

if [[ ! $DISPLAY && $XDG_VTNR -le 3 ]]; then
  exec startx
fi

For two monitors add the following as shown.

if [[ ! $DISPLAY && $XDG_VTNR -le 2 ]]; then
  exec startx
fi

For a single monitor use:

if [[ ! $DISPLAY && $XDG_VTNR -eq 1 ]]; then
  exec startx
fi

If you wish to remain logged in after quitting X, remove the exec from the above. Other configurations can be applied, but are irrelevant to autostarting Kodi.

Xinit Autostart X at login

If you are using LightDM and you just want to create a new login entry for user kodi, you must create a new file.

nano /usr/share/xsessions/custom.desktop

And insert the following text.

[Desktop Entry]
Name=Xsession
Exec=/etc/X11/Xsession

This will create a new login option for the custom Xsession. Only this one entry is necessary for LightDM and X11 to find the ~/.xsession script in each user's home directory.

You can use any window manager or desktop enviroment with a custom Xsession script as long as it is installed.

You do this by creating ~/.xinitrc script that contains the command to start the WM or DE.

For instance, if you are using FluxBox.

#!/usr/bin/env bash
exec fluxbox &
/usr/bin/kodi -fs

The -fs option is full-screen.

To run Gnome in the background, but launch Kodi in the foreground.

#!/usr/bin/env bash
exec gnome-session &
/usr/bin/kodi -fs

Use the ~/.xinitrc file to configure ~/.xsession to create a custom login for any user, not just Kodi.

sudo -H -u [username] bash -c "ln -s ~/.xinitrc ~/.xsession"

You must reconfigure Xserver for any user that is not a privileged user to be able to start a custom Xsession.

dpkg-reconfigure xserver-xorg-legacy 
dpkg-reconfigure xserver-xorg

When you reconfigure X11 with these commands, choose to allow non-root users (anybody) to start an Xsession. You should see your changes take effect after rebooting.