Skip to content

Commit 22a4106

Browse files
committed
update docker ssh passthrough docs
1 parent e7b47c5 commit 22a4106

File tree

1 file changed

+63
-84
lines changed

1 file changed

+63
-84
lines changed

docs/content/doc/installation/with-docker.en-us.md

Lines changed: 63 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -256,27 +256,27 @@ You can configure some of Gitea's settings via environment variables:
256256

257257
(Default values are provided in **bold**)
258258

259-
* `APP_NAME`: **"Gitea: Git with a cup of tea"**: Application name, used in the page title.
260-
* `RUN_MODE`: **dev**: For performance and other purposes, change this to `prod` when deployed to a production environment.
261-
* `DOMAIN`: **localhost**: Domain name of this server, used for the displayed http clone URL in Gitea's UI.
262-
* `SSH_DOMAIN`: **localhost**: Domain name of this server, used for the displayed ssh clone URL in Gitea's UI. If the install page is enabled, SSH Domain Server takes DOMAIN value in the form (which overwrite this setting on save).
263-
* `SSH_PORT`: **22**: SSH port displayed in clone URL.
264-
* `SSH_LISTEN_PORT`: **%(SSH\_PORT)s**: Port for the built-in SSH server.
265-
* `DISABLE_SSH`: **false**: Disable SSH feature when it's not available. If you want to disable SSH feature, you should set SSH port to `0` when installing Gitea.
266-
* `HTTP_PORT`: **3000**: HTTP listen port.
267-
* `ROOT_URL`: **""**: Overwrite the automatically generated public URL. This is useful if the internal and the external URL don't match (e.g. in Docker).
268-
* `LFS_START_SERVER`: **false**: Enables git-lfs support.
269-
* `DB_TYPE`: **sqlite3**: The database type in use \[mysql, postgres, mssql, sqlite3\].
270-
* `DB_HOST`: **localhost:3306**: Database host address and port.
271-
* `DB_NAME`: **gitea**: Database name.
272-
* `DB_USER`: **root**: Database username.
273-
* `DB_PASSWD`: **"\<empty>"**: Database user password. Use \`your password\` for quoting if you use special characters in the password.
274-
* `INSTALL_LOCK`: **false**: Disallow access to the install page.
275-
* `SECRET_KEY`: **""**: Global secret key. This should be changed. If this has a value and `INSTALL_LOCK` is empty, `INSTALL_LOCK` will automatically set to `true`.
276-
* `DISABLE_REGISTRATION`: **false**: Disable registration, after which only admin can create accounts for users.
277-
* `REQUIRE_SIGNIN_VIEW`: **false**: Enable this to force users to log in to view any page.
278-
* `USER_UID`: **1000**: The UID (Unix user ID) of the user that runs Gitea within the container. Match this to the UID of the owner of the `/data` volume if using host volumes (this is not necessary with named volumes).
279-
* `USER_GID`: **1000**: The GID (Unix group ID) of the user that runs Gitea within the container. Match this to the GID of the owner of the `/data` volume if using host volumes (this is not necessary with named volumes).
259+
- `APP_NAME`: **"Gitea: Git with a cup of tea"**: Application name, used in the page title.
260+
- `RUN_MODE`: **dev**: For performance and other purposes, change this to `prod` when deployed to a production environment.
261+
- `DOMAIN`: **localhost**: Domain name of this server, used for the displayed http clone URL in Gitea's UI.
262+
- `SSH_DOMAIN`: **localhost**: Domain name of this server, used for the displayed ssh clone URL in Gitea's UI. If the install page is enabled, SSH Domain Server takes DOMAIN value in the form (which overwrite this setting on save).
263+
- `SSH_PORT`: **22**: SSH port displayed in clone URL.
264+
- `SSH_LISTEN_PORT`: **%(SSH_PORT)s**: Port for the built-in SSH server.
265+
- `DISABLE_SSH`: **false**: Disable SSH feature when it's not available. If you want to disable SSH feature, you should set SSH port to `0` when installing Gitea.
266+
- `HTTP_PORT`: **3000**: HTTP listen port.
267+
- `ROOT_URL`: **""**: Overwrite the automatically generated public URL. This is useful if the internal and the external URL don't match (e.g. in Docker).
268+
- `LFS_START_SERVER`: **false**: Enables git-lfs support.
269+
- `DB_TYPE`: **sqlite3**: The database type in use \[mysql, postgres, mssql, sqlite3\].
270+
- `DB_HOST`: **localhost:3306**: Database host address and port.
271+
- `DB_NAME`: **gitea**: Database name.
272+
- `DB_USER`: **root**: Database username.
273+
- `DB_PASSWD`: **"\<empty>"**: Database user password. Use \`your password\` for quoting if you use special characters in the password.
274+
- `INSTALL_LOCK`: **false**: Disallow access to the install page.
275+
- `SECRET_KEY`: **""**: Global secret key. This should be changed. If this has a value and `INSTALL_LOCK` is empty, `INSTALL_LOCK` will automatically set to `true`.
276+
- `DISABLE_REGISTRATION`: **false**: Disable registration, after which only admin can create accounts for users.
277+
- `REQUIRE_SIGNIN_VIEW`: **false**: Enable this to force users to log in to view any page.
278+
- `USER_UID`: **1000**: The UID (Unix user ID) of the user that runs Gitea within the container. Match this to the UID of the owner of the `/data` volume if using host volumes (this is not necessary with named volumes).
279+
- `USER_GID`: **1000**: The GID (Unix group ID) of the user that runs Gitea within the container. Match this to the GID of the owner of the `/data` volume if using host volumes (this is not necessary with named volumes).
280280

281281
# Customization
282282

@@ -291,7 +291,8 @@ files; for named volumes, this is done through another container or by direct ac
291291
:exclamation::exclamation: **Make sure you have volumed data to somewhere outside Docker container** :exclamation::exclamation:
292292

293293
To upgrade your installation to the latest release:
294-
```
294+
295+
```bash
295296
# Edit `docker-compose.yml` to update the version, if you have one specified
296297
# Pull new images
297298
docker-compose pull
@@ -301,88 +302,66 @@ docker-compose up -d
301302

302303
# SSH Container Passthrough
303304

304-
Since SSH is running inside the container, you'll have to pass SSH from the host to the
305-
container if you wish to use SSH support. If you wish to do this without running the container
306-
SSH on a non-standard port (or move your host port to a non-standard port), you can forward
307-
SSH connections destined for the container with a little extra setup.
308-
309-
This guide assumes that you have created a user on the host called `git` which shares the same
310-
UID/GID as the container values `USER_UID`/`USER_GID`. You should also create the directory
311-
`/var/lib/gitea` on the host, owned by the `git` user and mounted in the container, e.g.
305+
Since SSH is running inside the container, SSH needs to be passed through from the host to the container if SSH support is desired. One option would be to run the container SSH on a non-standard port (or moving the host port to a non-standard port). Another option which might be more straightforward is to forward SSH connections from the host to the container. This setup is explained in the following.
312306

313-
```
314-
version: "3"
307+
This guide assumes that you have created a user on the host called `git` which shares the same `UID`/ `GID` as the container values `USER_UID`/ `USER_GID`. These values can be set as environment variables in the `docker-compose.yml`:
315308

