This is the 10th day of my participation in the Gwen Challenge.More article challenges

FRP is a high performance Intranet penetration tool. This article explains how to use Systemd to manage FRP services and keep THE FRP working properly.

About systemd

Systemd is a system and service manager for Linux. When run as a startup process (PID=1), it will run as an initialization system, that is, starting and maintaining various user-space services.

Check the systemd process on CentOS 7

> ps aux | grep systemd | grep -v grep root 1 191412 3536 0.0 0.0? Ss Mar19 21:14 /usr/lib/systemd/systemd --switched -- root --system --deserialize 21 root 452 0.0 0.1 98664 46160? Ss Mar19 19:42 /usr/lib/systemd/systemd-journald root 475 0.0 0.0 43944 1000? Ss Mar19 0:00 /usr/lib/systemd/systemd-udevd dbus 629 0.0 0.0 54080 1584? Ss Mar19 20:35 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation root 643 0.0 0.0 24692 1508? Ss Mar19 10:01 /usr/lib/systemd/systemd-logindCopy the code

Note that on MaxOS, there is no Systemd and instead launchctl.

Systemd Service unit

Systemd has 11 cell types: Service, Socket, Device, mount point, Automount, Target, Swap partition or swap file, Monitored path, Task plan, Resource control group (Slice), a set of externally created processes (scope).

The service unit is a.service unit file that encapsulates a process monitored and controlled by Systemd.

The Unit file for a Service consists of three parts: [Service], [Unit], and [Install].

The FRP compressed package downloaded from GitHub contains unit files and unit template files of FRPC and FRPS services. We will explain how to manage FRPS services based on unit files and unit template files respectively (FRPC services are similar).

Manage FRPS services based on cell files

We use FRPS unit file (frps.service) to manage the FRPS service.

Let’s look at the contents of the frps.service file:

[Unit]
Description=Frp Server Service # Service Description
After=network.target # FRPS will start after network.service has been started

[Service]
Type=simple Systemctl start is successfully executed regardless of whether the process is successfully started
User=nobody Set the user that the process will use when executing
Restart=on-failure # on-failure means restart only if the service process exits unexpectedly
RestartSec=5s # Set how long to pause before restarting the service
ExecStart=/usr/bin/frps -c /etc/frp/frps.ini The command line that needs to be executed to start the service

[Install]
WantedBy=multi-user.target Create a soft connection when systemctl enable is enabled
Copy the code

Copy this file to /usr/lib/systemd/system/ :

sudo cp /path/to/frps.service /usr/lib/systemd/system/
Copy the code

Start the FRPS:

sudo systemctl start frps
Copy the code

View the FRPS service status:

> sudo systemctl start FRPS low FRPS. Service - Frp Server service the Loaded: the Loaded (/ usr/lib/systemd/system/FRPS. Service; enabled; vendor preset: disabled) Active: active (running) since Sat 2021-06-12 01:37:46 CST; 1s ago Main PID: 23430 (FRPS) Tasks: 5 Memory: 8.5m CGroup: / system. Slice/FRPS. Service └ ─ 23430 / usr/bin/FRPS - c/etc/FRP/FRPS. Ini Jun 12 01:37:46 iZwz93g2xezuhc0vzfgsy6Z systemd[1]: Started Frp Server Service. Jun 12 01:37:46 iZwz93g2xezuhc0vzfgsy6Z frps[23430]: 2021/06/12 01:37:46 [I] [root.go:200] frps uses config file: /etc/frp/frps.ini Jun 12 01:37:46 iZwz93g2xezuhc0vzfgsy6Z frps[23430]: 2021/06/12 01:37:46 [I] [service.go:192] FRPS TCP Listen on 0.0.0.0:7000 Jun 12 01:37:46 iZwz93g2xezuhc0vzfgsy6Z frps[23430]: 2021/06/12 01:37:46 [I] [root.go:209] frps started successfullyCopy the code

Stop FRPS:

sudo systemctl stop frps
Copy the code

Start the FRPS service automatically upon startup:

> sudo systemctl enable frps
Created symlink from /etc/systemd/system/multi-user.target.wants/frps.service to /usr/lib/systemd/system/frps.service.
Copy the code

Disable the FRPS service startup:

> sudo systemctl disable frps
Removed symlink /etc/systemd/system/multi-user.target.wants/frps.service.
Copy the code

Manage FRPS services based on unit template files

The FRPS unit template file is [email protected], with an extra @ symbol compared to the unit file. We can instantiate a frps@test service based on the unit template file by adding a parameter between the @ sign and the.service file suffix (which will be used to set some substitutions, such as % I), such as [email protected].

Let’s take a look at [email protected] first:

[Unit]
Description=Frp Server Service
After=network.target

[Service]
Type=simple
User=nobody
Restart=on-failure
RestartSec=5s
ExecStart=/usr/bin/frps -c /etc/frp/%i.ini

[Install]
WantedBy=multi-user.target
Copy the code

As you can see, the name of the configuration file used by FRPS in ExecStart uses the substitution % I. The value of this % I is between the @ symbol and the suffix of the.service file, such as [email protected]. Then % I equals test. The FRPS configuration file becomes /etc/frp/test.ini, so that different configuration files can be selected for different instances.

Also, we need to copy the [email protected] file to /usr/lib/systemd/system/ :

sudo cp /path/to/[email protected] /usr/lib/systemd/system/
Copy the code

If we need to instantiate (start) a service based on the [email protected] unit template file, such as [email protected], we just need to execute the systemctl start command, This unit will be based on template file to instantiate a service (we don’t need to have a unit file/usr/lib/systemd/system/[email protected]) :

sudo systemctl start frps@test
Copy the code

Check the status of the frps@test service:

> sudo systemctl status frps@testLow [email protected] - Frp Server Service the Loaded: the Loaded (/ usr/lib/systemd/system/FRPS @. Service; disabled; vendor preset: disabled) Active: active (running) since Sat 2021-06-12 02:02:51 CST; 5s ago Main PID: 31171 (frps) CGroup: /system.slice/ system.frps. slice/[email protected] ├ ─ 088 /usr/bin/frps. c/ etc/frp/test.ini Jun 12 02:02:51 iZwz93g2xezuhc0vzfgsy6Z systemd[1]: Started Frp Server Service. Jun 12 02:02:51 iZwz93g2xezuhc0vzfgsy6Z frps[31171]: 2021/06/12 02:02:51 [I] [root.go:200] frps uses config file: /etc/frp/test.ini Jun 12 02:02:51 iZwz93g2xezuhc0vzfgsy6Z frps[31171]: 2021/06/12 02:02:51 [I] [service.go:192] FRPS TCP Listen on 0.0.0.0:7000 Jun 12 02:02:51 iZwz93g2xezuhc0vzfgsy6Z frps[31171]: 2021/06/12 02:02:51 [I] [root.go:209] frps started successfullyCopy the code

Stop frps@test service:

sudo systemctl stop frps@test
Copy the code

Start frps@test service startup (pointing to unit template file) :

> sudo systemctl enable frps@test 
Created symlink from /etc/systemd/system/multi-user.target.wants/[email protected] to /usr/lib/systemd/system/[email protected].
Copy the code

Disable frps@test service startup:

> sudo systemctl disable frps@test
Removed symlink /etc/systemd/system/multi-user.target.wants/[email protected].
Copy the code

Personal blog

frp-with-systemd/