[GH-ISSUE #176] frp怎样开机启动和后台运行? #118

Closed
opened 2026-05-05 11:47:43 -06:00 by gitea-mirror · 13 comments
Owner

Originally created by @piiiiiiiiiiiiiii on GitHub (Dec 14, 2016).
Original GitHub issue: https://github.com/fatedier/frp/issues/176

两个问题:
1.在centos上如何开机运行?
2.怎样后台运行,而不用一直开着putty

谢谢


已经解决:

二更@20210519
现在下载的frp打包文件目录内,已经写好了后台运行启动文件,比如服务器端,只需要把frps.service复制一份到/etc/systemd/system目录下即可。

如果想自己手写,参照如下:

使用systemd配置开机自启,适用于 centos7 Ubuntu 16 或 debian 8。

vi /etc/systemd/system/frps.service 新建此文件,并写入以下内容
:
[Unit]
Description=frps daemon

[Service]
Type=simple
ExecStart=/usr/bin/frps -c /etc/frps/frps.ini

[Install]
WantedBy=multi-user.target

启动并设为开机自启。
$ systemctl start frps
$ systemctl enable frps

参照:lcbk.net/9766.html

------------centos6.5及以下---------------------
vi /etc/rc.local
在最下面加一行/usr/sbin/frp/frps -c /usr/sbin/frp/frps.ini
其中 /usr/sbin/frp是程序放置的目录,重启ok

Originally created by @piiiiiiiiiiiiiii on GitHub (Dec 14, 2016). Original GitHub issue: https://github.com/fatedier/frp/issues/176 两个问题: 1.在centos上如何开机运行? 2.怎样后台运行,而不用一直开着putty 谢谢 --------------------------------------------- 已经解决: 二更@20210519 现在下载的frp打包文件目录内,已经写好了后台运行启动文件,比如服务器端,只需要把frps.service复制一份到/etc/systemd/system目录下即可。 如果想自己手写,参照如下: --------------------------------------------- 使用systemd配置开机自启,适用于 centos7 Ubuntu 16 或 debian 8。 vi /etc/systemd/system/frps.service 新建此文件,并写入以下内容 : [Unit] Description=frps daemon [Service] Type=simple ExecStart=/usr/bin/frps -c /etc/frps/frps.ini [Install] WantedBy=multi-user.target 启动并设为开机自启。 $ systemctl start frps $ systemctl enable frps 参照:lcbk.net/9766.html ------------centos6.5及以下--------------------- vi /etc/rc.local 在最下面加一行/usr/sbin/frp/frps -c /usr/sbin/frp/frps.ini 其中 /usr/sbin/frp是程序放置的目录,重启ok
Author
Owner

@lnzoro commented on GitHub (Dec 14, 2016):

nohup 后台可以用

<!-- gh-comment-id:266956783 --> @lnzoro commented on GitHub (Dec 14, 2016): nohup 后台可以用
Author
Owner

@wxyzh commented on GitHub (Dec 14, 2016):

supervisor
rc.local
systemd
nohup

<!-- gh-comment-id:266956805 --> @wxyzh commented on GitHub (Dec 14, 2016): supervisor rc.local systemd nohup
Author
Owner

@piiiiiiiiiiiiiii commented on GitHub (Dec 14, 2016):

楼上能详细说说吗? @lnzoro @wxyzh

<!-- gh-comment-id:266957640 --> @piiiiiiiiiiiiiii commented on GitHub (Dec 14, 2016): 楼上能详细说说吗? @lnzoro @wxyzh
Author
Owner

@fatedier commented on GitHub (Dec 14, 2016):

@impig33 建议这一类的问题可以通过搜索引擎自行解决。

<!-- gh-comment-id:266958718 --> @fatedier commented on GitHub (Dec 14, 2016): @impig33 建议这一类的问题可以通过搜索引擎自行解决。
Author
Owner

@piiiiiiiiiiiiiii commented on GitHub (Dec 14, 2016):

二更@20210519
现在下载的frp打包文件目录内,已经写好了后台运行启动文件,比如服务器端,只需要把frps.service复制一份到/etc/systemd/system目录下即可。

如果想自己手写,参照如下:

已经解决:
使用systemd配置开机自启,适用于 centos7 Ubuntu 16 或 debian 8。

vi /etc/systemd/system/frps.service 新建此文件,并写入以下内容
:
[Unit]
Description=frps daemon

[Service]
Type=simple
ExecStart=/usr/bin/frps -c /etc/frps/frps.ini

[Install]
WantedBy=multi-user.target

启动并设为开机自启。
$ systemctl start frps
$ systemctl enable frps

参照:lcbk.net/9766.html

------------centos6.5及以下---------------------
vi /etc/rc.local
在最下面加一行/usr/sbin/frp/frps -c /usr/sbin/frp/frps.ini
其中 /usr/sbin/frp是程序放置的目录,重启ok

.

<!-- gh-comment-id:266959497 --> @piiiiiiiiiiiiiii commented on GitHub (Dec 14, 2016): 二更@20210519 现在下载的frp打包文件目录内,已经写好了后台运行启动文件,比如服务器端,只需要把frps.service复制一份到/etc/systemd/system目录下即可。 如果想自己手写,参照如下: ---------------------------------------- 已经解决: 使用systemd配置开机自启,适用于 centos7 Ubuntu 16 或 debian 8。 vi /etc/systemd/system/frps.service 新建此文件,并写入以下内容 : [Unit] Description=frps daemon [Service] Type=simple ExecStart=/usr/bin/frps -c /etc/frps/frps.ini [Install] WantedBy=multi-user.target 启动并设为开机自启。 $ systemctl start frps $ systemctl enable frps 参照:lcbk.net/9766.html ------------centos6.5及以下--------------------- vi /etc/rc.local 在最下面加一行/usr/sbin/frp/frps -c /usr/sbin/frp/frps.ini 其中 /usr/sbin/frp是程序放置的目录,重启ok .
Author
Owner

@piiiiiiiiiiiiiii commented on GitHub (Dec 14, 2016):

太好用了,使用特权模式服务端免配置,太好用了,谢谢作者。

<!-- gh-comment-id:266968235 --> @piiiiiiiiiiiiiii commented on GitHub (Dec 14, 2016): 太好用了,使用特权模式服务端免配置,太好用了,谢谢作者。
Author
Owner

@vc5 commented on GitHub (Jun 2, 2017):

贴一下我之前无法开机自启frpc的解决办法,原因可能是因为系统开机并未准备好网络,或者其它依赖

sudo vim /etc/systemd/system/frpc.service

按如下修改

[Unit]
Description=frpc daemon
After=syslog.target  network.target
Wants=network.target

[Service]
Type=simple
ExecStart=/usr/sbin/frp/frpc -c /etc/frp/frpc.ini
Restart= always
RestartSec=1min
ExecStop=/usr/bin/killall frpc


[Install]
WantedBy=multi-user.target

使用sudo systemctl enable frpc.service启用

<!-- gh-comment-id:305726147 --> @vc5 commented on GitHub (Jun 2, 2017): 贴一下我之前无法开机自启frpc的解决办法,原因可能是因为系统开机并未准备好网络,或者其它依赖 `sudo vim /etc/systemd/system/frpc.service` 按如下修改 ``` [Unit] Description=frpc daemon After=syslog.target network.target Wants=network.target [Service] Type=simple ExecStart=/usr/sbin/frp/frpc -c /etc/frp/frpc.ini Restart= always RestartSec=1min ExecStop=/usr/bin/killall frpc [Install] WantedBy=multi-user.target ``` 使用`sudo systemctl enable frpc.service`启用
Author
Owner

@fatedier commented on GitHub (Jun 2, 2017):

@vc5 👍

也可以试试 0.11.0 frpc 新增的 login_fail_exit 参数设置为 false,这样启动时没连上服务器就不会自动退出了,而是每隔30s自动重连。

<!-- gh-comment-id:305791103 --> @fatedier commented on GitHub (Jun 2, 2017): @vc5 :+1: 也可以试试 0.11.0 frpc 新增的 login_fail_exit 参数设置为 false,这样启动时没连上服务器就不会自动退出了,而是每隔30s自动重连。
Author
Owner

@leke2015 commented on GitHub (Jul 8, 2017):

http://free-e.net/109 这篇文章能帮助到您。

<!-- gh-comment-id:313866354 --> @leke2015 commented on GitHub (Jul 8, 2017): http://free-e.net/109 这篇文章能帮助到您。
Author
Owner

@the-eric-kwok commented on GitHub (Dec 7, 2017):

If you don't have systemd or your kernel don't support systemd, this is my init.d service file:

#!/bin/sh

### BEGIN INIT INFO
# Provides:          frpc
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: frpc service deamon
# Description:       frpc service daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/frpc
DAEMON_OPTS=-c\ /etc/frp/frpc.ini
NAME=frpc
USER=eric

set -e
. /lib/lsb/init-functions

start() {
        echo -n "Starting $NAME: "
        start-stop-daemon --start \
                --user $USER \
                -m --pidfile /var/run/$NAME.pid \
                -b --exec $DAEMON -- $DAEMON_OPTS || true
        echo "$NAME."
}

stop() {
        echo -n "Stopping $NAME: "
        start-stop-daemon --stop \
                --pidfile /var/run/$NAME.pid || true
        echo "$NAME."
}

status() {
        status_of_proc -p /var/run/$NAME.pid "$DAEMON" $NAME && exit 0 || exit $?
}

case "$1" in
start)
  start
        ;;
