ubuntu

Ubuntu SSH如何设置自动重连

小樊
43
2026-01-06 18:39:55
栏目: 智能运维

在Ubuntu系统中,要设置SSH自动重连,可以使用autossh工具。以下是设置步骤:

1. 安装Autossh

首先,确保你的Ubuntu系统已经安装了autossh。如果没有安装,可以使用以下命令进行安装:

sudo apt update
sudo apt install autossh

2. 配置SSH客户端

编辑或创建SSH配置文件~/.ssh/config,添加以下内容:

Host your_remote_host
    HostName your_remote_host_ip_or_domain
    User your_username
    Port your_ssh_port
    IdentityFile ~/.ssh/your_private_key
    ServerAliveInterval 60
    ServerAliveCountMax 3
    ProxyCommand autossh -M 0 -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -q -W %h:%p your_gateway_host

解释一下配置项:

3. 测试连接

保存配置文件后,尝试连接远程主机:

ssh your_remote_host

如果一切配置正确,你应该能够成功连接到远程主机,并且连接会在断开后自动重连。

4. 后台运行Autossh

如果你希望autossh在后台运行,可以使用nohup命令:

nohup autossh -M 0 -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -q -W %h:%p your_gateway_host &

这样,即使你关闭终端,autossh也会继续运行。

注意事项

通过以上步骤,你可以在Ubuntu系统中设置SSH自动重连。

0
看了该问题的人还看了