大网数据广告活动
服务器运维问答

How to SSH tunnel with keys

· 52CE 我爱测

Port 9999 on a remote server needs to be accessed through an SSH tunnel at local port 9990 to avoid firewalls. I am using this command to SSH tunnel: ssh -N -i share.pem -L 9990:`ecshare`:9999 ubuntu@`ecshare` where ecmy contains…

问题原文

原帖链接 · fodon · CC BY-SA 3.0

Port 9999 on a remote server needs to be accessed through an SSH tunnel at local port 9990 to avoid firewalls.

I am using this command to SSH tunnel:

ssh -N -i share.pem -L 9990:`ecshare`:9999 ubuntu@`ecshare`

where ecmy contains the ec2 instance's ip. As a baseline, I can ssh and get a remote shell with this command:

ssh -i share.pem ubuntu@`ecshare`

But, when I try this on the local prompt:

curl -i -X GET http://localhost:9990

I get this on the shell where the tunnel was started:

channel 2: open failed: connect failed: Connection refused

When I try this command on the remote shell:

curl -i -X GET http://localhost:9999

... I get a response from the server.

Why is connection being refused?

已采纳解答

原帖链接 · Esa Jokinen · CC BY-SA 3.0

Despite the authentication method used, SSH tunneling works the same way. The problem here is not about using public key authentication but understanding the basics of How to Use SSH Tunneling.

Your -L 9990:example.com:9999 connects to the public network interface on the remote side while you connect to localhost:9999 in your curl test. The firewall you mentioned you need to avoid is probably on the remote side, preventing you to use http://example.com:9999/ in the first place.

You should use -L 9990:localhost:9999 instead to make it use local loopback.

This diagram visualizes your situation. The -L [ bind_address:]port:host:hostport...

...works by allocating a socket to listen to port on the local side, optionally bound to the specified bind_address. Whenever a connection is made to this port, the connection is forwarded over the secure channel, and a connection is made to host port hostport from the remote machine.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-04-04)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。