SSH Port Forwarding

ssh连接失败,排错经验 一、场景描述 ssh连接服务器,发现连接失败,但是对应服务器的ip能够ping通。 场景: [root@yl-web ~]# ssh root@10.1.101.35 ssh_exchange_identification: read: Connection reset by peer [root@yl-web ~]# ping 10.1.101.35 PING 10.1.101.... 阅读详情
 

Introduction

SSH is typically used for logging into remote servers so you have shell access to do maintenance, read your email, restart services, or whatever administration you require. SSH also offers some other native services, such as file copy (using scp and sftp) and remote command execution (using ssh with a command on the command line after the hostname).

Whenever we SSH from one machine to another, we establish a secure encrypted session. This first article in this SSH series[1] looked at properly verifying a server's host key, so that we can be sure that no attacker is able to perform a man-in-the-middle attack and gain access to read or manipulate what we do in that session. Other articles in this series looked at removing the need for static passwords using SSH user identities[2], and then using ssh-agent[3] to automate the task of typing passphrases.

SSH also has a wonderful feature called SSH Port Forwarding, sometimes called SSH Tunneling, which allows you to establish a secure SSH session and then tunnel arbitrary TCP connections through it. Tunnels can be created at any time, with almost no effort and no programming, which makes them very appealing. In this article we look at SSH Port Forwarding in detail, as it is a very useful but often misunderstood technology. SSH Port Forwarding can be used for secure communications in a myriad of different ways. Let's start with an example.

 

LocalForward Example

Say you have a mail client on your desktop, and currently use it to get your email from your mail server via POP, the Post Office Protocol, on port 110.[4] You may want to protect your POP connection for several reasons, such as keeping your password from going across the line in the clear[5], or just to make sure no one's sniffing the email you're downloading.

Normally, your mail client will establish a TCP connection to the mail server on port 110, supply your username and password, and download your email. You can try this yourself using telnet or nc on the command line:

 

 

  xahria@desktop$ nc mailserver 110
  +OK SuperDuper POP3 mail server (mailserver.my_isp.net) ready.
  USER xahria
  +OK
  PASS twinnies
  +OK User successfully logged on.
  LIST
  +OK 48 1420253
  1 1689
  2 1359
  3 59905
  ...
  47 3476
  48 3925
  .
  QUIT
  +OK SuperDuper POP3 mail server signing off.
  
  xahria@desktop$

 

We can wrap this TCP connection inside an SSH session using SSH Port Forwarding. If you have SSH access to the machine that offers your service (POP, port 110 in this case) then SSH to it. If you don't, you can SSH to a server on the same network if the network is trusted. (See the security implications of port forwarding later in this article.)

In this case, let's assume we don't have SSH access to the mail server, but we can log into a shell server on the same network, and create a tunnel for our cleartext POP connection:

 

 

  # first, show that nothing's listening on our local machine on port 9999:
  xahria@desktop$ nc localhost 9999
  Connection refused.
  
  xahria@desktop$ ssh -L 9999:mailserver:110 shellserver 
  xahria@shellserver's password: ********
  
  xahria@shellserver$ hostname
  shellserver

 

From a different window on your desktop machine, connect to your local machine (localhost) on port 9999:

 

 

  xahria@desktop$ nc localhost 9999
  +OK SuperDuper POP3 mail server (mailserver.my_isp.net) ready.
  USER xahria
  +OK
  PASS twinnies
  ...

 

Before we connected to the shellserver with SSH, nothing was listening on port 9999 on our desktop - once we'd logged in to the mail server with our tunnel, this port was bound by our SSH process, and the TCP connection to local port 9999 was magically tunneled through SSH to the other side.