stop)
  stop
        ;;
status)
  status
        ;;
restart)
  stop
  sleep 2
  start
        ;;
*)
        echo "Usage: $NAME {start|stop|restart|status}"
        exit 1
        ;;
esac
exit 0

Hope this can help you.
Oh, by the way, you should put the binary into /usr/bin and put the config file into /etc/frp/

<!-- gh-comment-id:349880778 --> @the-eric-kwok commented on GitHub (Dec 7, 2017): If you don't have systemd or your kernel don't support systemd, this is my init.d service file: ``` #!/bin/sh ### BEGIN INIT INFO # Provides: frpc # Required-Start: $local_fs $network # Required-Stop: $local_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: frpc service deamon # Description: frpc service daemon ### END INIT INFO PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/usr/bin/frpc DAEMON_OPTS=-c\ /etc/frp/frpc.ini NAME=frpc USER=eric set -e . /lib/lsb/init-functions start() { echo -n "Starting $NAME: " start-stop-daemon --start \ --user $USER \ -m --pidfile /var/run/$NAME.pid \ -b --exec $DAEMON -- $DAEMON_OPTS || true echo "$NAME." } stop() { echo -n "Stopping $NAME: " start-stop-daemon --stop \ --pidfile /var/run/$NAME.pid || true echo "$NAME." } status() { status_of_proc -p /var/run/$NAME.pid "$DAEMON" $NAME && exit 0 || exit $? } case "$1" in start) start ;; stop) stop ;; status) status ;; restart) stop sleep 2 start ;; *) echo "Usage: $NAME {start|stop|restart|status}" exit 1 ;; esac exit 0 ``` Hope this can help you. Oh, by the way, **you should put the binary into `/usr/bin` and put the config file into `/etc/frp/`**
Author
Owner

