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服務腳本的相關信息。