Let's describe how this works in detail, using the example above.

 

  • You launch the /usr/bin/ssh SSH client on the command line.
  • The SSH client logs into the remote machine using whatever authentication method (password, Pubkey, etc) is available.
  • The SSH client binds the local port you specified, port 9999, on the loopback interface, 127.0.0.1.
  • You can do anything in your remote machine that you want -- tar up some files, write to some users, delete /etc/shadow... This interactive login is completely usable, or you can just let it hang around doing nothing.
  • When a process connects to 127.0.0.1 on port 9999 on the client machine, the /usr/bin/ssh client program accepts the conection.
  • The SSH client informs the server, over the encrypted channel, to create a connection to the destination, in this case mailserver port 110.
  • The client takes any bits sent to this port (9999), sends them to the server inside the encrypted SSH session, who decrypts them and then sends them in the clear to the destination, port 110 of the mailserver.
  • The server takes any bits received from the destination, mailserver's port 110, and sends them inside the SSH session back to the client, who decrypts and sends them in the clear to the process that connected to the client's bound port, port 9999.
  • When the connection is closed by either endpoint, it is torn down inside the SSH session as well.

 

SSH Port Forward Debugging

Let's see it in action by using the verbose option to ssh:

 

  xahria@desktop$ ssh -v -L 9999:mailserver:110 shellserver 
  debug1: Reading configuration data /etc/ssh/ssh_config
  debug1: Rhosts Authentication disabled, originating port will not be trusted.
  debug1: Connecting to shellserver [296.62.257.251] port 22.
  debug1: Connection established.
  debug1: identity file /home/bri/.ssh/identity type 0
  debug1: identity file /home/bri/.ssh/id_rsa type 1
  debug1: identity file /home/bri/.ssh/id_dsa type 2
  ...
  debug1: Next authentication method: password
  xahria@shellserver's password: ********
  debug1: Authentication succeeded (password).
  debug1: Connections to local port 9999 forwarded to remote address localhost:110
  debug1: Local forwarding listening on 127.0.0.1 port 9999.
  debug1: channel 0: new [client-session]
  debug1: Entering interactive session.
  debug1: channel 0: request pty-req
  debug1: channel 0: request shell
  xahria@shellserver$

As you can see, there's a brief mention of port 9999 being bound and available for tunneling. We haven't made a connection to this port yet, so no tunnel is active yet. You can use the ~# escape sequence to see the connections in use. This sequence only works after a carriage return, so hit enter a few times before trying it:

 

  xahria@shellserver$ (enter)
  xahria@shellserver$ (enter)
  xahria@shellserver$ (enter)
  xahria@shellserver$ ~#
  The following connections are open:
    #1 client-session (t4 r0 i0/0 o0/0 fd 5/6)
  xahria@shellserver$

You can see that there's only one connection, our actual SSH session from which we're typing those unix commands.

Now, in a different window if we do a telnet localhost 9999, we'll open up a new connection through the tunnel, and we can see it from our SSH session using ~#

 

  xahria@shellserver$ (enter)
  xahria@shellserver$ ~#
  The following connections are open:
    #1 client-session (t4 r0 i0/0 o0/0 fd 5/6)
    #2 direct-tcpip: listening port 9999 for mailserver port 110,
                     connect from 127.0.0.1 port 42789 (t4 r1 i0/0
                     o0/0 fd 8/8)

You can see that now we have both the SSH session we're using, plus a tunnel, the second entry. It tells you all you need to know about the connection -- it came from our local machine (127.0.0.1) source port 42789, which we could look up with netstat or lsof output if we were curious about it.

 

RemoteForward Example

SSH Forwards actually come in two flavours. The one I've shown above is a local forward, where the ssh client machine is listening for new connections to be tunneled. A Remote Forward is just the opposite - a tunnel initiated on the server side that goes back through the client machine.

The classic example of using a Remote Forward goes something like this. You're at work, and the VPN access is going to be down for maintenance for the weekend. However you really have some important work to do, but you'd rather work from the comfort of your desk at home, rather than being stuck at work all weekend. There's no way for you to SSH to your work desktop because it's behind the firewall.

