autossh 配置开机启动

本文介绍如何使用autossh进行端口转发,包括断线重连功能的配置。通过设置systemd服务文件,实现开机自动运行autossh,确保远程连接的稳定性。详细解释了环境变量AUTOSSH_GATETIME的作用及在不同场景下的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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服务脚本的相关信息。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值