> For the complete documentation index, see [llms.txt](https://thorstenkramm.gitbook.io/saltstack/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://thorstenkramm.gitbook.io/saltstack/beispiele/wartungstunnel.md).

# Wartungstunnel

Vorbereitungen auf dem Host, welche als Endpunkt für den Tunnel dient, z.B. der Salt-Master

```
mkdir /srv/salt/mtunnel
useradd -m -r -d /var/lib/mtunnel mtunnel
mkdir /var/lib/mtunnel/.ssh
ssh-keygen -q -t ed25519 -N "" -f /var/lib/mtunnel/.ssh/id_ed25519 -C "mtunnel@salt-master.local"
cp /var/lib/mtunnel/.ssh/id_ed25519.pub /var/lib/mtunnel/.ssh/authorized_keys
cp /var/lib/mtunnel/.ssh/id_ed25519 /srv/salt/mtunnel/
chown -R mtunnel:mtunnel /var/lib/mtunnel/
chmod 0700 /var/lib/mtunnel/.ssh
chmod 0600 /var/lib/mtunnel/.ssh/*
```

{% code title="/srv/salt/mtunnel/init.sls" %}

```yaml
#
# Create a mtunnel user
#
mtunnel:
  group.present: []
  user.present:
    - fullname: Maintenance SSH Reverse Tunnel
    - shell: /usr/sbin/nologin
    - home: /var/lib/mtunnel
    - createhome: true
    - system: true
    - groups:
      - mtunnel

ssh-dir:
  file.directory:
    - name: /var/lib/mtunnel/.ssh
    - user: mtunnel
    - group: mtunnel
    - mode: 0700
    - require:
      - user: mtunnel

#
# Deploy a pre-generated priv key from the salt-master
#
priv-key:
  file.managed:
    - name: /var/lib/mtunnel/.ssh/id_ed25519
    - source: salt://mtunnel/id_ed25519
    - user: mtunnel
    - group: mtunnel
    - mode: 0600
    - require: 
      - file: ssh-dir

#
# Create a default config read later by systemd
#
mtunnel-default:
  file.managed:
    - name: /etc/default/mtunnel.conf
    - require:
      - file: priv-key
    - contents: |
        PORT={{ pillar['mtunnel']['port'] }}
        HOST={{ pillar['mtunnel']['host'] }}

mtunnel-service-file:
  file.managed:
    - name: /etc/systemd/system/mtunnel.service
    - require:
      - file: mtunnel-default
    - contents: |
        [Unit]
        Description=Create a reverse SSH Tunnel for maintenance
        After=network.target

        [Service]
        EnvironmentFile=/etc/default/mtunnel.conf
        ExecStart=/usr/bin/ssh -NT \
          -o ServerAliveInterval=60 \
          -o StrictHostKeyChecking=no \
          -o ExitOnForwardFailure=yes \
          -R ${PORT}:localhost:22 ${HOST}
        RestartSec=5
        Restart=always
        User=mtunnel
        Group=mtunnel

        [Install]
        WantedBy=multi-user.target

mtunnel.service:
  service.dead:
    - enable: False

{% if pillar['mtunnel']['http_proxy'] is defined %}
corkscrew:
  pkg.installed: []

{% if pillar['mtunnel']['http_proxy_user'] is defined %}
cork-auth:
  file.managed:
    - name: ~/.ssh/cork.auth
    - contents: {{pillar['mtunnel']['http_proxy_user']}}:{{pillar['mtunnel']['http_proxy_password']}}
ssh-conf:
  file.managed:
    - name: /var/lib/mtunnel/.ssh/config
    - contents: |
        Host *
             ProxyCommand /usr/bin/corkscrew {{pillar['mtunnel']['http_proxy']}} {{pillar['mtunnel']['http_proxy_port']}} %h %p ~/.ssh/cork.auth
    - require:
      - file: cork-auth
{% else %}
ssh-conf:
  file.managed:
    - name: /var/lib/mtunnel/.ssh/config
    - contents: |
        Host *
             ProxyCommand /usr/bin/corkscrew {{pillar['mtunnel']['http_proxy']}} {{pillar['mtunnel']['http_proxy_port']}} %h %p
{%endif %}
{% endif %}
```

{% endcode %}

Beispiel Pillar (hostspezifisch, Type `file_tree`)

{% code title="/srv/pillar/hosts/minion1/mtunnel" %}

```yaml
host: 192.168.3.136
port: 2222
http_proxy: 192.168.3.136
http_proxy_port: 8888
```

{% endcode %}

Tunnel vorbereiten und starten

```bash
salt minion1 state.apply mtunnel
salt minion1 service.start mtunnel
netstat -tulpen|grep sshd.*mtunnel
salt minion1 service.stop mtunnel
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://thorstenkramm.gitbook.io/saltstack/beispiele/wartungstunnel.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