@ihipop commented on GitHub (Aug 24, 2018):

This Version Allow run as nobody AND ports below 1024

/etc/systemd/system/frps.service

Systems that has AmbientCapabilities support (usually kernel >= linux4.3)

[Unit]
Description=FRP Server Daemon

[Service]
Type=simple
AmbientCapabilities=CAP_NET_BIND_SERVICE
ExecStart=/opt/bin/frps -c /opt/etc/frps.ini
Restart=always
RestartSec=2s
User=nobody
LimitNOFILE=infinity

[Install]
WantedBy=multi-user.target

Systems that without AmbientCapabilities support,You need apt install libcap2-bin then

[Unit]
Description=FRP Server Daemon

[Service]
Type=simple
ExecStartPre=-/usr/sbin/setcap cap_net_bind_service=+ep /opt/bin/frps
ExecStart=/opt/bin/frps -c /opt/etc/frps.ini
Restart=always
RestartSec=2s
User=nobody
PermissionsStartOnly=true
LimitNOFILE=infinity

[Install]
WantedBy=multi-user.target

/etc/systemd/system/frpc.service

[Unit]
Description=FRP Client Daemon
After=network.target
Wants=network.target

[Service]
Type=simple
ExecStart=/opt/bin/frpc -c /opt/etc/frpc.ini
Restart=always
RestartSec=20s
User=nobody
LimitNOFILE=infinity

