TorqueWrench/SSH_TUNNEL_README.md

253 lines
4.7 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# SSH反向隧道配置指南
## 问题分析
错误信息 `sh: ssh -fN -R 2222:localhost:22 root@123.56.98.4: command not found` 表示:
1. **SSH客户端未安装** - 系统找不到 `ssh` 命令
2. 需要先安装 `openssh-client``openssh-clients`
## 快速解决方案
### 步骤1安装SSH客户端
**CentOS/RHEL系统**
```bash
sudo yum install -y openssh-clients openssh-server
```
**Debian/Ubuntu系统**
```bash
sudo apt-get update
sudo apt-get install -y openssh-client openssh-server
```
**Arch Linux**
```bash
sudo pacman -S openssh
```
### 步骤2验证安装
```bash
# 检查SSH是否安装成功
ssh -V
# 应该显示类似OpenSSH_8.0p1, OpenSSL 1.1.1g
```
### 步骤3建立SSH反向隧道
```bash
# 基本命令
ssh -fN -R 2222:localhost:22 root@123.56.98.4 -p 22
# 参数说明:
# -f: 后台运行
# -N: 不执行远程命令
# -R: 反向隧道
# 2222: 阿里云服务器上的端口
# localhost:22: 本地SSH端口
# root@123.56.98.4: 阿里云服务器地址
# -p 22: SSH端口
```
### 步骤4测试连接
在**任何能访问阿里云服务器的机器**上:
```bash
# 先SSH到阿里云服务器
ssh root@123.56.98.4
# 然后在阿里云服务器上连接内网服务器
ssh -p 2222 root@localhost
```
## 使用提供的脚本(推荐)
### 方法1使用自动设置脚本
```bash
# 1. 下载并运行设置脚本
chmod +x ssh_tunnel_setup.sh
./ssh_tunnel_setup.sh
# 脚本会自动:
# - 检测并安装SSH客户端
# - 配置SSH服务
# - 生成SSH密钥
# - 测试连接
```
### 方法2使用自动重连脚本
```bash
# 1. 编辑 ssh_tunnel_auto.sh修改配置参数
vim ssh_tunnel_auto.sh
# 2. 运行自动重连脚本(前台运行)
chmod +x ssh_tunnel_auto.sh
./ssh_tunnel_auto.sh
# 或者后台运行
nohup ./ssh_tunnel_auto.sh > /dev/null 2>&1 &
```
### 方法3配置为系统服务开机自启
```bash
# 1. 使用root权限运行
sudo chmod +x ssh_tunnel_service.sh
sudo ./ssh_tunnel_service.sh
# 2. 服务会自动:
# - 创建systemd服务
# - 设置开机自启
# - 自动重连
```
## 常见问题排查
### 1. SSH命令找不到
**问题:** `command not found: ssh`
**解决:**
```bash
# 检查是否安装
which ssh
# 如果为空安装SSH客户端
# CentOS/RHEL:
sudo yum install openssh-clients
# Ubuntu/Debian:
sudo apt-get install openssh-client
```
### 2. 连接被拒绝
**问题:** `Connection refused`
**可能原因:**
- 阿里云服务器防火墙未开放22端口
- SSH服务未启动
- 用户名或密码错误
**解决:**
```bash
# 检查SSH服务状态
sudo systemctl status sshd
# 启动SSH服务
sudo systemctl start sshd
sudo systemctl enable sshd
# 检查防火墙
sudo firewall-cmd --list-ports # CentOS
sudo ufw status # Ubuntu
```
### 3. 隧道建立后无法连接
**问题:** 在阿里云服务器上 `ssh -p 2222 root@localhost` 失败
**检查:**
```bash
# 1. 检查隧道是否建立
ps aux | grep ssh | grep 2222
# 2. 检查端口是否监听(在阿里云服务器上)
netstat -tlnp | grep 2222
# 或
ss -tlnp | grep 2222
# 3. 检查阿里云服务器SSH配置
sudo vim /etc/ssh/sshd_config
# 确保有GatewayPorts yes
sudo systemctl restart sshd
```
### 4. 连接频繁断开
**解决:** 使用自动重连脚本,或添加保活参数:
```bash
ssh -fN -R 2222:localhost:22 \
-o ServerAliveInterval=30 \
-o ServerAliveCountMax=3 \
root@123.56.98.4 -p 22
```
## 安全建议
1. **使用SSH密钥认证**(推荐)
```bash
# 生成密钥对
ssh-keygen -t rsa -b 2048
# 复制公钥到阿里云服务器
ssh-copy-id root@123.56.98.4
```
2. **修改SSH端口**(可选)
```bash
# 编辑 /etc/ssh/sshd_config
sudo vim /etc/ssh/sshd_config
# 修改Port 2222
# 重启服务
sudo systemctl restart sshd
```
3. **禁用密码登录**(使用密钥后)
```bash
# 编辑 /etc/ssh/sshd_config
PasswordAuthentication no
# 重启服务
sudo systemctl restart sshd
```
## 验证连接
### 在阿里云服务器上验证
```bash
# 1. 检查端口监听
ss -tlnp | grep 2222
# 2. 测试连接
ssh -p 2222 root@localhost
# 3. 查看隧道进程
ps aux | grep "ssh.*-R.*2222"
```
### 从外部访问
```bash
# 1. SSH到阿里云服务器
ssh root@123.56.98.4
# 2. 通过隧道连接内网服务器
ssh -p 2222 root@localhost
```
## 文件说明
- `ssh_tunnel_setup.sh` - 自动设置脚本安装SSH、配置密钥等
- `ssh_tunnel_auto.sh` - 自动重连脚本(监控并自动重连)
- `ssh_tunnel_service.sh` - 创建systemd服务开机自启
- `SSH_TUNNEL_README.md` - 本文档
## 下一步
1. 运行 `ssh_tunnel_setup.sh` 完成初始设置
2. 运行 `ssh_tunnel_auto.sh` 建立隧道
3. 或运行 `ssh_tunnel_service.sh` 配置为系统服务
如有问题,请检查日志或联系技术支持。