Setting up a OpenTTD Server
Tags: linux, games, systemd
Published: 2020-07-09 15:04:33

######Tl;Dr; at the end

> OpenTTD is a business simulation game in which players try to earn money via transporting passengers and freight by road, rail, water and air. It is an open-source[3] remake and expansion of the 1994 Chris Sawyer video game Transport Tycoon Deluxe. (from: [Wikipedia](https://en.wikipedia.org/wiki/OpenTTD))

Setting up a server to play with your friends is a easy thing to do, but finding a guide has been really hard.
So I decided to write my own:

##Prerequisites

* You have a Linux server with Ubuntu
* You have cli and sudo access to that server
* You have your favorite editor installed (this guide uses nano)

##Getting OpenTTD
Getting OpenTTD is easy. Simply run 
`sudo apt install openttd openttd-opengfx` 
and you're set!

##Making OpenTTD run 
To make sure OpenTTD keeps running even after you close your terminal window, it has to be a systemd service. 
To create a service navigate to /etc/systemd/system (using `cd /etc/systemd/system`) and create a new file called openttd.service (`sudo nano openttd.service`).

In this file, we add the following lines:
```
[Unit]
Description=OpenTTD Server
Documentation=""

[Service]
Type=simple
ExecStart=/usr/games/openttd -D
Restart=on-failure
RestartSec=10s

[Install]
WantedBy=multi-user.target
```
###Let's see whats going on here line by line:
The file is separated into three main parts, `[Unit]`,`[Service]` and `[Install]`

* `[Unit]` is for metadata like the title of the service and its relationship to other services
* `[Service]` is the place for instructions on what the service should do
* `[Install]` tells systemd how to enable and disable the service (this part stays the same most of the time)  

###A closer look at `[Service]`
First we define the service type with `Type=simple`. This is the default type and tells systemd that this service is going to be a long running one.
The next line is `ExecStart=/usr/games/openttd -D`. This tells systemd to start `/usr/games/openttd` with the `-D` command line flag (`-D` tells OpenTTD to start in server mode)
The next to lines `Restart=on-failure` and `RestartSec=10s` tell systemd to restart the service if if fails, but not before it has waited ten seconds.

(If you want more information on services, [this](https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files#anatomy-of-a-unit-file) is a good guide)

###Starting the service
To start the service, you first have to enable it. Enable it with:
 `sudo systemctl enable openttd.service`.
Afterwards, you can start it with
 `sudo systemctl start openttd.service` 
and view its status with
 `sudo systemctl status openttd.service`.

##Configuring the firewall
To access the server from anywhere in the world, you'll have to configure the firewall of your server. Thankfully, Ubuntu makes this easy!

Install the "**U**ncomplicated **F**ire **W**all":
`sudo apt install ufw`
and allow connections on the ssh and the OpenTTD ports:
`sudo ufw allow ssh`
`sudo ufw allow 3979`
This makes your server available to the world, but it will not be visible on the public server list. If you want to add your server to the list, run
`sudo ufw allow 3979/udp`
Now you need to enable the firewall. You can do this by running: 
`sudo ufw enable`

And that's it! Your OpenTTD server should be working fully now!

If your are searching for the openttd.cfg, it is located at `/root/.openttd/openttd.cfg`

##TL;DR:
These are the commands you need to setup a server:
```
sudo apt install openttd openttd-opengfx ufw
sudo nano /etc/systemd/system/openttd.service
```
Paste this inside the file: 
```
[Unit]
Description=OpenTTD Server
Documentation=""

[Service]
Type=simple
ExecStart=/usr/games/openttd -D
Restart=on-failure
RestartSec=10s

[Install]
WantedBy=multi-user.target
```
```
sudo systemctl enable openttd.service
sudo systemctl start openttd.service
sudo systemctl status openttd.service

sudo ufw allow ssh
sudo ufw allow 3979
sudo ufw enable
```