316-
services:
317-
server:
318-
image: gitea/gitea:{{< version >}}
319-
container_name: gitea
320-
environment:
321-
- USER_UID=1000
322-
- USER_GID=1000
323-
restart: always
324-
networks:
325-
- gitea
326-
volumes:
327-
- /var/lib/gitea:/data
328-
- /etc/timezone:/etc/timezone:ro
329-
- /etc/localtime:/etc/localtime:ro
330-
ports:
331-
- "3000:3000"
332-
- "127.0.0.1:2222:22"
309+
```bash
310+
environment:
311+
- USER_UID=1000
312+
- USER_GID=1000
333313
```
334314

335-
You can see that we're also exposing the container SSH port to port 2222 on the host, and binding this
336-
to 127.0.0.1 to prevent it being accessible external to the host machine itself.
315+
Next mount `/home/git/.ssh` of the host into the container. Otherwise the SSH authentication cannot work inside the container.
337316

338-
On the **host**, you should create the file `/app/gitea/gitea` with the following contents and
339-
make it executable (`chmod +x /app/gitea/gitea`):
340-
341-
```
342-
#!/bin/sh
343-
ssh -p 2222 -o StrictHostKeyChecking=no [email protected] "SSH_ORIGINAL_COMMAND=\"$SSH_ORIGINAL_COMMAND\" $0 $@"
317+
```bash
318+
volumes:
319+
- /home/git/.ssh/:/data/git/.ssh
344320
```
345321

346-
Your `git` user needs to have an SSH key generated:
322+
Now a SSH key pair needs to be created on the host. This key pair will be used to authenticate the `git` user on the host to the container.
347323

348-
```
324+
```bash
349325
sudo -u git ssh-keygen -t rsa -b 4096 -C "Gitea Host Key"
350326
```
351327

