ssh -L: quién puede usar el puerto reenviado (127.0.0.1 frente a 0.0.0.0), ExitOnForwardFailure, -N y AllowTcpForwarding no (prueba con un sshd desechable)
con OpenSSH_9.6p1 en Ubuntu 24.04.5, ssh -N -L 18081:127.0.0.1:18080 escuchó en 127.0.0.1 y [::1]; curl por 127.0.0.1 recibió la página interna y por la IP de la máquina (172.30.0.2) falló con código 7. -L 0.0.0.0:..., -L *:..., -g y -o GatewayPorts=yes abrieron el puerto a todas las interfaces (curl por 172.30.0.2 dio la página con los tres primeros); GatewayPorts yes en el sshd_config del servidor no cambió nada (siguió en 127.0.0.1 y [::1]). curl, un curl lanzado como el usuario nobody y un socket Python, sin claves, usaron el túnel. Con el puerto 18081 ocupado en 127.0.0.1, ssh -f -N -L 18081:... salió con 0 aunque también con ExitOnForwardFailure=yes (enlazó [::1]); con -L 127.0.0.1:18081:... y ExitOnForwardFailure=yes salió con 255 y sin él quedó un ssh vivo (código 0) con el aviso Could not request local forwarding. Con AllowTcpForwarding remote o no (y con Match, y con permitopen en la clave) ssh siguió vivo y el puerto local escuchando, pero curl recibió una conexión cerrada sin datos (código 52 o 56, varía entre ejecuciones) y sshd registró el rechazo. Mismos resultados en 3 de 3 ejecuciones.
Este artículo no contiene enlaces de afiliado.
Un túnel ssh -L parece una operación sin riesgo: abres un puerto local y el tráfico viaja cifrado hasta un servicio interno. Lo que se suele dar por hecho (que solo tú lo usas, que ssh avisa si falla, que el servidor lo prohíbe con una línea) se puede medir. Aquí se hace contra un sshd temporal, con una página interna falsa como destino. Para otras comprobaciones de sshd_config, mira qué directiva gana cuando hay un Match y qué hace StrictModes con authorized_keys.
Condiciones: OpenSSH_9.6p1 Ubuntu-3ubuntu13.20 (cliente y servidor), Ubuntu 24.04.5 LTS, Linux 7.0.0-1017-oracle, bash 5.2.21, usuario normal (uid 10002) para ssh y sshd; solo una comprobación usa sudo -u nobody para lanzar un curl como otro usuario. El sshd se arranca con /usr/sbin/sshd -f y una configuración temporal: puerto 18022, ListenAddress 127.0.0.1, clave de host y de usuario recién generadas, UsePAM no, StrictModes no. Solo puede autenticar al usuario que lo lanza. El servicio "interno" es python3 -m http.server en 127.0.0.1:18080; el puerto reenviado es el 18081. Todo vive en un directorio de mktemp -d; la configuración real de sshd del sistema no se lee ni se toca, y al salir el script borra el directorio y mata los procesos (la última línea de la salida comprueba que no quedan procesos, ni el directorio, ni puertos abiertos). El script se ejecutó 3 veces y la salida fue idéntica en las 3 (comparada sin la cabecera; los puertos de origen de los clientes se sustituyen por N y los pid por N). No se probó -R ni -D, ProxyJump, ControlMaster, otro sistema operativo, otras versiones de OpenSSH, un servidor con PAM, otra máquina de la red como cliente (la IP de la máquina se probó desde la propia máquina), ni túneles hacia máquinas remotas reales: el destino siempre fue el propio loopback. El texto lo redacté con ayuda de una IA a partir de la salida del script, y las cifras salen de esas ejecuciones.
Salida de la ejecución 1
Idéntica en las ejecuciones 2 y 3. Antes de la primera ejecución el script comprueba que la página interna se ve en 127.0.0.1:18080 (SECRET-INTERNAL-PAGE) y que no se ve por la IP de la máquina, 172.30.0.2 (no).
[1] default bind address (ssh -N -L 18081:127.0.0.1:18080):
listening on: 127.0.0.1:18081 [::1]:18081
curl 127.0.0.1:18081 -> exit=0 body=SECRET-INTERNAL-PAGE
curl 172.30.0.2:18081 -> exit=7 body=(empty)
with -L localhost:18081:...:
listening on: 127.0.0.1:18081 [::1]:18081
[2] making it listen on all interfaces:
-L 0.0.0.0:18081:127.0.0.1:18080
listening on: 0.0.0.0:18081
curl 172.30.0.2:18081 -> exit=0 body=SECRET-INTERNAL-PAGE
-L *:18081:127.0.0.1:18080
listening on: 0.0.0.0:18081 [::]:18081
curl 172.30.0.2:18081 -> exit=0 body=SECRET-INTERNAL-PAGE
-g -L 18081:127.0.0.1:18080
listening on: 0.0.0.0:18081 [::]:18081
curl 172.30.0.2:18081 -> exit=0 body=SECRET-INTERNAL-PAGE
-o GatewayPorts=yes -L 18081:127.0.0.1:18080
listening on: 0.0.0.0:18081 [::]:18081
server side GatewayPorts yes in sshd_config, client plain -L 18081:127.0.0.1:18080:
listening on: 127.0.0.1:18081 [::1]:18081
[3] who can use the forwarded port (no key, no ssh, only a TCP client):
process using the key: ssh pid owned by uid 10002
plain curl without any credentials:
curl 127.0.0.1:18081 -> exit=0 body=SECRET-INTERNAL-PAGE
curl run as another local user (sudo -u nobody, uid 65534), no key, no ssh:
exit=0 body=SECRET-INTERNAL-PAGE
python socket from a different process, raw HTTP request:
reply body: SECRET-INTERNAL-PAGE
listener owner (ss -ltnp):
127.0.0.1:18081 users:(("ssh",pid=N,fd=5))
[::1]:18081 users:(("ssh",pid=N,fd=4))
[4] port already in use (another process holds 127.0.0.1:18081):
-f -N -L 18081:... (default bind: 127.0.0.1 and ::1), without ExitOnForwardFailure:
ssh exit=0
> bind [127.0.0.1]:18081: Address already in use
ssh still running afterwards: 1 process(es); listening: 127.0.0.1:18081 [::1]:18081
same with -o ExitOnForwardFailure=yes:
ssh exit=0
> bind [127.0.0.1]:18081: Address already in use
ssh still running afterwards: 1 process(es); listening: 127.0.0.1:18081 [::1]:18081
-L 127.0.0.1:18081:... (only the busy address), -f, without ExitOnForwardFailure:
ssh exit=0
> bind [127.0.0.1]:18081: Address already in use
> channel_setup_fwd_listener_tcpip: cannot listen to port: 18081
> Could not request local forwarding.
ssh still running afterwards: 1 process(es)
-L 127.0.0.1:18081:... with -o ExitOnForwardFailure=yes, -f:
ssh exit=255
> bind [127.0.0.1]:18081: Address already in use
> channel_setup_fwd_listener_tcpip: cannot listen to port: 18081
> Could not request local forwarding.
ssh still running afterwards: 0 process(es)
-L 127.0.0.1:18081:... without -f, without ExitOnForwardFailure, timeout 2 s:
exit=124 (124 = still running when timeout killed it)
> bind [127.0.0.1]:18081: Address already in use
> channel_setup_fwd_listener_tcpip: cannot listen to port: 18081
> Could not request local forwarding.
-L 127.0.0.1:18081:... without -f, with ExitOnForwardFailure=yes, timeout 2 s:
exit=255
> bind [127.0.0.1]:18081: Address already in use
> channel_setup_fwd_listener_tcpip: cannot listen to port: 18081
> Could not request local forwarding.
what answers on 127.0.0.1:18081 while the forward failed: OTHER-PROCESS-PAGE (the blocker's own directory, not the tunnel)
[5] -N versus a command:
ssh -L ... true (a command, no -N):
exit=0; port listening after it returned:
ssh -L ... sleep 2, curl during the sleep:
curl 127.0.0.1:18081 -> exit=0 body=SECRET-INTERNAL-PAGE
ssh -N -L ... (no remote command), timeout 2 s:
exit=124
ssh -N with a command appended (true):
exit=124 (the command is ignored if 124)
[6] AllowTcpForwarding in sshd_config:
sshd_config: AllowTcpForwarding yes
ssh still running: yes; local port listening: 127.0.0.1:18081 [::1]:18081
curl 127.0.0.1:18081 -> exit=0 body=SECRET-INTERNAL-PAGE
sshd_config: AllowTcpForwarding local
ssh still running: yes; local port listening: 127.0.0.1:18081 [::1]:18081
curl 127.0.0.1:18081 -> exit=0 body=SECRET-INTERNAL-PAGE
sshd_config: AllowTcpForwarding remote
ssh still running: yes; local port listening: 127.0.0.1:18081 [::1]:18081
curl 127.0.0.1:18081 -> exit=52 or 56 (connection closed with no data; the code varies between runs) body=(empty)
sshd log> refused local port forward: originator 127.0.0.1 port N, target 127.0.0.1 port 18080
sshd_config: AllowTcpForwarding no
ssh still running: yes; local port listening: 127.0.0.1:18081 [::1]:18081
curl 127.0.0.1:18081 -> exit=52 or 56 (connection closed with no data; the code varies between runs) body=(empty)
sshd log> refused local port forward: originator 127.0.0.1 port N, target 127.0.0.1 port 18080
yes globally, Match User agent -> no:
curl 127.0.0.1:18081 -> exit=52 or 56 (connection closed with no data; the code varies between runs) body=(empty)
sshd log> refused local port forward: originator 127.0.0.1 port N, target 127.0.0.1 port 18080
options on the key: restrict,port-forwarding,permitopen="127.0.0.1:9":
curl 127.0.0.1:18081 -> exit=52 or 56 (connection closed with no data; the code varies between runs) body=(empty)
sshd log> Received request from 127.0.0.1 port N to connect to host 127.0.0.1 port 18080, but the request was denied.
Lo que se ve
- Por defecto solo escucha en loopback.
ssh -N -L 18081:127.0.0.1:18080abrió127.0.0.1:18081y[::1]:18081.curlpor127.0.0.1devolvió la página interna; por172.30.0.2salió con código 7 (conexión rechazada).localhost:delante hizo lo mismo. - Hay cuatro formas de abrirlo a la red, y una que no funciona.
-L 0.0.0.0:18081:...,-L *:18081:...y-ghicieron quecurlpor172.30.0.2recibiera la página.-o GatewayPorts=yesabrió el puerto en0.0.0.0y[::]. En cambio,GatewayPorts yesen elsshd_configdel servidor, con un-Lnormal en el cliente, dejó el puerto en127.0.0.1y[::1]: esa directiva del servidor no afecta a-L(afecta a-R, no medido aquí). Un0.0.0.0:explícito basta; un túnel abierto así expone el servicio interno a quien llegue a esa máquina. - Quien llega al puerto usa el túnel sin credenciales. Un
curlnormal, uncurllanzado consudo -u nobody(uid 65534, otro usuario local) y un socket de Python, procesos distintos de ssh y sin clave alguna, recibieron la página. El que escucha es el procesossh(ss -ltnp). Dicho de otra forma: mientras el túnel esté abierto, el servicio interno queda al alcance de cualquier proceso que pueda conectar a ese puerto, no solo de quien tiene la clave. No se midió desde otra máquina de la red. - Con el puerto ocupado, ssh puede salir con 0 sin túnel. Con otro proceso en
127.0.0.1:18081,ssh -f -N -L 18081:...salió con código 0, imprimióbind [127.0.0.1]:18081: Address already in usey se quedó vivo escuchando solo en[::1]; con-o ExitOnForwardFailure=yespasó lo mismo: el túnel valía como éxito porque una de las dos direcciones se pudo enlazar. Con-L 127.0.0.1:18081:...(una sola dirección) y-f, sin la opción, ssh salió con 0, avisóCould not request local forwarding.y quedó vivo; conExitOnForwardFailure=yessalió con 255 y no quedó ningún proceso. Sin-f, sin la opción: tras 2 s seguía vivo (código 124 detimeout); con la opción: código 255. Mientras tanto,curlal puerto 18081 devolvió la página del otro proceso (OTHER-PROCESS-PAGE), que es lo que ocurre si un script confía en el túnel sin comprobar. -Nquita el comando remoto.ssh -L ... truesalió con 0 y el puerto dejó de escuchar al terminar el comando.ssh -L ... -- sleep 2sirvió la página durante elsleep.ssh -N -L ...siguió vivo pasados 2 s (124), y-Ncon un comando añadido (true) también: con-Nla sesión no terminó con el comando.AllowTcpForwardingno impide abrir el puerto local. Conyesylocalel túnel sirvió la página. Conremotey conno(también conMatch User ... AllowTcpForwarding no) ssh siguió vivo y el puerto local estaba escuchando, perocurlsalió con código 52 o 56 (conexión cerrada sin datos; el código varió entre ejecuciones, por eso el script imprime ambos) y el log de sshd (LogLevel VERBOSE) registrórefused local port forward ... target 127.0.0.1 port 18080. El cliente no imprimió nada, ni conLogLevel=INFO. Conrestrict,port-forwarding,permitopen="127.0.0.1:9"en la clave, el resultado para el cliente fue el mismo y el log dijo... but the request was denied.. El fallo solo se ve al usarlo, yExitOnForwardFailureno lo detecta (ssh still running: yes, con la opción activa).
Qué significa
- Si no quieres exponer el túnel, no escribas
0.0.0.0,*ni-g, y no pongasGatewayPorts=yesen tu~/.ssh/config. Si lo abres, filtra con firewall: lo medido es que no se pide ninguna credencial. - En un script o un servicio, usa
-o ExitOnForwardFailure=yesy una dirección explícita (-L 127.0.0.1:18081:...) para que un puerto ocupado pare el proceso. Conlocalhosto sin dirección, el túnel puede contar como establecido con solo[::1]. - Comprueba el túnel por su uso (un
curlal destino), no por el código de salida dessh -f: aquí salió con 0 con el puerto ocupado. - Usa
-Nsi solo quieres el reenvío. Sin él, el túnel dura lo que dura el comando. AllowTcpForwarding nose puede probar desde el cliente solo usando el túnel; en el servidor, el log lo registra conLogLevel VERBOSE. Es una restricción del servidor, no del puerto local.- Lo medido es OpenSSH 9.6p1 en Ubuntu 24.04.5; otras versiones pueden cambiar los mensajes.
Script completo
#!/bin/bash
# ssh -L pitfalls measured against a throwaway sshd run as the current non-root user
# (own port, own host key, own authorized_keys, all in mktemp -d, removed on exit).
# 1 default bind address; 2 explicit bind / -g / GatewayPorts; 3 who can use the port;
# 4 port already in use +/- ExitOnForwardFailure; 5 -N vs a command; 6 AllowTcpForwarding.
# The system sshd configuration is never read or modified. Usage: test-ssh-local-forward.sh
set -u
export LC_ALL=C.UTF-8
T=$(mktemp -d /tmp/sshfwd.XXXXXX) || exit 1
SP=18022 # throwaway sshd
WP=18080 # web service on the "remote" side, loopback only
FP=18081 # local forwarded port
PIDS=()
cleanup(){ [ -f "$T/sshd.pid" ] && kill "$(cat "$T/sshd.pid")" 2>/dev/null
for p in "${PIDS[@]:-}"; do [ -n "$p" ] && kill "$p" 2>/dev/null; done; pkill -u "$(id -u)" -f "$T" 2>/dev/null; sleep 0.5; rm -rf "$T"
echo "after cleanup: temp dir exists: $([ -e "$T" ] && echo yes || echo no); processes left: $(pgrep -u "$(id -u)" -f "$T" | wc -l); ports $SP/$WP/$FP listening: $(ss -ltn | grep -cE ":($SP|$WP|$FP) ")"; }
trap cleanup EXIT
EXTIP=$(hostname -I | awk '{print $1}')
echo "$(ssh -V 2>&1); $(. /etc/os-release; echo "$PRETTY_NAME"); $(uname -sr); bash ${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]}.${BASH_VERSINFO[2]}; uid $(id -u) (non-root: $([ "$(id -u)" != 0 ] && echo yes || echo no))"
ssh-keygen -q -t ed25519 -N '' -f "$T/hostkey"
ssh-keygen -q -t ed25519 -N '' -f "$T/userkey"
cp "$T/userkey.pub" "$T/authorized_keys"; chmod 600 "$T/authorized_keys"
mkdir "$T/www" "$T/www2"; echo "OTHER-PROCESS-PAGE" > "$T/www2/index.html"; echo "SECRET-INTERNAL-PAGE" > "$T/www/index.html"
python3 -m http.server "$WP" --bind 127.0.0.1 --directory "$T/www" >/dev/null 2>&1 & PIDS+=($!)
startsshd(){ # startsshd EXTRA_CONFIG_LINES
[ -f "$T/sshd.pid" ] && kill "$(cat "$T/sshd.pid")" 2>/dev/null; sleep 0.3
cat > "$T/sshd_config" <<CFG
Port $SP
ListenAddress 127.0.0.1
HostKey $T/hostkey
PidFile $T/sshd.pid
AuthorizedKeysFile $T/authorized_keys
UsePAM no
PasswordAuthentication no
StrictModes no
LogLevel VERBOSE
$1
CFG
/usr/sbin/sshd -f "$T/sshd_config" -E "$T/sshd.log" || { echo "sshd failed"; cat "$T/sshd.log"; exit 1; }
for i in $(seq 50); do ss -ltn "sport = :$SP" | grep -q LISTEN && break; sleep 0.1; done
}
SSHO=(-F /dev/null -i "$T/userkey" -o IdentitiesOnly=yes -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=ERROR -o BatchMode=yes -p "$SP")
mkargs(){ # mkargs OPTIONS... [-- COMMAND...] : builds ARGS; the destination sits between options and command
local o=(); while [ $# -gt 0 ] && [ "$1" != -- ]; do o+=("$1"); shift; done; [ "${1:-}" = -- ] && shift
ARGS=("${SSHO[@]}" "${o[@]}" 127.0.0.1 "$@"); }
sshx(){ mkargs "$@"; ssh "${ARGS[@]}"; }
tsshx(){ local t=$1; shift; mkargs "$@"; timeout "$t" ssh "${ARGS[@]}"; }
fwd(){ # fwd LOGNAME ssh-args... : background ssh, wait until the local port listens or ssh exits
local log=$1; shift
mkargs "$@"; ssh "${ARGS[@]}" >"$T/$log.out" 2>"$T/$log.err" & FPID=$!; PIDS+=($FPID)
for i in $(seq 50); do ss -ltn "sport = :$FP" | grep -q LISTEN && return 0; kill -0 $FPID 2>/dev/null || return 1; sleep 0.1; done; return 1
}
killall_ssh(){ pkill -u "$(id -u)" -f "$T/userkey" 2>/dev/null; sleep 0.3; }
stopfwd(){ kill "$FPID" 2>/dev/null; wait "$FPID" 2>/dev/null; killall_ssh; for i in $(seq 30); do ss -ltn "sport = :$FP" | grep -q LISTEN || break; sleep 0.1; done; }
listen(){ ss -ltn "sport = :$FP" | awk 'NR>1{print $4}' | sort | tr '\n' ' '; }
get(){ # get HOST: HTTP fetch through the forwarded port, prints result
local o rc; o=$(curl -s -m 3 "http://$1:$FP/" 2>&1); rc=$?
case $rc in 52|56) rc="52 or 56 (connection closed with no data; the code varies between runs)";; esac
echo " curl $1:$FP -> exit=$rc body=${o:-(empty)}"
}
tgt=127.0.0.1:$WP
startsshd "AllowTcpForwarding yes"
SSHD_VER=$(ps -o args= -p "$(cat "$T/sshd.pid")" | sed "s#$T#TMP#g")
echo "throwaway sshd: $SSHD_VER; external address of this host: $EXTIP"
echo "direct access to the internal service from this host: $(curl -s -m 3 http://127.0.0.1:$WP/)"
echo "internal service reachable via external address $EXTIP:$WP: $(curl -s -m 3 "http://$EXTIP:$WP/" || echo no)"
for r in 1 2 3; do
echo "=== run $r ==="
echo "[1] default bind address (ssh -N -L $FP:$tgt):"
fwd a -N -L $FP:$tgt && echo " listening on: $(listen)"
get 127.0.0.1; get "$EXTIP"; stopfwd
echo " with -L localhost:$FP:...:"; fwd a -N -L localhost:$FP:$tgt && echo " listening on: $(listen)"; stopfwd
echo "[2] making it listen on all interfaces:"
for spec in "0.0.0.0:$FP:$tgt" "*:$FP:$tgt"; do
echo " -L $spec"; fwd a -N -L "$spec" && echo " listening on: $(listen)"; get "$EXTIP"; stopfwd
done
echo " -g -L $FP:$tgt"; fwd a -N -g -L $FP:$tgt && echo " listening on: $(listen)"; get "$EXTIP"; stopfwd
echo " -o GatewayPorts=yes -L $FP:$tgt"; fwd a -N -o GatewayPorts=yes -L $FP:$tgt && echo " listening on: $(listen)"; stopfwd
echo " server side GatewayPorts yes in sshd_config, client plain -L $FP:$tgt:"
startsshd $'AllowTcpForwarding yes\nGatewayPorts yes'; fwd a -N -L $FP:$tgt && echo " listening on: $(listen)"; stopfwd
startsshd "AllowTcpForwarding yes"
echo "[3] who can use the forwarded port (no key, no ssh, only a TCP client):"
fwd a -N -L $FP:$tgt
echo " process using the key: ssh pid owned by uid $(ps -o uid= -p $FPID | tr -d ' ')"
echo " plain curl without any credentials:"; get 127.0.0.1
if sudo -n true 2>/dev/null; then
echo " curl run as another local user (sudo -u nobody, uid $(sudo -n -u nobody id -u)), no key, no ssh:"
o=$(sudo -n -u nobody curl -s -m 3 "http://127.0.0.1:$FP/" 2>&1); echo " exit=$? body=${o:-(empty)}"
fi
echo " python socket from a different process, raw HTTP request:"
echo " $(python3 - <<PY
import socket
s=socket.create_connection(("127.0.0.1",$FP),timeout=3)
s.sendall(b"GET / HTTP/1.0\r\n\r\n"); b=b""
while True:
c=s.recv(4096)
if not c: break
b+=c
d=b.decode().split("\r\n\r\n")[1].strip()
print("reply body:",d)
PY
)"
echo " listener owner (ss -ltnp):"; ss -ltnp "sport = :$FP" 2>/dev/null | awk 'NR>1{print " "$4, $NF}' | sed 's/pid=[0-9]*/pid=N/'
stopfwd
echo "[4] port already in use (another process holds 127.0.0.1:$FP):"
python3 -m http.server $FP --bind 127.0.0.1 --directory "$T/www2" >/dev/null 2>&1 & BLK=$!; PIDS+=($BLK); sleep 0.5
echo " -f -N -L $FP:... (default bind: 127.0.0.1 and ::1), without ExitOnForwardFailure:"
sshx -f -N -L $FP:$tgt >"$T/u.out" 2>"$T/u.err"; echo " ssh exit=$?"; sed 's/^/ > /' "$T/u.err"
echo " ssh still running afterwards: $(pgrep -u "$(id -u)" -f "$T/userkey" | wc -l) process(es); listening: $(listen)"; killall_ssh
echo " same with -o ExitOnForwardFailure=yes:"
sshx -o ExitOnForwardFailure=yes -f -N -L $FP:$tgt >"$T/u.out" 2>"$T/u.err"; echo " ssh exit=$?"; sed 's/^/ > /' "$T/u.err"
echo " ssh still running afterwards: $(pgrep -u "$(id -u)" -f "$T/userkey" | wc -l) process(es); listening: $(listen)"; killall_ssh
echo " -L 127.0.0.1:$FP:... (only the busy address), -f, without ExitOnForwardFailure:"
sshx -f -N -L 127.0.0.1:$FP:$tgt >"$T/u.out" 2>"$T/u.err"; echo " ssh exit=$?"; sed 's/^/ > /' "$T/u.err"
echo " ssh still running afterwards: $(pgrep -u "$(id -u)" -f "$T/userkey" | wc -l) process(es)"; killall_ssh
echo " -L 127.0.0.1:$FP:... with -o ExitOnForwardFailure=yes, -f:"
sshx -o ExitOnForwardFailure=yes -f -N -L 127.0.0.1:$FP:$tgt >"$T/u.out" 2>"$T/u.err"; echo " ssh exit=$?"; sed 's/^/ > /' "$T/u.err"
echo " ssh still running afterwards: $(pgrep -u "$(id -u)" -f "$T/userkey" | wc -l) process(es)"; killall_ssh
echo " -L 127.0.0.1:$FP:... without -f, without ExitOnForwardFailure, timeout 2 s:"
tsshx 2 -N -L 127.0.0.1:$FP:$tgt 2>"$T/u.err"; echo " exit=$? (124 = still running when timeout killed it)"; sed 's/^/ > /' "$T/u.err"
echo " -L 127.0.0.1:$FP:... without -f, with ExitOnForwardFailure=yes, timeout 2 s:"
tsshx 2 -o ExitOnForwardFailure=yes -N -L 127.0.0.1:$FP:$tgt 2>"$T/u.err"; echo " exit=$?"; sed 's/^/ > /' "$T/u.err"
echo " what answers on 127.0.0.1:$FP while the forward failed: $(curl -s -m 3 http://127.0.0.1:$FP/) (the blocker's own directory, not the tunnel)"
kill $BLK; wait $BLK 2>/dev/null; sleep 0.3
echo "[5] -N versus a command:"
echo " ssh -L ... true (a command, no -N):"
sshx -L $FP:$tgt -- true; echo " exit=$?; port listening after it returned: $(listen)"
echo " ssh -L ... sleep 2, curl during the sleep:"
( sshx -L $FP:$tgt -- sleep 2 & sleep 1; get 127.0.0.1; wait )
echo " ssh -N -L ... (no remote command), timeout 2 s:"
tsshx 2 -N -L $FP:$tgt; echo " exit=$?"
echo " ssh -N with a command appended (true):"
tsshx 3 -N -L $FP:$tgt -- true; echo " exit=$? (the command is ignored if 124)"
echo "[6] AllowTcpForwarding in sshd_config:"
for v in yes local remote no; do
startsshd "AllowTcpForwarding $v"
echo " sshd_config: $(grep '^AllowTcpForwarding' "$T/sshd_config")"
fwd a -o LogLevel=INFO -o ExitOnForwardFailure=yes -N -L $FP:$tgt; echo " ssh still running: $(kill -0 $FPID 2>/dev/null && echo yes || echo no); local port listening: $(listen)"
get 127.0.0.1; sleep 0.3; sed 's/^/ ssh stderr> /' "$T/a.err"; grep -i "refused\|administratively\|permitopen\|denied" "$T/sshd.log" | sed 's/^[^ ]* [^ ]* sshd\[[0-9]*\]: //;s/\(originator\|from\) 127.0.0.1 port [0-9]*/\1 127.0.0.1 port N/' | sort -u | sed 's/^/ sshd log> /'; : > "$T/sshd.log"; : > "$T/a.err"; stopfwd
done
startsshd $'AllowTcpForwarding yes\nMatch User '"$(id -un)"$'\n AllowTcpForwarding no'
echo " yes globally, Match User $(id -un) -> no:"
fwd a -o LogLevel=INFO -N -L $FP:$tgt; get 127.0.0.1; sleep 0.3; sed 's/^/ ssh stderr> /' "$T/a.err"; grep -i "refused\|administratively\|permitopen\|denied" "$T/sshd.log" | sed 's/^[^ ]* [^ ]* sshd\[[0-9]*\]: //;s/\(originator\|from\) 127.0.0.1 port [0-9]*/\1 127.0.0.1 port N/' | sort -u | sed 's/^/ sshd log> /'; : > "$T/sshd.log"; stopfwd
startsshd "AllowTcpForwarding yes"
echo " options on the key: restrict,port-forwarding,permitopen=\"127.0.0.1:9\":"
echo "restrict,port-forwarding,permitopen=\"127.0.0.1:9\" $(cat "$T/userkey.pub")" > "$T/authorized_keys"
fwd a -o LogLevel=INFO -N -L $FP:$tgt; get 127.0.0.1; sleep 0.3; sed 's/^/ ssh stderr> /' "$T/a.err"; grep -i "refused\|administratively\|permitopen\|denied" "$T/sshd.log" | sed 's/^[^ ]* [^ ]* sshd\[[0-9]*\]: //;s/\(originator\|from\) 127.0.0.1 port [0-9]*/\1 127.0.0.1 port N/' | sort -u | sed 's/^/ sshd log> /'; : > "$T/sshd.log"; stopfwd
cp "$T/userkey.pub" "$T/authorized_keys"
done
Este artículo no contiene enlaces de afiliado.