Creating an SSH tunnel is a powerful way to securely forward network traffic from one machine to another. This can be useful for accessing services that are behind a firewall, securing data transmission, or bypassing network restrictions. Here’s a step-by-step guide on how to create an SSH tunnel and its usage:
Creating an SSH Tunnel
Install SSH Client
Ensure you have an SSH client installed. On most Unix-based systems (Linux, macOS), ssh is pre-installed. For Windows, you can use the OpenSSH via PowerShell or Command Prompt, or GUI tools like MobaXterm, PuTTY.
SSH Tunnel Command via CLI
The command to create an SSH tunnel is:
ssh -L [local_port]:[remote_host]:[remote_port] [user]@[ssh_server]
- local_port: The port on your local machine where you want to forward traffic.
- remote_host: The remote machine you want to access.
- remote_port: The port on the remote machine you want to access.
- user: Your username on the SSH server.
- ssh_server: The SSH server you are connecting to.
For example, to forward traffic from your local port 8080 to a remote web server on port 80 through an SSH server:
ssh -L 8080:remote_web_server:80 user@ssh_server
Closing the Tunnel
To close the SSH tunnel, simply terminate the SSH session by closing the terminal or using the exit command.
SSH Tunnel using Putty
- Enter Host Information
- Navigate to Tunnels: In the left sidebar under the Category options, navigate to Connection > SSH > Tunnels
- Configure Local Port Forwarding: Select "Local" to define the type of SSH port forward. In the "Source port" field, enter the port number to use on your local system (e.g., 5050). In the "Destination" field, enter the destination address followed by the port number (e.g., 127.0.0.1:5432).
- Add and Connect: Verify the details and press the "Add" button. Connect the SSH session to make the tunnel. The tunnel will work as long as the SSH session is active.
Closing the Tunnel
To close the SSH tunnel, simply terminate the SSH session by closing the terminal or using the exit command.
SSH Tunnel using MobaXTerm
- Select Tunneling to bring up the tunneling the configuration tool
- Click [New SSH tunnel] to create an SSH tunnel
- Enter the tunnel configuration
- Forwarded port: The port on your local machine where you want to forward traffic.
- Remote server: The remote machine you want to access.
- Remote port: The port on the remote machine you want to access.
- SSH login: Your username on the SSH server.
- SSH server: The SSH server you are connecting to.
- SSH port: defaults to 22
- i.e.
- Click [Save] when done.
- Click the start button to create the SSH tunnel
- Use [localhost:<Forwarded port>] to access the tunnel connection.
- ie. http://localhost:8080 to connect to a webserver through the tunnel connection.
Closing the Tunnel
- Click the stop button to disconnect the SSH tunnel
Related to
Comments
0 comments
Please sign in to leave a comment.