[Install]
WantedBy=multi-user.target

Config

systemctl daemon-reload
systemctl enable frpc
systemctl status frpc
systemctl enable frps
systemctl status frps

Also Archived at https://gist.github.com/ihipop/4dc607caef7c874209521b10d18e35af

<!-- gh-comment-id:415742455 --> @ihipop commented on GitHub (Aug 24, 2018): # This Version Allow run as `nobody` AND ports below 1024 ## /etc/systemd/system/frps.service ### Systems that has [`AmbientCapabilities` support (usually kernel >= linux4.3) ](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=58319057b7847667f0c9585b9de0e8932b0fdb08) ``` [Unit] Description=FRP Server Daemon [Service] Type=simple AmbientCapabilities=CAP_NET_BIND_SERVICE ExecStart=/opt/bin/frps -c /opt/etc/frps.ini Restart=always RestartSec=2s User=nobody LimitNOFILE=infinity [Install] WantedBy=multi-user.target ``` ### Systems that without `AmbientCapabilities` support,You need `apt install libcap2-bin` then ``` [Unit] Description=FRP Server Daemon [Service] Type=simple ExecStartPre=-/usr/sbin/setcap cap_net_bind_service=+ep /opt/bin/frps ExecStart=/opt/bin/frps -c /opt/etc/frps.ini Restart=always RestartSec=2s User=nobody PermissionsStartOnly=true LimitNOFILE=infinity [Install] WantedBy=multi-user.target ``` ## /etc/systemd/system/frpc.service ``` [Unit] Description=FRP Client Daemon After=network.target Wants=network.target [Service] Type=simple ExecStart=/opt/bin/frpc -c /opt/etc/frpc.ini Restart=always RestartSec=20s User=nobody LimitNOFILE=infinity [Install] WantedBy=multi-user.target ``` # Config ```bash systemctl daemon-reload systemctl enable frpc systemctl status frpc systemctl enable frps systemctl status frps ``` Also Archived at https://gist.github.com/ihipop/4dc607caef7c874209521b10d18e35af
Author
Owner

@popy32 commented on GitHub (Feb 13, 2019):

centos 6.5 init.d script
centos 6.5 客户端环境下配置后台自启

inspire by
https://serverfault.com/a/869143
https://github.com/utobi/prometheus-rpm/blob/master/node_exporter/contrib/node_exporter.init

ensure binary in /usr/bin/frpc, and configure file in /etc/frp/frpc.ini
edit /etc/init.d/frpc and chkconfig add frpc

#!/bin/bash

# chkconfig: - 85 15
# # description: frp init script

RETVAL=0
PROG="frpc"
EXEC="/usr/bin/frpc"
LOCKFILE="/var/lock/subsys/$PROG"
OPTIONS="-c /etc/frp/frpc.ini"

# Source function library.
if [ -f /etc/rc.d/init.d/functions ]; then
  . /etc/rc.d/init.d/functions
else
  echo "/etc/rc.d/init.d/functions is not exists"
  exit 0
fi

start() {
  if [ -f $LOCKFILE ]
  then
    echo "$PROG is already running!"
  else
    echo -n "Starting $PROG: "
    #$EXEC $OPTIONS  &
    nohup $EXEC $OPTIONS >/dev/null 2>&1 &
    RETVAL=$?
    [ $RETVAL -eq 0 ] && touch $LOCKFILE && success || failure
    echo
    return $RETVAL
  fi
}

stop() {
  echo -n "Stopping $PROG: "
  killproc $EXEC
  RETVAL=$?
  [ $RETVAL -eq 0 ] && rm -r $LOCKFILE && success || failure
  echo
}

