autossh 配置开机启动

1 篇文章 0 订阅
1 篇文章 0 订阅

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

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
要在开机时自动启动autossh,你可以使用以下方法: 1. 使用systemd服务: - 创建一个名为`autossh.service`的服务文件,可以使用任何文本编辑器打开该文件。 - 在服务文件中添加以下内容: ``` [Unit] Description=autossh service After=network.target [Service] ExecStart=/usr/bin/autossh -M 0 -NR <remote_port>:localhost:<local_port> <remote_user>@<remote_host> Restart=always RestartSec=3 [Install] WantedBy=multi-user.target ``` - 替换`<remote_port>`、`<local_port>`、`<remote_user>`和`<remote_host>`为你的实际值。这将设置autossh将本地端口转发到远程主机。 - 将服务文件保存到`/etc/systemd/system/`目录下。 - 运行以下命令以启用服务: ``` sudo systemctl enable autossh.service sudo systemctl start autossh.service ``` 2. 使用rc.local脚本: - 打开`/etc/rc.local`文件,可以使用任何文本编辑器打开该文件。 - 在文件的末尾添加以下行(在`exit 0`之前): ``` /usr/bin/autossh -M 0 -NR <remote_port>:localhost:<local_port> <remote_user>@<remote_host> & ``` - 替换`<remote_port>`、`<local_port>`、`<remote_user>`和`<remote_host>`为你的实际值。 - 保存并关闭文件。 - 运行以下命令以确保`/etc/rc.local`文件可执行: ``` sudo chmod +x /etc/rc.local ``` 这些方法中的任何一种都可以使autossh开机时自动启动。请确保替换相关参数为你的实际值,并根据你的操作系统版本和配置进行适当的调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值