I have a personal server at my office and I would like to ssh into this Ubuntu 18.04 server. I can't forward ports since I dont have access to the router since its a shared office space. I tried ngrok service, it works but still limited. How else can I do this? I read about reverse ssh, but how do I set this up?
1 Answers
First, in order to setup a reverse ssh into a firewalled server behind routers, you need to have a public server (VPS) with a public IP address.
Office-Server - Ubuntu 18.04 -- SSH port: 9999
Relay-VPS - Any cheap VPS will do. -- SSH port: 6666
First, you need to establish an open "connection" from the Office-Server to the Relay-VPS by doing:
ssh -fN -R 10022:localhost:9999 relay_username@relayvps.xyz -p 6666
10022 is just a random port you pick when establishing a connection from the Office-Server to the Relay-VPS. 9999 is the ssh port for the Office-Server.
Now while you're in Relay-VPS, you can just:
ssh root@localhost -p 10022
The above requires you to log in to the Relay-VPS and then login again to the Office-Server. To make it one simple step. You need to first set up passwordless login via ssh using id_rsa.pub keys. Also, you need to enable in /etc/sshd/sshd_config file:
GatewayPorts clientspecified
Now kill the previous open ssh connection. And open a new one on Office-Server with:
ssh -fN -R relayssh.xyz:10022:localhost:9999 relay_username@relayssh.xyz -p 6666
Now on your laptop just do:
ssh office_username@relayssh.xyz -p 10022
The only difference here is the office_username is used to log in to relayssh.xyz and port is now 10022 instead of 6666.
I got a lot of hints from the following article: https://www.xmodulo.com/access-linux-server-behind-nat-reverse-ssh-tunnel.html
- 9
- 3,153