Bastille Templates für FreeBSD Jails

Bastille Templates

Attention: For the english version, click here!

Bastille Templates sind eine hervorragende Möglichkeit, Jails auf einfachem Wege zu erstellen, zu automatisieren und zu verwalten. Es ist zwar grundsätzlich äußerst bequem einen Container mit Bastille zu erstellen, aber durch Templates ist es möglich einen Service direkt zu installieren und eventuelle Konfigurationen vorzunehmen. In diesem Tutorial möchte ich kurz zeigen, wie das Ganze funktioniert. Wer noch nie von Bastille ein Wort gehört hat, sollte sich den Artikel „Bastille – Der Jail Manager unter FreeBSD“ durchlesen, denn für die Nutzung von Templates brauchen wir eine FreeBSD Jail als Grundstein.

Auf Gitlab finden wir eine große Auswahl von bereits erstellten Templates für Bastille. Wir haben allerdings auch die Möglichkeit eigene Templates zu erstellen.

Der Vorteil von Templates lässt sich einfach zusammenfassen. Templates lassen sich automatisch umsetzen und bieten erhöhte Sicherheit und Genauigkeit bei der Konfiguration. Hat man ein Template gewissenhaft erstellt, so kann man sich sicher sein, dass alle darauf basierenden Jails sauber konfiguriert sind.

Heute wollen wir als erstes ein bereits verfügbares Template beziehen und auf eine Jail anwenden. Hierbei möchte ich auch mal einen kurzen Blick in das entsprechende File werfen, damit wir sehen, was das Script eigentlich tut. Für uns selbst setzen wir damit einen Grundstein, was den Umgang mit Templates angehen. Im nächsten Post werden wir auch ein eigenes Template from the scratch erstellen.

Genug geredet, lasst uns anfangen!

Jail erstellen

Als Template habe ich mir aus dem Gitlab-Repo den Apache Web Server herausgesucht. Wie der Name schon sagt, ist es neben ein nginx einer der gängigen Web Server im WWW.

Hier nochmal schnell die Schritte für die Erstellung einer FreeBSD 13 Jail. Als Erstes updaten wir unser Repository und installieren Bastille, falls es noch nicht installiert ist. Danach bootstrappen wir das 13.1-RELEASE von FreeBSD. Im nächsten Schritt sagen wir: „Erstelle eine Jail namens www, basierend auf dem zuvor geladenen Release mit der IP 192.168.178.190 auf die Schnittstelle em0, die hier meinem Host-System gehört, aber trotzdem auch von der Jail als Alias genutzt werden soll“.

pkg update
pkg install bastille
bastille bootstrap 13.1-RELEASE
bastille create www 13.1-RELEASE 192.168.178.190 em0

So sollte es zum Schluss aussehen:

[www]:
www: created

Das Bastillefile

Wenn wir uns das Bastillefile vom Template des Apache anschauen, erkennt jeder, der bereits einen Apache installiert hat, die Befehle wieder:

PKG apache24
SYSRC apache24_enable=YES
SYSRC apache24_flags=""
CMD httpd -t
SERVICE apache24 start

Das Programm apache24 wird in Form eines Packages installiert. Der Dienst wird unter /etc/rc.conf eingetragen, sodass bei einem Neustart die – aufgepasst – Jail, nicht das Host-System, der Dienst automatisch wieder gestartet wird. Als Nächstes könnte man, sofern benötigt, noch flags setzen um weitere Optionen für den Apache zu setzen. CMD httpd -t führt Syntax-Tests nur für conf-Files durch. Die letzte Zeile sagt aus, dass der Dienst nach erfolgreicher Installation gleich gestartet werden soll. Man merkt direkt wieder, dass der Bastille-Entwickler Wert auf Einfachkeit legt. Wir könnten hier an der Stelle auch noch eigene Anpassungen am Bastillefile vornehmen, aber das, wie gesagt, beim nächsten Bastille-Tutorial.

Das Bastille Templates anwenden

Schaut man sich die README.md an, sieht man direkt, dass neben der FreeBSD Jail nur zwei weitere Befehle nötig sind um das Template zum Laufen zu bringen. Einer zum Bootstrappen und der andere, um das Template auf die vorhandene Jail anzuwenden. Also, machen wir das!

Vorsicht: Falls nicht schon längst geschehen, muss auch noch das package git installiert werden.

pkg install git
bastille bootstrap https://gitlab.com/bastillebsd-templates/apache

Der Output sollte dann das ergeben:

Template ready to use.

Jetzt möchten wir das Template natürlich auf unsere eben erstellte Jail namens www anwenden:

bastille template www bastillebsd-templates/apache

Euer Dienst ist nun einsatzbereit, Gratulation! Als Beweis ruft in eurem Browser doch mal die IP der Jail auf und ihr werdet die typische HTML-Seite von Apache sehen. In meinem Fall ist das die 192.168.178.190:

