Skip to content

Commit

Permalink
✨ New tutorial - "How to setup a Factorio server on a VPS or Dedicate…
Browse files Browse the repository at this point in the history
…d server" (#991)

* add make-a-factorio-server tutorial
* Added link
* Spelling and formatting updates

---------

Co-authored-by: svenja.michal <[email protected]>
  • Loading branch information
JopGamer and svenja11 authored Dec 10, 2024
1 parent 068909e commit 11980d0
Showing 1 changed file with 351 additions and 0 deletions.
351 changes: 351 additions & 0 deletions tutorials/game-server-factorio/01.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,351 @@
---
SPDX-License-Identifier: MIT
path: "/tutorials/game-server-factorio"
slug: "game-server-factorio"
date: "2024-12-10"
title: "How to setup a Factorio server on a VPS or Dedicated server"
short_description: "You will learn how to setup a Factorio server on a VPS or dedicated server."
tags: ["Factorio", "Gaming"]
author: "Jozef Mäsiar"
author_link: "https://github.com/JopGamer"
author_img: "https://avatars.githubusercontent.com/u/59136132"
author_description: ""
language: "en"
available_languages: ["en"]
header_img: "header-8"
cta: "gaming"
---

## Introduction

This tutorial will show you how to create a Factorio server on your VPS or dedicated server.

This tutorial was tested on **Ubuntu 24.04.**

**Prerequisites**

