Port Forwarding - How to Expose Port from Server to Your PC by SSH Tunnel

Port Forwarding - How to Expose Port from Server to Your PC by SSH Tunnel

SSH stands for “Secure Shell” or “Secure Socket Shell“. It is a cryptographic network protocol that allows two computers to communicate and share the data over on insecure network such as Internet. SSH protocols protects the network from various attacks.?

Local Port Forwarding

Starting from the one that I use the most. Oftentimes, there might be a service listening on localhost or a private interface of a machine that I can only SSH to via its public IP. And I desperately need to access this port from the outside. A few typical examples:

  • Accessing a database (MySQL, Postgres, Redis, etc) using a fancy UI tool from your laptop.
  • Using your browser to access a web application exposed only to a private network.
  • Accessing a container's port from your laptop without publishing it on the server's public interface.

All of the above use cases can be solved with a single ssh command:

ssh -L local_port:remote_addr:remote_port [user@]bastion_addr        

Remote Port Forwarding

Another popular (but rather inverse) scenario is when you want to momentarily expose a local service to the outside world. Of course, for that, you'll need a public-facing ingress gateway server. A few typical examples:

  • Exposing a dev service from your laptop to the public Internet for a demo.
  • Canary test a service without installing the new service on the production machine.

But fear not! Any public-facing server with an SSH daemon on it can be used as such a gateway:

ssh -R [server_addr:]server_port:bastion_addr:bastion_port [user@]gateway_addr        

Conclusion

SSH port forwarding provides a secure way to access services on remote servers or local machines. Local port forwarding maps a remote service to a local port, while remote port forwarding maps a local service to a remote port. Both types of port forwarding have their specific use cases and benefits, but it’s important to consider the security implications when using either type.

References

https://www.geeksforgeeks.org/difference-between-ssh-local-and-remote-port-forwarding/

https://iximiuz.com/en/posts/ssh-tunnels/

要查看或添加评论,请登录

Amr Amin的更多文章

社区洞察

其他会员也浏览了