sshのport forwarding

ssh remote login以外にもport forwardすることも出来るんですね。でもバックドアのようにもなるので使い方には少し注意。sshd_configのGatewayPortsの設定次第で挙動が変わる。

The argument may be ``no'' to force remote port forwardings to be available to the local host only, ``yes'' to force remote port forwardings to bind to the wildcard address, or ``clientspecified'' to allow the client to select the address to which the forwarding is bound. The default is ``no''.

defaultではGatewayPorts no に設定されているので、bind できるlisten address はloopback のinterfaceのみ。また、複数のport をforwardする時は、-L/-R オプションを複数指定。GatewayPorts yes か ssh -g を指定するとloopback 以外のinterface をbindする。

local portをremote portに転送

sshのオプションに-Lをつける

ssh -L
  -L [bind_address:]port:host:hostport
ex: localhostの10000番portを全interfaceの20000番portにbind

softwareによってはlocalhostでしかlistenしないものがあるけど、諸事情により外部からアクセスしたいときとか。

$ ssh -g -L 20000:localhost:10000 user@localhost

こやって-Lでforwardするportを指定してlocalhostsshすれば、software側を変更せずに、listenしてるportやinterfaceを変更できる。

remote portをlocal portに転送

sshのオプションに-Rをつける

ssh -R
  -R [bind_address:]port:host:hostport