Before you leave for the evening, you SSH from your work desktop back to your home network. Your ~/.ssh/config file has the following snippet:

 

  lainee@work$ tail ~/.ssh/config
    Host home-with-tunnel
       Hostname      204.225.288.29
       RemoteForward 2222:localhost:22
       User          laineeboo
  
  
  
  lainee@work$ ssh home-with-tunnel
  laineeboo@204.225.228.29's password: ********
  
  laineeboo@home$ ping -i 5 127.0.0.1
  PING 127.0.0.1 (127.0.0.1): 56 data bytes
  64 bytes from 127.0.0.1: icmp_seq=0 ttl=255 time=0.1 ms
  64 bytes from 127.0.0.1: icmp_seq=1 ttl=255 time=0.2 ms
  64 bytes from 127.0.0.1: icmp_seq=2 ttl=255 time=0.2 ms
  ...

We've set up a tunnel using the RemoteForward option in the SSH configuration file. (We could have set it up on the command line using the -R option if we'd prefered.) Just to make sure our firewall doesn't kill the connection for inactivity, we run a ping for grins. Then we head on home.

Later that evening, we can sit down on our home machine and see that we're logged in:

 

  laineeboo@home$ last -1
  laineeboo  pts/18   firewall.my_work.com Tue Nov 23 22:28   still logged in
  
  laineeboo@home$ ps -t pts/18
   PID TTY          TIME CMD
  3794 pts/18   00:00:00 ksh
  4027 pts/18   00:00:00 ping -i 5 127.0.0.1

Now comes the payoff - our tunnel is listening on our home machine on port 2222, and will be tunneled back through the corporate firewall to our work machine's port 22. So to SSH to work from home, since we have our tunnel ready, we simply point /usr/bin/ssh to port 2222:

 

  laineboo@home$ ssh -p 2222 lainee@localhost 
  lainee@localhost's password: ********
  
  lainee@work$

Success!

Port Forwarding Cheat Sheet

Remembering how to specify the kind of SSH Forward you want is sometimes tricky. Hopefully, the following table will make it a bit easier.

 

LocalForwards
Command Line Option-L local_listen_port:destination_host:destination_port
Configuration file entryLocalForward local_listen_port:destination_host:destination_port
local_listen_port is on SSH client loopback interface
destination_host is contacted from SSH server host

 

RemoteForwards
Command Line Option-R remote_listen_port:destination_host:destination_port
Configuration file entryRemoteForward remote_listen_port:destination_host:destination_port
remote_listen_port is on SSH server loopback interface
destination_host is contacted from SSH client host

Forwards can be confusing - we typically think of connections as being made up of four things - the local IP and port, and the remote IP and port. In the forward definition you create, you only have three things because the first port is always either the SSH client or server machine, and thus isn't specified.

 

Port Forward Security

Port forwards bind a port on either the ssh client (Local Forwards) or ssh server (Remote Forwards). With a default installation, the port will only be bound on the localhost interface, 127.0.0.1. This means that the tunnel is only available to someone on the machine where that port is listening.

In general, you don't want to allow other machines to contact your SSH tunnel so this is the correct setting. If you want to allow these ports to be available to any machine, then use one of the following:

 Command line optionConfiguration file option
LocalForwards-gGatewayPorts yes
(in ~/.ssh/config or /etc/ssh/ssh_config)
RemoteForwards(none available)GatewayPorts yes
(in /etc/sshd_config)

The other important thing you must remember is that the data connection is only encrypted inside the SSH connection. If your destination_host you specify is not localhost[6] then the portion of the connection that extends out of the tunnel is not encrypted. For example if you used the the following:

 

 

  desktop$ ssh -L 8080:www.example.com:80 somemachine

then any connection to localhost:8080 will be encrypted from your desktop through to somemachine, but it will be in cleartext from somemachine to www.example.com. If that fits your security model, no problem. But keep it in mind.

We'll see how you can put further limits on port forwards in a future article, such as rejecting or limiting them based on the SSH Pubkeys/Identity that is used for authentication.

 

NOTES:

 

[1] SSH Host Key Protection: http://www.securityfocus.com/infocus/1806

[2] SSH User Identities: http://www.securityfocus.com/infocus/1810

[3] SSH and ssh-agent: http://www.securityfocus.com/infocus/1812

