当前位置: 首页 > news >正文

通过SSH转发端口

情景

存在一个Linux服务器,只开放22端口。通过ssh转发将服务转发到中转服务器。

命令

ssh -L 0.0.0.0:8888:localhost:80 liuyang@172.24.52.208 -p 22
nohup sudo autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -N -L 0.0.0.0:1022:localhost:22 test@172.22.9.250 -p 22 -i /home/ylifs/.ssh/id_ed25519 > /dev/null 2>&1 &

前面是中转服务器的ip+端口
后面是目标服务器的ip+端口

Auto SSH

指定密钥

sudo autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -N -L 0.0.0.0:1022:localhost:22 test@172.22.9.250 -p 22 -i /home/ylifs/.ssh/id_ed25519

指定密钥就是-i /home/ylifs/.ssh/id_ed25519
注意的是这里是客户端用私钥。

改为系统服务

sudo nano /etc/systemd/system/autossh-tunnel.service
[Unit]
Description=AutoSSH Tunnel for Port 1022->22
After=network.target
[Service]
User=test  # 指定运行用户(避免sudo)
ExecStart=/usr/bin/autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -N -L 0.0.0.0:1022:localhost:22 test@172.22.9.250 -p 22 -i /home/ylifs/.ssh/id_ed25519
Restart=always
RestartSec=10  # 失败后10秒重试
StandardOutput=syslog  # 日志输出到syslog
StandardError=syslog[Install]
WantedBy=multi-user.target

开机自启

sudo systemctl daemon-reload
sudo systemctl start autossh-tunnel
sudo systemctl enable autossh-tunnel  # 开机自启

查看日志

sudo journalctl -u autossh-tunnel -f  # 实时日志

停止并禁用服务

sudo systemctl stop autossh-tunnel      # 停止服务
sudo systemctl disable autossh-tunnel   # 禁用开机自启

删除文件

sudo rm /etc/systemd/system/autossh-tunnel.service
http://www.rkmt.cn/news/53168.html

相关文章:

  • CVPR 2024 目标检测!开放词汇
  • linux apache 解析php
  • linux apache 的日志
  • 权威发布:2025年度MES系统综合排名,聚焦实用功能与选型避坑指南
  • 2025年遗产继承咨询律师权威推荐榜单:遗产继承/婚姻诉讼/财产纠纷律师精选
  • 2025年11月合肥抗衰老公司排名情况
  • LINUX在线扩文件系统(/目录)
  • 18new - when
  • linux apache 服务器搭建
  • 2025杭州最好的留学中介是哪家公司
  • 2025年11月国内PMS酒店管理系统公司排行榜:智能化升级的十大优选方案
  • 687
  • 视频汇聚平台EasyCVR构筑新时代边防哨所的“智能视觉防线”
  • 从“人防”到“技防”:视频融合平台EasyCVR打造快递代收点智能视频监控网
  • 2025年塑料回收行业领军企业排名出炉,塑料回收供应商TOP企业引领行业技术新高度
  • 2025杭州最好的留学中介排名榜单
  • 2025办理出国留学机构哪家好
  • 21、LIKE 子句详解
  • 2025年国内锯床公司权威排名榜单:成都鸿远机械有限公司排名首位
  • 2025成都留学机构十大排名
  • show 语法
  • 罗氏线圈积分技术:从理论到工程的精确电流重构
  • linux android 搭建
  • AI 十大论文精讲(五):RAG——让大模型 “告别幻觉、实时更新” 的检索增强生成秘籍
  • 2025年耐高压硅胶线批发厂家权威推荐榜单:硅胶线哪家质量好/硅胶多芯线/陶瓷化硅胶线源头厂家精选
  • AI 十大论文精讲(三):RLHF 范式奠基 ——InstructGPT 如何让大模型 “听懂人话”
  • 【LVGL】圆弧部件
  • 2025年电机生产流水线实力厂家权威推荐:电机生产线/无刷电机自动生产线/电机自动化生产源头厂家精选
  • [题解]BYOI Round 1 T1~T2
  • 服务器接口调用微信小程序获取手机号接口报:The SSL connection could not be established, see inner exception.