Créer un tunnel SSH en mode service sur centos 7
Soumis par kacy le
Des fois on en a besoin, lorsqu'on a pas encore fait les ouvertures de flux qui vont bien. J'ai trouvé un p'tit tuto qui marche bien ici
Je claque le code ici histoire de pas le perdre :P
Create a template service file at `
/etc/systemd/system/[email protected]
`. The template parameter will correspond to the name of target host:[Unit] Description=Setup a secure tunnel to %I After=network.target [Service] Environment="LOCAL_ADDR=localhost" EnvironmentFile=/etc/default/[email protected]%i ExecStart=/usr/bin/ssh -NT -o ServerAliveInterval=60 -o ExitOnForwardFailure=yes -L ${LOCAL_ADDR}:${LOCAL_PORT}:localhost:${REMOTE_PORT} ${TARGET} # Restart every >2 seconds to avoid StartLimitInterval failure RestartSec=5 Restart=always [Install] WantedBy=multi-user.targetWe need a configuration file (inside `/etc/default`) for each target host we will be creating tunnels for. For example, let's assume we want to tunnel to a host named `jupiter` (probably aliased in `/etc/hosts`). Create the file at `
/etc/default/[email protected]
`:TARGET=jupiter LOCAL_ADDR=0.0.0.0 LOCAL_PORT=20022 REMOTE_PORT=22Note that for the above to work we need to have allready setup a password-less SSH login to target (e.g. by giving access to a non-protected private key).
Now we can start the service instance:
systemctl start secure-tunnel@jupiter.service systemctl status secure-tunnel@jupiter.serviceOr enable it, so it get's started at boot time:
systemctl enable secure-tunnel@jupiter.service
Pour forwarder un service d'une machine vers une autre il faut remplacer -L [...]
par
[...] -R ${REMOTE_PORT}:localhost:${LOCAL_PORT} [...]
et ajouter
GatewayPorts yes
dans le /etc/ssh/sshd_config
sur la machine distante pour autoriser SSH à pousser le port sur une autre interface que localhost
Du coup quand on fait un netstat
ça marche :)
[root@myhost:~]# netstat -an |grep 8080 tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN tcp 0 0 :::8080 :::* LISTEN