[4] Some POP server software offers SSL-encrypted POP, by negotiating SSL using STARTTLS on port 110, or wrapped entirely in SSL on port 995. For this example, however, let's assume you have a non-SSL aware POP server.

[5] Some POP servers support alternate authentication methods, such as S/Key or challenge response, which can keep your password from going across the network.

[6] Unsniffable options would be localhost, 127.0.0.1 (or any 127/8 address on most unix-like systems) or any local IP address - these should all go through the local machine's TCP/IP stack without hitting the network card at all, and thus would be as secure as the network stack itself.

View more articles by Brian Hatch

SSH配置过程及无法正常链接问题的解决【小白教学】 出现下面的提示【错误】: Cloning into 'PythonRobotics'... fatal: unable to access 'https://github.com/mogualla/PythonRobotics.git/': SSL certificate problem: unable to get local issuer certificate 阅读详情

相关推荐

SSH端口转发失败?手把手教你解决remote port forwarding failed问题(附完整配置流程)

本文详细解析SSH远程端口转发(remote port forwarding)失败的常见原因及解决方案,提供完整的配置流程和关键参数解析。从SSH服务端配置、端口可用性检查到防火墙设置,手把手教你解决'remote port forwarding failed'问题,并分享保持连接稳定、多端口转发等高级技巧,帮助开发者轻松实现内网穿透。

weixin_33738555的博客 355

SSH连接错误

用xshell客户端连接主机出现错误: ssh 用户名@ip_address [port] 官方文档上的提示: "Could not connect to 'hostname' (port 22): Connection failed." Check if the host machine is ready for connection and the port

lucifer_凡 852

【通信原理 入坑之路】—— 详解IQ调制以及星座图原理

