autossh 的用法示例
时间: 2024-05-20 08:17:48 浏览: 14
autossh 是一个用于在不可靠的网络上建立安全的 SSH 连接的工具,它可以在连接中断时自动重连,以确保连接的持久性。
以下是一个典型的 autossh 命令示例:
autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -N -R 8080:localhost:80 user@remote-server
此命令将在远程服务器上建立一个 SSH 反向隧道,将本地主机的 8080 端口映射到远程服务器的 80 端口上。
-M 参数指定用于监控 SSH 连接的端口,值为 0 表示禁用此功能;-o 参数用于设置 SSH 客户端选项,此处设置断开检测间隔为 30 秒,最大重试次数为 3;-N 参数指定不执行远程命令;-R 参数指定反向隧道的配置信息,格式为 remote-port:local-host:local-port,其中 remote-port 是远程服务器的端口,local-host 是本地主机名或 IP 地址,local-port 是本地端口号。
当这个 SSH 连接意外中断时,autossh 会自动尝试重新连接,以确保连接的可靠性。
相关问题
autossh自启动
要实现autossh的自启动,你可以按照以下步骤进行操作:
1. 创建一个启动脚本:首先,你需要创建一个启动脚本,以便在系统启动时运行autossh。你可以使用任何文本编辑器来创建一个新的脚本文件,比如autossh_start.sh。
2. 编辑启动脚本:在启动脚本中,你需要指定autossh的命令和参数。例如,如果你要使用autossh来建立一个SSH隧道连接,可以在启动脚本中添加以下内容:
```bash
#!/bin/bash
autossh -M 0 -f -N -L <本地端口>:<目标主机>:<目标端口> <SSH用户名>@<目标主机IP>
```
请将 `<本地端口>`, `<目标主机>`, `<目标端口>`, `<SSH用户名>` 和 `<目标主机IP>` 替换为实际的值。
3. 保存并退出:完成编辑后,请保存并退出启动脚本。
4. 设置脚本权限:运行以下命令,以确保启动脚本具有执行权限:
```bash
chmod +x autossh_start.sh
```
5. 将脚本添加到启动项:将启动脚本添加到系统的启动项中,以便在系统启动时自动运行。具体的操作方式可能因操作系统而异。
- 对于基于 systemd 的系统(如Ubuntu 16.04+),你可以创建一个名为 `autossh.service` 的服务单元文件,并将其放置在 `/etc/systemd/system/` 目录下。在文件中添加以下内容:
```
[Unit]
Description=Autossh Service
After=network.target
[Service]
ExecStart=/path/to/autossh_start.sh
[Install]
WantedBy=default.target
```
请将 `/path/to/autossh_start.sh` 替换为实际的启动脚本路径。
然后,运行以下命令以重新加载 systemd 配置并启用服务:
```bash
sudo systemctl daemon-reload
sudo systemctl enable autossh.service
```
- 对于其他系统(如 CentOS、Red Hat、Debian 等),你可以将启动脚本的符号链接添加到 `/etc/rc.d/rc.local` 文件中。运行以下命令将启动脚本添加到 `rc.local` 文件中:
```bash
sudo ln -s /path/to/autossh_start.sh /etc/rc.d/rc.local
```
请将 `/path/to/autossh_start.sh` 替换为实际的启动脚本路径。
6. 重新启动系统:完成上述步骤后,重新启动系统,autossh应该会在系统启动时自动运行。
这样,autossh就会在系统启动时自动建立 SSH 隧道连接。记得根据你的实际需求和配置进行相应的调整。
开机autossh自启动
要在开机时自动启动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在开机时自动启动。请确保替换相关参数为你的实际值,并根据你的操作系统版本和配置进行适当的调整。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)