352-
Now, proceed with one of the points given below:
353-
354-
- symlink the container `.ssh/authorized_keys` file to your git user `.ssh/authorized_keys`.
355-
This can be done on the host as the `/var/lib/gitea` directory is mounted inside the container under `/data`:
328+
In the next step a file named `/app/gitea/gitea` (with executable permissions) needs to be created on the host. This file will issue the SSH forwarding from the host to the container. Add the following contents to `/app/gitea/gitea`:
356329

357-
```
358-
ln -s /var/lib/gitea/git/.ssh/authorized_keys /home/git/.ssh/authorized_keys
330+
```bash
331+
ssh -p 2222 -o StrictHostKeyChecking=no [git@127.0.0.1](mailto:git@127.0.0.1) "SSH_ORIGINAL_COMMAND=\"$SSH_ORIGINAL_COMMAND\" $0 $@"
359332
```
360333

361-
Then echo the `git` user SSH key into the authorized_keys file so the host can talk to the container over SSH:
334+
To make the forwarding work, the SSH port of the container (22) needs to be mapped to the host port 2222 in `docker-compose.yml` . Since this port does not need to be exposed to the outside world, it can be mapped to the `localhost` of the host machine:
362335

363-
```
364-
echo "no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty $(cat /home/git/.ssh/id_rsa.pub)" >> /var/lib/gitea/git/.ssh/authorized_keys
336+
```bash
337+
ports:
338+
# [...]
339+
- "127.0.0.1:2222:22"
365340
```
366341

367-
Lastly, Gitea makes `authorized_keys` backups by default. This could be a problem
368-
as the symbolic link made to `authorized_keys` previously could end up pointing
369-
to an old backup. To resolve this, please put the following into your Gitea
370-
config:
342+
In addition, `/home/git/.ssh/authorized_keys` on the host needs to be modified. It needs to act in the same way as `authorized_keys` within the Gitea container. Therefore add
371343

372-
```
373-
[ssh]
374-
SSH_AUTHORIZED_KEYS_BACKUP=false
344+
```bash
345+
command="/app/gitea/gitea --config=/data/gitea/conf/app.ini serv key-1",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa <YOUR_SSH_PUBKEY>
375346
```
376347

377-
- mount your `.ssh` directory directly into the container i.e. add the
378-
following to the `volumes` section of your Docker container config:
348+
and replace `<YOUR_SSH_PUBKEY>` with a valid SSH public key of yours.
379349

380-
```
381-
- /home/git/.ssh/:/data/git/.ssh/
382-
```
350+
In addition the public key of the `git` user on the host needs to be added to `/home/git/.ssh/authorized_keys` so authentication against the container can succeed: `echo "$(cat /home/git/.ssh/id_rsa.pub)" >> /home/git/.ssh/authorized_keys`.
351+
352+
Here is a detailed explanation what is happening when a SSH request is made:
353+
354+
1. A SSH request is made against the host using the `git` user, e.g. `git clone git@domain:user/repo.git`.
355+
2. In `/home/git/.ssh/authorized_keys` , the command executes the `/app/gitea/gitea` script.
356+
3. `/app/gitea/gitea` forwards the SSH request to port 2222 which is mapped to the SSH port (22) of the container.
357+
4. Due to the existence of the public key of the `git` user in `/home/git/.ssh/authorized_keys` the authentication host → container succeeds and the SSH request get forwarded to Gitea running in the docker container.
358+
359+
If a new SSH key is added in the Gitea web interface, it will be appended to `.ssh/authorized_keys` in the same way as the already existing key.
360+
361+
**Notes**
383362

384-
Now you should be able to use Git over SSH to your container without disrupting SSH access to the host.
363+
SSH container passthrough will work only if
385364

386-
Please note: SSH container passthrough will work only if using opensshd in container, and will not work if
387-
`AuthorizedKeysCommand` is used in combination with setting `SSH_CREATE_AUTHORIZED_KEYS_FILE=false` to disable
388-
authorized files key generation.
365+
- `opensshd` is used in the container
366+
- if `AuthorizedKeysCommand` is _not used_ in combination with `SSH_CREATE_AUTHORIZED_KEYS_FILE=false` to disable authorized files key generation
367+
- `LOCAL_ROOT_URL` is not changed

0 commit comments

Comments
 (0)