文章目录一. IQ调制与解调的原理与过程二.PSK(相移键控)和IQ调制的关系:2.1 用IQ调制实现QPSK调制 一. IQ调制与解调的原理与过程 IQ调制的定义是:“IQ调制就是数据分为两路,分别进行载波调制,两路载波相互正交。I是in-phase(同相), q是 quadrature(正交)” 我们通过下面的示意图介绍IQ调制的过程: 首先,信号分成两路:I路和Q路(为了方便理解我们把这两...

凝望,划过星空的博客 11万+

连接SSH失败的原因以及方法

一、检查用户名密码 连接失败时可以先检查验证信息,步骤如下: 1、运行软件,在会话管理中找到连接失败的会话。右键单击会话名,点击属性。 图1:查看属性 2、在弹出的对话框点击用户身份验证,看输入的信息和服务器上设置的用户名密码是否一致。如果输入的验证信息有误,重新输入后点击连接即可。 图2:检查验证信息 二、 检查服务器网络 先检查是否是网络的问题。按快捷键Win+R,在弹出的对话框中输入cmd。 图3:运行cmd命令 点击确定运行。在cmd窗口输入ping一下..

Zzzz_jjj的博客 2万+

实战:如何通过修改VTS和HTS调整GC4653 sensor帧率(附完整计算步骤)

本文以GC4653图像传感器为例,详细解析了通过修改VTS和HTS寄存器来精确调整输出帧率的完整流程。文章深入剖析了帧率计算公式FPS = PCLK / (VTS × HTS)的核心原理,并提供了从获取寄存器参数、计算目标VTS值到系统验证的实战步骤,帮助工程师掌握独立优化Sensor性能、提升低照度画质的关键技能。

gin88的博客 348

camera——HAL层知识分享

CameraHAL层是Android相机系统的核心中间层,负责连接Framework层与硬件驱动层。它通过标准化接口解耦软硬件,实现跨平台兼容性,同时为厂商提供硬件定制空间。主要功能包括:设备管理、数据流处理、参数控制以及图像优化。关键子系统涵盖预览、拍照、录像、对焦与曝光等模块,采用HAL3的"请求-响应"模型实现精细控制。性能优化重点关注启动时间、帧率稳定性和延迟控制。通过分层架构设计(接口层-核心层-适配层)和缓冲池机制,确保高效数据传输和硬件资源调度。

2302_77429403的博客 2097

SSH连接服务器:Could not connect to ‘xxx.xxx.xx.xx‘ (port 22): Connection failed.

Could not connect to 'xxx.xxx.xx.xx' (port 22): Connection failed

panlupeng的博客 7173

告别UI字体臃肿!详解TextMeshPro字体AssetBundle打包优化技巧(Unity 2022实测)

本文详细解析了Unity 2022中使用TextMeshPro时字体资源AssetBundle打包的优化技巧,特别针对中日文等多语言场景下的字体臃肿问题。通过公共资源标记、依赖关系分析和实战案例,帮助开发者有效减少包体体积和内存占用,提升游戏性能。

weixin_33725722的博客 421

本地端口转发Local Port Forwarding(SSH命令)

本地端口转发Local Port Forwarding(SSH命令)

RealTiffany的博客 399

【AI编程】Vibe Coding 开发环境搭建与项目实战操作详解

Vibe Coding 开发环境搭建与项目实战操作详解

congge 7360

Windows Docker Desktop部署MaxKB详细教程

MaxKB(Max Knowledge Base)是一款基于大语言模型(LLM)和检索增强生成(RAG)技术的开源知识库问答系统,旨在帮助企业、教育机构及研究组织高效管理知识并提供智能问答服务。

猿小白的博客 1380

ssh port forwarding

SSH: Port Forwarding 1.正向隧道-隧道监听本地port,为普通活动提供安全连接 ssh -qTfnN -L port:host:hostport -l user remote_ip 2.反向隧道----隧道监听远程port,突破防火墙提供服务 ssh

bbplayers的专栏 3770

十九、帧间预测编码_5、skip模式和direct模式

帧间预测的几种特殊模式: P_Skip: 即COPY模式,在该模式下无运动矢量残差,无像素预测残差。重建时以MVP作为实际运动矢量,以预测像素块作为重建; B_Skip:同P_Skip类似的是该模式下同样无运动矢量残差,无像素预测残差。重建时,以Direct模式获取双向MV,以预测像素块作为重建; B_Direct:B_Direct模式分为B_Direct_8x8和B_Direct_16x16,分别表示整个宏块和一个子块按照B_Direct模式编码。该模式下,无运动矢量残差,有像素预测残差。重建时,以D.

叮咚的博客 2404

ssh port forwarding(ssh端口转发)

SSH的的Port Forward,中文可以称为端口转发,是SSH的一项非常重要的功能。它可以建立一条安全的SSH通道,并把任意的TCP连接放到这条通道中。下面仔细就仔细讨论SSH的这种非常有用的功能。 SSH Tunnel有三种,分别是本地Local(ssh -NfL),远程Remote(ssh -NfR),动态Dynamic(ssh -NfD)。(含义参考man ss

专注于互联网架构技术,努力成为一名架构师 2008

SAP 针对不同行业(制造业、零售业、金融业)推出了专属的云解决方案,以下是各行业的核心方案与特点

SAP推出行业专属云解决方案:制造业(SAP Digital Manufacturing Cloud)提供AI排产与质量预警,降低缺陷率;零售业(S/4HANA Cloud零售版)实现AI预测补货与全渠道库存同步,减少浪费;金融业(Business ByDesign)支持合规自动化与实时风控,降低审计成本。各方案均采用云原生架构,典型案例显示实施效果显著。

金牌架构师 496

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

基于MobaXterm配置ssh隧道(port forwarding)

文章目录1. 打开 MobaXterm,在工具中找到 MobaSSHtunnal,点击建立新的隧道2. 代理配置3. 添加私钥4. 点击启动/停止启动/关闭代理 1. 打开 MobaXterm,在工具中找到 MobaSSHtunnal,点击建立新的隧道 2. 代理配置 3. 添加私钥 4. 点击启动/停止启动/关闭代理 ...

zhou_zhao_xu的博客 4631

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

SSH端口转发(port forwarding)基础知识

1.介绍 我们知道SSH的一个重要作用就是连接远程机器(比如一台服务器)来实现对远程机器的操作。 但是除此之外,SSH还可以用于构建机器之间的通信中介。 举个简单的例子,我们有时会在本地(localhost)运行某些程序或应用,而每个程序或者应用都会运行在一个指定的端口下(port), 我们可以直接通过监听(listen)该端口来查看其他用户向该应用发出的请求(request)并做出响应(response)。但是如果我们希望对本地端口的请求,由远程机器来进行响应,换句话说,我们希望能将发送给本地端口的

weixin_44607838的博客 1万+

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

【工具使用】怎么设置SSH隧道(Port Forwarding)

- SSH隧道(SSH tunneling)是一个在一个客户端和服务器之间创建加密的SSH连接的方法,它通过能够被转播的端口进行。 - SSH传送(SSH forwarding)对于运送网络中的服务数据很有用,它使用未加密的协议,比如VNC或者FTP,来获取受到地域限制的内容,或者绕开中间的防火墙。基本上,你可以传送任何TCP端口和打通通道通过一个安全的SSH连接。 - 这里有三种类型的SSH端口传送: - 局部端口传送:从客户端主机到SSH服务器主机传送连接,之后再到达目标主机端口。 - 远程端口

GW_Krystal的博客 2339

How to Create SSH Tunneling (Port Forwarding)

#Linux #Command #SSH

yuguo_im的博客 1211

ssh端口转发(port forwarding)

<br /><br />SSH(Secure Shell)有很多功能,它既可以代替Telnet,又可以为FTP、POP、甚至为PPP<br />提供一个安全的"通道".<br />从ssh这个命令可以看出一些端倪,通常形式为:<br />ssh ... [command]<br />可以直接在远程主机执行命令,当忽略command时相当于一个安全的telnet.<br />其次,sshd类似于xinetd,为许多其他服务监听连接,当有连接到达时fork一下服务程序就<br />可以了,sftpd大概就是如此

agan04的专栏 1283

A short guide to SSH port forwarding

A short guide to SSH port forwarding SSH port forwarding, or TCP/IP connection tunneling, is a process whereby a TCP/IP connection that would otherwise be insecure is tunneled through a secure SSH link, thus protecting the tunneled connection from netwo..

星瀚 278

SSH Port Forwarding (SSH Tunneling)

Sintax: ssh -L localport:host:hostport user@ssh_server -N where: -L - port forwarding parameters (see below) localport - local port (chose a port that is not in use by other ser...

weixin_33816300的博客 261

SSH port forwarding - socks server 转载

利用SSH port forwarding来做代理,运行这样的命令:ssh -D 7070 -N -C-o PubkeyAuthentication=no<username>@<hostname>From:http://www.debian-administration.org/article/SSH_dynamic_port_forwarding_with_SOC...

weixin_30345577的博客 242

SSH Tunnel扫盲(ssh port forwarding端口转发)

SSH的的Port Forward,中文可以称为端口转发,是SSH的一项非常重要的功能。它可以建立一条安全的SSH通道,并把任意的TCP连接放到这条通道中。下面仔细就仔细讨论SSH的这种非常有用的功能。 SSH Tunnel有三种,分别是本地Local(ssh -NfL),远程Remote(ssh -NfR),动态Dynamic(ssh -NfD)。(含义参考man ssh)

chinaclock的专栏 2987

ssh-remote-port-forwarding

1、ssh免密码登录(A登录到B)   1.1、在A上执行并在~/.ssh下生成id_rsa和id_rsa.pub两个文件 ssh-keygen -t rsa -P ''   1.2、拷贝id_rsa.pub到B的~/.ssh/authorized_keys文件中 scp ~/.ssh/id_rsa.pub tato@192.168.1.100:~/.ssh/authorize...

weixin_34082177的博客 225
上一篇: 第一次用SSH Tunnel就上手
下一篇: SSH-Tunnels
iiprogram
博客等级 码龄25年 1068粉丝 1515原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值