ANALYSIS
SSH 远程端口转发服务配置指南
🔧 配置步骤
创建 Systemd 服务文件
PRTCL // BASH
sudo nano /etc/systemd/system/ssh-tunnel.service服务文件配置
PRTCL // INI
[Unit]Description=SSH Remote Port Forward ServiceAfter=network.target
[Service]# 本地运行服务的用户User=your_local_user
# SSH 命令配置ExecStart=/usr/bin/ssh -N -R 22300:192.168.1.201:22300 username@8.154.81.6 \ -o "ExitOnForwardFailure=yes" \ -o "ServerAliveInterval=60" \ -o "ServerAliveCountMax=3"
# 服务运行参数Restart=alwaysRestartSec=10StandardOutput=journalStandardError=journal
[Install]WantedBy=multi-user.target📝 参数说明
-N: 仅建立端口转发,不执行远程命令-R: 设置远程端口转发ExitOnForwardFailure: 端口转发失败时立即退出ServerAliveInterval: 保活检测间隔(秒)ServerAliveCountMax: 最大重试次数
服务管理命令
PRTCL // BASH
# 重新加载配置sudo systemctl daemon-reload
# 启动服务sudo systemctl start ssh-tunnel
# 设置开机自启sudo systemctl enable ssh-tunnel
# 查看服务状态sudo systemctl status ssh-tunnel
# 实时查看日志journalctl -u ssh-tunnel -f📋 前置检查清单
- SSH 密钥配置
PRTCL // BASH
# 生成 SSH 密钥(如果没有)ssh-keygen -t ed25519 -C "your_email@example.com"
# 复制公钥到远程服务器ssh-copy-id username@8.154.81.6- 防火墙设置
PRTCL // BASH
# 在远程服务器上检查端口sudo netstat -tlnp | grep 22300
# 配置 UFW(如果使用)sudo ufw allow 22300/tcp🔍 故障排查
常见问题检查
| 问题 | 检查命令 |
|---|---|
| 服务状态 | systemctl status ssh-tunnel |
| 详细日志 | journalctl -u ssh-tunnel -n 50 |
| 端口占用 | netstat -tlnp | grep 22300 |
| SSH 连接 | ssh -v username@8.154.81.6 |
连接测试
PRTCL // BASH
# 本地测试curl http://localhost:22300
# 远程测试curl http://8.154.81.6:22300⚠️ 注意事项
- 确保 SSH 密钥已正确配置
- 检查远程服务器防火墙设置
- 验证本地服务用户权限
- 定期检查日志确保服务正常
- 建议配置备用端口或故障转移方案
🔒 安全建议
- 使用强密钥加密(ED25519/RSA 4096)
- 限制远程服务器上的端口访问范围
- 配置 SSH 密钥使用限制
- 定期更新系统和 SSH 服务
- 监控异常连接尝试
📚 参考资料
作者:EchoWang
小红书:汪多多是只猫
B 站:汪多多是只猫
公众号:汪多多是只猫
R P
Rhine Lab Pioneer Division
Auth_Verified: 2026.04.08
Auth_Verified: 2026.04.08