restart ()
{
  stop
  sleep 1
  start
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status $PROG
    ;;
  restart)
    restart
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
esac
exit $RETVAL

in server with ubuntu 16.04, systemd is available
cat /etc/systemd/system/frps.service

[Unit]
Description=Frps Service
After=network.target
Wants=network.target

[Service]
Type=simple
PIDFile=/var/run/frps.pid
ExecStart=/usr/sbin/frp/frps -c /etc/frp/frps.ini
Restart=on-failure

[Install]
WantedBy=multi-user.target
<!-- gh-comment-id:463099986 --> @popy32 commented on GitHub (Feb 13, 2019): centos 6.5 init.d script centos 6.5 客户端环境下配置后台自启 inspire by https://serverfault.com/a/869143 https://github.com/utobi/prometheus-rpm/blob/master/node_exporter/contrib/node_exporter.init ensure binary in `/usr/bin/frpc`, and configure file in `/etc/frp/frpc.ini` edit `/etc/init.d/frpc` and `chkconfig add frpc` ```bash #!/bin/bash # chkconfig: - 85 15 # # description: frp init script RETVAL=0 PROG="frpc" EXEC="/usr/bin/frpc" LOCKFILE="/var/lock/subsys/$PROG" OPTIONS="-c /etc/frp/frpc.ini" # Source function library. if [ -f /etc/rc.d/init.d/functions ]; then . /etc/rc.d/init.d/functions else echo "/etc/rc.d/init.d/functions is not exists" exit 0 fi start() { if [ -f $LOCKFILE ] then echo "$PROG is already running!" else echo -n "Starting $PROG: " #$EXEC $OPTIONS & nohup $EXEC $OPTIONS >/dev/null 2>&1 & RETVAL=$? [ $RETVAL -eq 0 ] && touch $LOCKFILE && success || failure echo return $RETVAL fi } stop() { echo -n "Stopping $PROG: " killproc $EXEC RETVAL=$? [ $RETVAL -eq 0 ] && rm -r $LOCKFILE && success || failure echo } restart () { stop sleep 1 start } case "$1" in start) start ;; stop) stop ;; status) status $PROG ;; restart) restart ;; *) echo "Usage: $0 {start|stop|restart|status}" exit 1 esac exit $RETVAL ``` in server with ubuntu 16.04, systemd is available cat /etc/systemd/system/frps.service ```bash [Unit] Description=Frps Service After=network.target Wants=network.target [Service] Type=simple PIDFile=/var/run/frps.pid ExecStart=/usr/sbin/frp/frps -c /etc/frp/frps.ini Restart=on-failure [Install] WantedBy=multi-user.target ```
Author
Owner

@qiushile commented on GitHub (Nov 17, 2022):

贴一下我之前无法开机自启frpc的解决办法,原因可能是因为系统开机并未准备好网络,或者其它依赖

sudo vim /etc/systemd/system/frpc.service

按如下修改

[Unit]
Description=frpc daemon
After=syslog.target  network.target
Wants=network.target

[Service]
Type=simple
ExecStart=/usr/sbin/frp/frpc -c /etc/frp/frpc.ini
Restart= always
RestartSec=1min
ExecStop=/usr/bin/killall frpc


[Install]
WantedBy=multi-user.target

使用sudo systemctl enable frpc.service启用

万分感谢!!!

<!-- gh-comment-id:1318575526 --> @qiushile commented on GitHub (Nov 17, 2022): > 贴一下我之前无法开机自启frpc的解决办法,原因可能是因为系统开机并未准备好网络,或者其它依赖 > > `sudo vim /etc/systemd/system/frpc.service` > > 按如下修改 > > ``` > [Unit] > Description=frpc daemon > After=syslog.target network.target > Wants=network.target > > [Service] > Type=simple > ExecStart=/usr/sbin/frp/frpc -c /etc/frp/frpc.ini > Restart= always > RestartSec=1min > ExecStop=/usr/bin/killall frpc > > > [Install] > WantedBy=multi-user.target > ``` > > 使用`sudo systemctl enable frpc.service`启用 万分感谢!!!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/frp#118
No description provided.