Skip to content

Commit 4ee6dd5

Browse files
committed
Add more details about port forwarding
1 parent 940f7aa commit 4ee6dd5

File tree

4 files changed

+1194
-12
lines changed

4 files changed

+1194
-12
lines changed

README.md

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Docker SSH Tunnel
22

3-
This project provides alpine-based Docker images
3+
This project provides Alpine-based Docker images
44
for setting up a persistent SSH tunnel
55
between two containers on different servers.
66

@@ -10,7 +10,7 @@ between two containers on different servers.
1010
docker pull ghcr.io/querateam/docker-ssh-tunnel/server
1111
```
1212

13-
The following environment variables are supported in the server image.
13+
The server image supports the following environment variables:
1414

1515
#### SSH keys
1616

@@ -54,7 +54,7 @@ docker pull ghcr.io/querateam/docker-ssh-tunnel/client
5454
```
5555

5656
The client image uses `autossh` to establish a persistent SSH tunnel.
57-
The following environment variables are supported in the client image.
57+
The client image supports the following environment variables:
5858

5959
#### SSH keys
6060

@@ -77,14 +77,19 @@ For more information, see the [`ssh_config(5)`](https://linux.die.net/man/5/ssh_
7777
| `SSH_SESSION_TYPE` | SessionType | `none` |
7878

7979
You can pass arguments to `ssh` command using the `SSH_CLI_OPTIONS` environment variable.
80-
You can define the SSH port forwarding rule using this variable. Examples:
80+
You can define the SSH port forwarding using this variable (`-R` and `-L` options).
81+
Please note that the server image only supports
82+
remote port forwarding by default for security reasons.
83+
If you want to use local port forwarding,
84+
you need to enable it in the server image
85+
by setting the `SSHD_ALLOW_TCP_FORWARDING` environment variable to `local` or `all`.
86+
In this case, consider `SSHD_PERMIT_OPEN` option to restrict the port forwarding.
8187

82-
- `-R remote_port:local_host:local_port`
83-
- `-R remote_host:remote_port:local_host:local_port`
88+
![SSH port forwarding cheatsheet](docs/ssh-port-forwarding.png)
8489

85-
#### AutoSSH options
90+
#### Autossh options
8691

87-
All [AutoSSH environment variables](https://github.com/Autossh/autossh)
92+
All [autossh environment variables](https://github.com/Autossh/autossh)
8893
are supported, but the following variables have a default set in this image:
8994

9095
| Environment Variable | Default Value |
@@ -94,8 +99,10 @@ are supported, but the following variables have a default set in this image:
9499
| `AUTOSSH_POLL` | `30` |
95100

96101
In this image, the autossh monitoring function is turned off by default (`AUTOSSH_PORT=0`).
97-
Instead of the monitoring function, this image uses `ServerAliveInterval`, `ServerAliveCountMax`, and `ExitOnForwardFailure` ssh options to have the SSH client exit if it finds itself no longer connected to the server.
98-
As a result, autossh detects the connection problem and restarts ssh.
102+
Instead of the monitoring function,
103+
this image uses `ServerAliveInterval`, `ServerAliveCountMax`, and `ExitOnForwardFailure` SSH options
104+
to have the SSH client exit if it finds itself no longer connected to the server.
105+
As a result, autossh detects the connection problem and restarts SSH.
99106

100107
## Key generation
101108

@@ -124,6 +131,7 @@ KEY2_PUB=$(cat key2.pub)
124131
docker run --name tunnel-server --rm -it --init \
125132
-e SERVER_ED25519_PRIVATE_KEY_BASE64="$KEY1_BASE64" \
126133
-e CLIENT_AUTHORIZED_KEYS="$KEY2_PUB" \
134+
-e SSHD_PERMIT_LISTEN="0.0.0.0:4444" \
127135
-p 2222:22 \
128136
-p 127.0.0.1:4444:4444 \
129137
ghcr.io/querateam/docker-ssh-tunnel/server
@@ -141,13 +149,20 @@ docker run --name tunnel-client --rm -it --init --add-host=host.docker.internal:
141149
ghcr.io/querateam/docker-ssh-tunnel/client
142150
```
143151

144-
Test the tunnel using `nc`:
152+
To test the tunnel connection,
153+
run the following commands in separate terminals:
145154

146155
```shell
147156
docker exec -it tunnel-client /usr/bin/nc -l -s 127.0.0.1 -p 6666
157+
```
158+
159+
```shell
148160
nc 127.0.0.1 4444
149161
```
150162

163+
Type some text and press `Enter` in one terminal.
164+
You should see the text in the other terminal.
165+
151166
## Docker compose example
152167

153168
```yaml
@@ -158,6 +173,7 @@ services:
158173
environment:
159174
SERVER_ED25519_PRIVATE_KEY_BASE64: ... value of key1.base64 ...
160175
CLIENT_AUTHORIZED_KEYS: ... value of key2.pub ...
176+
SSHD_PERMIT_LISTEN: 0.0.0.0:4444
161177
ports:
162178
- 2222:22
163179
- 127.0.0.1:4444:4444
@@ -175,9 +191,16 @@ services:
175191
- host.docker.internal:host-gateway
176192
```
177193
178-
Test the tunnel using `nc`:
194+
To test the tunnel connection,
195+
run the following commands in separate terminals:
179196
180197
```shell
181198
docker compose exec -it tunnel-client /usr/bin/nc -l -s 127.0.0.1 -p 6666
199+
```
200+
201+
```shell
182202
nc 127.0.0.1 4444
183203
```
204+
205+
Type some text and press `Enter` in one terminal.
206+
You should see the text in the other terminal.

0 commit comments

Comments
 (0)