Reverse ssh tunnel not exposing the port on ec2

Viewed 928

This question is probably answered already and there are numerous articles showing how to do reverse ssh tunnel. I am however stuck on the last step.

I have a Linux box that runs a web service on port 8080. This is behind a NAT and I can not port-forward from the overlaying routers to it.

I have a fresh Ubuntu 18 ec2 instance on AWS and I am using the following command to reverse the tunnel from my Linux box.

ssh -i "key.pem" -R \*:8080:\*:8080 ubuntu@ec2 -p 45332

EC2 ssh server listening on port 45332

On the server:

$ sudo lsof -i -P -n | grep LISTEN
systemd-r   650 systemd-resolve   13u  IPv4  15830      0t0  TCP 127.0.0.53:53 (LISTEN)
sshd        893            root    3u  IPv4  18322      0t0  TCP *:45332 (LISTEN)
sshd        893            root    4u  IPv6  18333      0t0  TCP *:45332 (LISTEN)
sshd      20731          ubuntu    9u  IPv6  75581      0t0  TCP [::1]:8080 (LISTEN)
sshd      20731          ubuntu   10u  IPv4  75582      0t0  TCP 127.0.0.1:8080 (LISTEN)

If I do curl localhost:8080 on Ec2, I get the correct web service response.

But when I do the curl using the Public IP/Public DNS name from same ec2 or a completely different device I get:

curl: (7) Failed to connect to <IP> port 8080: Connection refused

I have checked my VPC ACL and allowed ALL inbound and outbound traffic in the security group.

I made sure that there the firewall rules are not blocking it:

$ sudo ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
45332/tcp                  ALLOW IN    Anywhere                  
45332/udp                  ALLOW IN    Anywhere                  
8080/tcp                   ALLOW IN    Anywhere                  
8080/udp                   ALLOW IN    Anywhere                  
45332/tcp (v6)             ALLOW IN    Anywhere (v6)             
45332/udp (v6)             ALLOW IN    Anywhere (v6)             
8080/tcp (v6)              ALLOW IN    Anywhere (v6)             
8080/udp (v6)              ALLOW IN    Anywhere (v6)

When this didn't work, I removed the ssh tunnel and directly ran the same web service on the ec2. This worked perfectly fine.

However, I want to run the service from my Linux box.

2 Answers

I had the same problem and this worked for me.

From you local Linux box:

ssh -i "key.pem" -R 8081:\*:8080 ubuntu@ec2 -p 45332

notice I use 8081 instead of 8080

Then in the EC2 instance, ssh to itself tunneling 8081 to 8080.

ssh locahlost -L 8080:localhost:8081

I don't really understand how, but after trying different combinations, this one worked for me.

Related