Script to configure and manage frp service with one click
Introduction: This is a simple and practical Bash script that helps you configure and manage frp services with one click. With this script, you can easily create a service file for frp and use the systemctl command to start, stop, restart, and reload the frp service configuration file.
text:
Frp is a powerful reverse proxy tool for mapping external traffic to services on your internal network. However, there is no service management in the official installation package, which makes the operation and management of the program relatively troublesome. In order to simplify the configuration and management process of the frp service, a Bash script is used, which can automatically create the frp service file and provide some commonly used systemctl commands for managing the frp service.
prerequisite
- Your system needs to have the Bash shell installed.
- frp has been correctly installed in the system and the path has been set. The command is used in the script
find
to find the executable files and configuration files of frpc and frps, so you need to ensure that these files exist in the system. - appropriate permissions. Running the script requires sufficient permissions to create and modify system service files
/etc/systemd/system/frpc.service
and/etc/systemd/system/frps.service
. - Knowledge of systemd and systemctl commands. The script uses the systemctl command to start, stop, restart, and reload the frp service.
Please ensure that the above requirements are met before running the script, and use scripts with caution to configure and manage the frp service.
Quick to use
# 安装脚本
curl -O https://raw.githubusercontent.com/liuyuanchengweb/create_frp_service/main/create_frp_service.sh
# 或者
wget https://raw.githubusercontent.com/liuyuanchengweb/create_frp_service/main/create_frp_service.sh
# 或者
curl -O https://gitee.com/useryc/create_frp_service/raw/main/create_frp_service.sh
# 或者
wget https://gitee.com/useryc/create_frp_service/raw/main/create_frp_service.sh
# 给脚本添加执行权限
chmod +x create_frp_service.sh
# 执行脚本
./create_frp_service.sh
Script implementation
- First,
find
find specific files in the system through commands, such as the paths offrpc
,frpc.ini
,frps
andfrps.ini
, and store the results in the corresponding variables. frpc_service
Next, two functions and are definedfrps_service
, which are used to create configuration files offrpc.service
and respectivelyfrps.service
.- In
frpc_service
the function, first check whether the file already existsfrpc.service
, and if it exists, output a prompt message; if it does not exist, checkfrpc
whetherfrpc.ini
the file exists. If it exists, usecat
the command tofrpc.service
write the configuration content into the file/etc/systemd/system/frpc.service
, and output a prompt message indicating that the creation is successful. . - In
frps_service
the function, also check whetherfrps.service
the file already exists, and if it exists, output a prompt message; if it does not exist, check whether the file exists, andfrps
iffrps.ini
it exists, usecat
the command tofrps.service
write the configuration content into the file/etc/systemd/system/frps.service
, and output a prompt message indicating that the creation is successful. . - Finally, a function is defined
load_frp
that calls thefrpc_service
andfrps_service
function and then usessystemctl daemon-reload
the command to reload systemd's configuration file. Finally, the configuration completion and related command prompts are output.
Generally speaking, the implementation mechanism of this script is to create a corresponding service configuration file based on the existence of a specific file, and then manage the service through the systemd command.
Script code:
#! /bin/bash
frpc=$(find / -name "frpc")
frpc_ini=$(find / -name "frpc.ini")
frps=$(find / -name "frps")
frps_ini=$(find / -name "frps.ini")
frpc_service="/etc/systemd/system/frpc.service"
frps_service="/etc/systemd/system/frps.service"
frpc_service(){
if [ -f $frpc_service ];then
echo -e "frpc服务文件以存在$frpc_service"
else
if [ -f $frpc ] && [ -f $frpc_ini ];then
cat > /etc/systemd/system/frpc.service << EOF
[Unit]
Description=Frpc Service
After=network.target
[Service]
ExecStart=$frpc -c $frpc_ini
ExecReload=/bin/kill -HUP
Restart=always
[Install]
WantedBy=multi-user.target
EOF
echo -e "创建frpc.service文件成功,文件路径:$frpc_service"
else
echo -e "没有找到frpc程序的路径和frpc.ini文件的路径"
fi
fi
}
frps_service(){
if [ -f $frps_service ];then
echo -e "frps服务文件以存在$frps_service"
else
if [ -f $frps ] && [ -f $frps_ini ];then
cat > /etc/systemd/system/frps.service << EOF
[Unit]
Description=Frps Service
After=network.target
[Service]
ExecStart=$frps -c $frps_ini
ExecReload=/bin/kill -HUP
Restart=always
[Install]
WantedBy=multi-user.target
EOF
echo -e "创建frpc.service文件成功,文件路径:$frpc_service"
else
echo -e "没有找到frps程序的路径和frps.ini文件的路径"
fi
fi
}
load_frp(){
frpc_service
frps_service
systemctl daemon-reload
echo -e "配置frp服务完成"
echo -e "使用systemctl enable --now frpc.service 命令设置frpc开机自动启动并立即启动服务"
echo -e "使用systemctl status frpc.service 命令查看frpc服务状态信息"
echo -e "使用systemctl stop frpc.service 停止 frpc 服务"
echo -e "使用systemctl disable frpc.service 将 frpc 服务从开机自动启动中移除"
echo -e "使用systemctl restart frpc.service 将 frpc 服务重新启动"
echo -e "使用systemctl reload frpc.service 重新加载frpc的配置文件"
echo -e "使用systemctl enable --now frps.service 命令设置frps开机自动启动并立即启动服务"
echo -e "使用systemctl status frps.service 命令查看frps服务状态信息"
echo -e "使用systemctl stop frps.service 停止 frps 服务"
echo -e "使用systemctl disable frps.service 将 frps 服务从开机自动启动中移除"
echo -e "使用systemctl restart frps.service 将 frps 服务重新启动"
echo -e "使用systemctl reload frps.service 重新加载frps的配置文件"
}
load_frp
Instructions
Follow these steps to use:
-
Copy the above code to your own system to create an sh script file
-
Give the script execution permission
chmod +x sh脚本文件
-
Execute script
./sh脚本文件
-
Wait for the script execution to complete and output the corresponding results.
Precautions
When using this script, you need to pay attention to the following things:
-
Permissions: Make sure you run the script as a user with sufficient permissions to be able to create and modify system files.
-
File path: The script uses
find
commands to find the path of specific files. Please ensure that the relevant software has been correctly installed and these files exist in the system before using the script. If the file path is incorrect or the file does not exist, the script will not run properly. -
Script path: Save the script to a suitable location and give it executable permissions. You can modify the file path and service configuration information in the script as needed.
-
Conflict checking: When creating a service configuration file, the script checks whether the corresponding file already exists in the target path. If it already exists, the corresponding prompt message will be output. This avoids duplicating or overwriting existing configuration files.
-
systemd service management: Commands are used in the script
systemctl
to manage services, such as starting, stopping, reloading and setting automatic startup at boot. Make sure you have systemd installed correctly and understandsystemctl
how to use the commands. -
Note: The script outputs a series of command
systemctl
prompts at the end to remind users how to operate and manage the configured services. Please execute the corresponding command according to the prompt information.
Please note that before using the script, it is recommended to read the code of the script carefully and make appropriate modifications and adjustments according to the actual situation. Make sure you understand the function and operation mechanism of the script in order to use and manage the frp service correctly.