配置system服务

发布于2025-07-08
在 Ubuntu 22.04 上设置 FRP (Frps/Frpc) 开机自启动可以通过 systemd 实现,以下是详细步骤:

方法一:使用 systemd 服务(推荐)

1. 创建 systemd 服务文件

shell

sudo nano /etc/systemd/system/frps.service # 服务端用frps # 或 sudo nano /etc/systemd/system/frpc.service # 客户端用frpc
Shell

2. 写入服务配置

服务端 (frps) 示例

plain

[Unit] Description=Frp Server Service After=network.target [Service] Type=simple User=nobody Restart=on-failure RestartSec=5s ExecStart=/path/to/frps -c /path/to/frps.ini # 修改为实际路径 WorkingDirectory=/path/to/frps_directory # FRP所在目录 [Install] WantedBy=multi-user.target
Plain text
客户端 (frpc) 示例

plain

[Unit] Description=Frp Client Service After=network.target [Service] Type=simple User=nobody Restart=on-failure RestartSec=5s ExecStart=/path/to/frpc -c /path/to/frpc.ini # 修改为实际路径 WorkingDirectory=/path/to/frpc_directory [Install] WantedBy=multi-user.target
Plain text

3. 设置权限并启用服务

shell

sudo chmod 644 /etc/systemd/system/frps.service # 或frpc.service sudo systemctl daemon-reload sudo systemctl enable frps # 或frpc sudo systemctl start frps # 立即启动
Shell

4. 验证状态

shell

sudo systemctl status frps # 查看状态 journalctl -u frps -f # 查看实时日志
Shell

方法二:使用 crontab(备用方案)

如果无法使用 systemd,可通过 crontab 实现:

shell

crontab -e
Shell
添加以下行(以 frpc 为例):

shell

@reboot /path/to/frpc -c /path/to/frpc.ini > /tmp/frpc.log 2>&1 &
Shell

关键注意事项

  1. 路径修正
      • 确保 ExecStart 中的路径指向真实的 FRP 二进制文件和配置文件
      • 可通过 which frpsfind / -name frps 查找路径
  1. 权限问题
      • 如果使用非标准端口(<1024),需要修改 User=root
      • 建议使用普通用户时绑定高位端口(如 8080+)
  1. 环境变量: 如果 FRP 需要特定环境变量,在 [Service] 部分添加:
    1. plain

      Environment="KEY=value"
      Plain text
  1. 多实例配置: 如需运行多个 FRP 实例,创建多个 service 文件如 frps2.service 并指定不同配置

故障排查命令

shell

# 检查服务是否启用 systemctl is-enabled frps # 测试配置文件 /path/to/frps -c /path/to/frps.ini --verify # 查看完整日志 journalctl -u frps --no-pager -n 50
Shell

卸载自启动

shell

sudo systemctl disable frps sudo systemctl stop frps sudo rm /etc/systemd/system/frps.service
Shell
通过以上配置,FRP 将在系统启动时自动运行,并在崩溃时自动重启(得益于 Restart=on-failure)。建议优先使用 systemd 方案,它能提供更好的进程管理和日志集成。
 
 
中间遇到了问题:
启动报错:frps.service: Changing to the requested working directory failed: Permission denied,我设置的工作目录是/home/win10/code/auto-login/frp
修改 frps.service,明确指定运行用户为目录所有者 win10
仍然报错
仍然报错:frps.service: Failed to determine user credentials: No such process 4月 30 10:04:48 win10-KVM systemd[2441815]: frps.service: Failed at step USER spawning
 
最后的配置
 

bash

[Unit] Description=Frp Client Service After=network.target [Service] Type=simple User=root Group=root Restart=on-failure RestartSec=5s ExecStart=/home/win10/code/auto-login/frp/frpc -c frpc.toml WorkingDirectory=/home/win10/code/auto-login/frp Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" Environment="LD_LIBRARY_PATH=/usr/local/lib" [Install] WantedBy=multi-user.target
Bash
 
 
 
 
 
 
 
 
IT远程软件开发就业市场现状分析与应聘建议基于PostgreSQL和NetworkX的人物关系图系统设计
Loading...
©2021-2025 Arterning.
All rights reserved.