Apache: "It works!"

Fazit

Ihr werdet es vielleicht schon gemerkt haben, aber ich bin großer Fan von Bastille und seinen Möglichkeiten geworden. Für mich ist der Einsatz von Templates einer super Möglichkeit, den Umgang mit Jails zu vereinfachen, Konfigurationsarbeit zu vereinfachen und für die ganz Wilden lässt sich alles prima mit dem Tool Ansible automatisieren. Fehler werden reduziert und man ist sehr flexibel in seiner Gestaltung. Schaut es euch an, überzeugt euch selbst. Was an der Stelle auch noch wichtig ist, es lassen sich mehrere Templates auf eine Jail anwenden. So könnte man beispielsweise eine Jail erstellen und mit den Templates Apache, MySQL und dem Zabbix-Server alles soweit erstellen, dass nur noch wenige conf-Dateien angepasst und die Datenbank erstellt werden müssen. Weiter kann man sich sein eigenes Template erstellen oder bereits bestehende anpassen, dazu dann aber mehr im nächsten Tutorial. Wie immer: Viel Spaß beim Selbermachen!


Bastille templates for FreeBSD jails

Bastille templates are an excellent way to create, automate, and manage jails easily. While creating a container with Bastille is generally extremely convenient, templates make it possible to install a service directly and make any necessary configurations. In this tutorial, I would like to briefly show how it all works. If you have never heard of Bastille, you should read the article „Bastille – The Jail Manager under FreeBSD“ (english version is also available), because we need a FreeBSD Jail as the foundation for using templates.

On Gitlab, we can find a wide selection of pre-created templates for Bastille. However, we also have the option to create our own templates.

The advantage of templates can be summed up simply. Templates can be automatically implemented and offer increased security and accuracy in configuration. If a template is created carefully, we can be sure that all jails based on it are properly configured.

Today, as a first step, we want to obtain an already available template and apply it to a jail. In doing so, I also want to take a brief look at the corresponding file so that we can see what the script actually does. This will lay the groundwork for us in terms of working with templates. In the next post, we will also create our own template from scratch.

Enough talking, let’s get started!

Creating a jail

I have chosen the Apache Web Server as the template from the Gitlab repo. As the name suggests, it is one of the common web servers in the WWW, alongside nginx.

Here are the steps for creating a FreeBSD 13 Jail. First, we update our repository and install Bastille if it is not already installed. Then, we bootstrap the 13.1-RELEASE of FreeBSD. Next, we say: „Create a jail named www, based on the previously loaded release with the IP 192.168.178.190 on the em0 interface, which belongs to my host system but should also be used as an alias by the jail.

pkg update
pkg install bastille
bastille bootstrap 13.1-RELEASE
bastille create www 13.1-RELEASE 192.168.178.190 em0

This is how it should look like now:

[www]:
www: created

The Bastillefile

If we take a look at the Bastillefile from the Apache template, anyone who has already installed Apache will recognize the commands:

PKG apache24
SYSRC apache24_enable=YES
SYSRC apache24_flags=""
CMD httpd -t
SERVICE apache24 start

The program apache24 is installed in the form of a package. The service is registered under /etc/rc.conf, so that upon a reboot, the service will automatically be started in the – attention – jail, not the host system. Next, if needed, we could set flags to add more options for Apache. CMD httpd -t performs syntax tests only for conf files. The last line indicates that the service should be started immediately after a successful installation. Once again, we can see that the Bastille developer values simplicity. We could make our own adjustments to the Bastillefile at this point, but we’ll save that for the next Bastille tutorial.

Applying Bastille templates

If you look at the README.md file, you’ll see that in addition to the FreeBSD jail, only two more commands are needed to get the template up and running. One to bootstrap, and the other to apply the template to the existing jail. So, let’s do it!

Note: If it hasn’t already been done, the git package must also be installed.

pkg install git
bastille bootstrap https://gitlab.com/bastillebsd-templates/apache

Your output should look like that:

Template ready to use.

Now, we want to apply the template to our newly created jail called www:

bastille template www bastillebsd-templates/apache

Great, your service is now ready to use! As proof, try accessing the IP of the jail in your browser and you will see the typical HTML page of Apache. In my case, it is 192.168.178.190:

Result

You may have noticed already, but I have become a big fan of Bastille and its possibilities. For me, the use of templates is a great way to simplify the handling of Jails, simplify configuration work, and for the more adventurous, everything can be easily automated with the tool Ansible. Errors are reduced and you have great flexibility in your design. Take a look at it, convince yourself. Another important thing to note is that multiple templates can be applied to a Jail. For example, you could create a Jail and use the templates for Apache, MySQL, and the Zabbix server to create everything you need, so that only a few conf files need to be adjusted and the database needs to be created. Furthermore, you can create your own template or modify existing ones, but more on that in the next tutorial. As always: Have fun doing it yourself

Start the discussion

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert