Skip to content

Instantly share code, notes, and snippets.

@cregx
Last active May 4, 2024 05:04
Show Gist options
  • Save cregx/5d18b82e7b609fc704191839b1a7338b to your computer and use it in GitHub Desktop.
Save cregx/5d18b82e7b609fc704191839b1a7338b to your computer and use it in GitHub Desktop.
Howto redirect ports over SSH

Howto redirect ports over SSH

Introduction

This how-to describes the possibility to redirect a (server) remote port, e.g. 80 to a client local port, e.g. 8888 via SSH. This is sometimes useful, e.g. if you need to access a web server (e.g. an internal website) but only have SSH access to the network, which is otherwise sealed off from the outside (internet).

The instructions (screenshots) described here refer to a macOS environment but can also be used under Windows or Linux.

It is assumed that access to the remote server via SSH is available.


Here is an example of a simple diversion with the corresponding explanation:

ssh -L 8888:localhost:80 user@example-remote-host-873383.ex -p 49111

The command means: Log on to the remote server example-remote-host-873383.eu. Then forward the standard web server port 80 to the port 8888 of my client.

The optional parameter -p defines the port on which the SSH service on the remote server listens (49111). In a standard SSH configuration, this is port 22.

Important: For SSH accesses via the Internet, the default port 22 should never be (open) used.

The syntax for port forwarding has the following meaning:

my-localport:localhost:server-port

And this is what the result of a redirected port of the web server looks like in the local browser window.

redirected-web-server

The same example with a slightly different notation:

ssh -L 8888:127.0.0.1:80 user@example-remote-host-873383.ex -p 49111

This example corresponds exactly to the example above. 127.0.0.1 represents the loopback IPv4 address of the localhost entry.

redirected-web-server

You can also redirect other service ports, e.g. those of a MySQL service running on the remote server.

ssh -Ng -L 33306:127.0.0.1:3306 myUser@example-remote-host-873383.ex -p 49111

The command means: Log on to the remote system and execute each command remotely (-Ng) and perform port forwarding (-L). In this case, it means that all commands executed locally and routed via port 33306 are tunneled to port 3306 of the remote server.

Summary

I know that the examples are only a tiny sample of what is possible. However, the topic is very fascinating and allows you to do cool things.

For more info, check out the manual pages on SSH

man ssh

Disclaimer

This manual or parts of it are provided "as is" without warranty of any kind.

MIT License

Copyright (c) 2020 cregx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment