当前位置: 首页 > 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.jsqmd.com/news/43665/

相关文章:

  • Codeforces Round 1064题解
  • 2025 最新工业检测仪源头厂家推荐排行榜:粉尘气体 / 颗粒物 / 固体流量等设备靠谱企业精选固体流量/空间环境粉尘/便携式粉尘检测仪公司推荐
  • CVPR 2024 目标检测!开放词汇
  • 国标GB28181算法算力平台EasyGBS如何实现停车场监控系统的高效统一管理?
  • LiteFlow(规则引擎)
  • SCQL:安全协同查询平台的技术解析
  • linux apache 解析php
  • idea中maven转gradle
  • linux apache 版本
  • LINUX在线扩文件系统(/目录)
  • linux apache 服务器搭建
  • linux apache 域名配置
  • 2025济南留学中介十大排名
  • 2025杭州最好的留学中介是哪家公司
  • 2025成都留学机构十强名单最新
  • 2025年11月国内PMS酒店管理系统公司排行榜:智能化升级的十大优选方案
  • 荒原之梦考研数学:26考研最后冲刺阶段还要重点学习考研数学吗?
  • virtualbox新建centos虚拟机并配置双网卡
  • 687
  • 2025年预防性维护用热像仪厂家权威推荐榜单:手持测温热像仪/TiX560/TiX520红外热像仪/手机测温热像仪源头厂家精选
  • 荒原之梦考研数学 | 做选填题,就不要局限于所用的方法
  • 767
  • drf 查找
  • nacos集群安装
  • CF2172H - Shuffling Cards with Problem Solver 68!
  • 2025年福祉座椅定制厂家权威推荐榜单:轮椅升降平台/轮椅升降机/福祉车源头厂家精选
  • 如何通过 KubeSphere 构建云原生平台,加速金融科技业务创新
  • SQL学习:WITH RECURSIVE
  • 2025年温度传感器批发厂家权威推荐榜单:水温传感器/传感器/红外温度传感器源头厂家精选
  • 视频汇聚平台EasyCVR构筑新时代边防哨所的“智能视觉防线”