* **One local device** with [Factorio](https://www.factorio.com/) installed on it → so you can connect to the game server.
* **One server** with Ubuntu → to host the game server.
* You will need access to a root account or a user with sudo permissions.
* This server has to be **x86-64**.

**Example terminology**

* `203.0.113.1` - Example public IP of the remote server
* `faserver` - Example user on the remote server

## Step 1 - Setting up the server

In this section, we will set up a new non-root user with sudo permissions that we can use to run the server. We will also download the server and create a first save!

### Step 1.1 - Adding new non-root user

It's usually best to avoid using the root user. As root users can do anything they want on the server, there's a risk of accidental or unintentional changes being made. One way to reduce this risk is to create a new user with sudo privileges. So let's create the new user.

```bash
adduser faserver
```

Now you will be asked for a password. Make sure to set a secure one!

You don't need to set the personal details. Just press enter to leave default values.
Press enter to confirm the details.

```bash
Is the information correct? [Y/n] Y
```

Next, add the new user to the sudo group:

```bash
usermod -aG sudo faserver
```

We will need to log in as our new user:

```bash
su - faserver
```

### Step 1.2 - Enabling port 34197

If you've got a firewall on your VPS, you'll need to make sure it allows port 34197 UDP so you can connect to it from the internet.

There are a few firewalls you can use, but we'll just cover these two:

<details>

<summary>UFW → Default firewall on Ubuntu</summary>

First things first, let's check if our firewall is enabled.

```bash
sudo ufw status
```

If you see `Status: active` then your firewall is up and running. Now we can add our new rule:

```bash
sudo ufw allow 34197/udp
```

Now we can check if our rules are as they should be

```bash
sudo ufw status
```

and you should see something like this

```bash
faserver@ubuntu:~# sudo ufw status
Status: active

To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
34197/udp ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
34197/udp (v6) ALLOW Anywhere (v6)
```

The resulting output may vary slightly depending on the rules that were applied prior to this step.

</details>

<details>

<summary>Hetzner Cloud Firewall</summary>

This only applies if you're using a [Hetzner Cloud](https://console.hetzner.cloud/projects) server and have Hetzner Cloud Firewall enabled. To edit your firewall rules, just go to Cloud Console and select your project. When you're in your project, select "Firewall" from the sidebar. Then select the firewall from the list that is applied to your server. Add the following "inbound" rule:

| IPs | Protocol | Port | Port range |
| -----------------| -------- | ----- | ------------- |
| `Any IPv4` `Any IPv6` | UDP | 34197 | *Leave empty* |
</details>

### Step 1.3 - Downloading Factorio server

Now we can download the latest version of the game

```bash
wget https://factorio.com/get-download/stable/headless/linux64 -O factorio-server.tar.xz
```

> **NOTE:** If you want an older version, just add the version of the game. For example if you want version **1.1.109**, the link will look like this `https://factorio.com/get-download/1.1.109/headless/linux64`. You can find a full list of all versions here: [factorio.com/download/archive](https://factorio.com/download/archive/)
We will need to extract the `tar.xz` file. To extract it, run this command:

```bash
tar -xf factorio-server.tar.xz
```

This will create a new directory called "factorio", so we will go into it:

```bash
cd factorio
```

### Step 1.4 - Creating a save

The Factorio server won't start without a save. You can create one using this command:

```bash
mkdir saves && ./bin/x64/factorio --create ./saves/my-save.zip
```

This command will create a map using the default settings. You can replace `my-save` with a name of your choice.

### Step 1.5 - Starting the server

Now we can start the server:

```bash
./bin/x64/factorio --start-server-load-latest
```

This command will use tha latest save that is in the "saves" directory.

You can use `/players` to test if the game server is working:

```bash
2.439 Info ServerMultiplayerManager.cpp:806: updateTick(0) changing state from(CreatingGame) to(InGame)
/players
Players (0):
```

On your local device, you can now open the Factorio launcher, select "Multiplayer", click the "connect to an address" option, and enter your IP address and the default port, e.g. `203.0.113.1:34197`.

On your server, you can run `/quit` to stop the game.

## Step 2 - Making the server run non-stop

For this, we will use `systemd`.

1. Create a new file called `factorio-server.service` in `/etc/systemd/system` and edit it.
```bash
sudo touch /etc/systemd/system/factorio-server.service && sudo nano /etc/systemd/system/factorio-server.service
```

2. Here are the required contents of the `factorio-server.service` file:
```toml
[Unit]
Description=Factorio Headless Server

[Service]
Type=simpleP
User=faserver
WorkingDirectory=/home/faserver/factorio
ExecStart=/home/faserver/factorio/bin/x64/factorio --start-server-load-latest

[Install]
WantedBy=multi-user.target
```

3. Make it so the server starts on server boot
```bash
sudo systemctl enable factorio-server.service
```

4. Start the server
```bash
sudo systemctl start factorio-server.service
```

Run `ss -ul` to check if the game is running on port 34197.

On your local device, you can now open the Factorio launcher and connect to the game as explained before.

## Step 3 - Customization

### Step 3.1 - Changing the port

1. Open the `factorio-server.service` file:
```bash
sudo nano /etc/systemd/system/factorio-server.service
```

2. Change the 8th line:
```bash
# Before
ExecStart=/home/faserver/factorio/bin/x64/factorio --start-server-load-latest

# After
ExecStart=/home/faserver/factorio/bin/x64/factorio --start-server-load-latest --port <your-port>
```

Replace the port with your desired port (it should have 4 or 5 digits).

> **NOTE:** If you password protected your game, don't forget to add `--password <your-password>` to the end of the replaced line! Also don't forget to update your firewall settings! Refer to [Step 1.3](#step-13---enabling-port-34197) if you have UFW or Hetzner Cloud Firewall.
3. Restart the game server:
```bash
sudo systemctl daemon-reload
sudo systemctl restart factorio-server.service
```

Run `ss -ul` to check if the game is running on your preferred port.

On your local device, you can now open the Factorio launcher, select "Multiplayer", click the "connect to an address" option, and enter your IP address and the custom port, e.g. `203.0.113.1:<your-port>`.

### Step 3.2 - Giving yourself admin permissions

1. Stop the game server:
```bash
sudo systemctl stop factorio-server.service
```

2. Start the game server manually:
```bash
cd factorio/ && ./bin/x64/factorio --start-server-load-latest
```

> **NOTE:** If you set custom port don't forget to add `--port <your-port>` at the end of the command
3. Join the game server and run:
```bash
/promote <your-username>
```

4. Stop the game server using `CTRL`+`C` and restart the game server via `systemctl`:
```bash
sudo systemctl start factorio-server.service
```

### Step 3.3 - Protecting your game

You can protect your game using a password or a whitelist. We will only focus on password protection here.

1. Open `factorio-server.service` file:
```bash
sudo nano /etc/systemd/system/factorio-server.service
```

2. Change the 8th line:
```bash
# Before:
ExecStart=/home/faserver/factorio/bin/x64/factorio --start-server-load-latest

# After:
ExecStart=/home/faserver/factorio/bin/x64/factorio --start-server-load-latest --password <your-password>
```

> **NOTE:** If you set a custom port, don't forget to add `--port <your-port>` to the end of the replaced line!
3. Restart the server:
```bash
sudo systemctl daemon-reload
sudo systemctl restart factorio-server.service
```

## Step 4 - Updating the Factorio version

1. Download the latest version of Factorio:
```bash
wget https://factorio.com/get-download/stable/headless/linux64 -O factorio-server.tar.xz
```

2. Extract the latest version of Factorio:
```bash
tar -xf factorio-server.tar.xz
```

3. And restart the server:
```bash
sudo systemctl restart factorio-server.service
```

## Conclusion

You should now have a Factorio game server up and running. For more custom settings, edit `factorio/config/config.ini`. To enable a setting, remove the `;` symbol at the beginning of the corresponding line. To apply the changes, restart the game server with systemctl.

##### License: MIT

<!--
Contributor's Certificate of Origin
By making a contribution to this project, I certify that:
(a) The contribution was created in whole or in part by me and I have
the right to submit it under the license indicated in the file; or
(b) The contribution is based upon previous work that, to the best of my
knowledge, is covered under an appropriate license and I have the
right under that license to submit that work with modifications,
whether created in whole or in part by me, under the same license
(unless I am permitted to submit under a different license), as
indicated in the file; or
(c) The contribution was provided directly to me by some other person
who certified (a), (b) or (c) and I have not modified it.
(d) I understand and agree that this project and the contribution are
public and that a record of the contribution (including all personal
information I submit with it, including my sign-off) is maintained
indefinitely and may be redistributed consistent with this project
or the license(s) involved.
Signed-off-by: Jozef Mäsiar [email protected]
-->

0 comments on commit 11980d0

Please sign in to comment.