autossh一般用来做端口转发。有断线重连功能。以下贴出该autossh的systemctl服务文件
#file path /lib/systemd/system/autossh.service
[Unit]
Description=autossh
Wants=network-online.target
After=network-online.target
[Service]
Type=simple
Environment="AUTOSSH_GATETIME=0"
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu
ExecStart=/usr/bin/autossh -M 0 -p 1234 -NR 8888:localhost:22 -i /home/ubuntu/.ssh/id_rsa user@remoteip
[Install]
WantedBy=multi-user.target
AutoSSH can also be controlled via a couple of environmental variables. Those are useful if you want to run AutoSSH unattended via cron
, using shell scripts or during boot time with the help of systemd
services. The most used variable is probably AUTOSSH_GATETIME
:
AUTOSSH_GATETIME
How long ssh must be up before we consider it a successful connection. Default is 30 seconds. If set to 0, then this behaviour is disabled, and as well, autossh will retry even on failure of first attempt to run ssh.
Setting AUTOSSH_GATETIME
to 0 is most useful when running AutoSSH at boot time.
在树梅派3B+测试时发现,需要添加 User,Group,WorkingDirectory时可以作为服务正常工作
开机启动服务
sudo systemctl enable autossh.service #开机启动autossh服务
sudo systemctl start autossh.service #开始autossh服务
以上就是autossh服务脚本的相关信息。