到远程 Ubuntu 的 SSH RemoteForward 端口不起作用

到远程 Ubuntu 的 SSH RemoteForward 端口不起作用

我的本地计算机是 mac,我正在连接到名为 mini 的远程 Ubuntu 20.04.2 LTS 计算机。

以下是 mini 的 ~/.ssh/config 部分

Host mini
    User root
    RemoteForward 8377 localhost:8377

我用这个命令来 ssh 到 mini

/usr/bin/ssh -v mini

我已经使用命令禁用了防火墙:

ufw disable

在迷你车内,我运行命令:

echo "hello from mini" | nc localhost 8377 

它会挂在日志上

debug1: channel 1: connected to localhost port 8377

以下是整个日志:

➜  ~ /usr/bin/ssh -v mini
OpenSSH_8.1p1, LibreSSL 2.7.3
debug1: Reading configuration data /Users/huangyingw/.ssh/config
debug1: /Users/huangyingw/.ssh/config line 1: Applying options for *
debug1: /Users/huangyingw/.ssh/config line 16: Applying options for mini
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 47: Applying options for *
debug1: Connecting to mini port 22.
debug1: Connection established.
debug1: identity file /Users/huangyingw/.ssh/id_rsa type 0
debug1: identity file /Users/huangyingw/.ssh/id_rsa-cert type -1
debug1: identity file /Users/huangyingw/.ssh/id_dsa type 1
debug1: identity file /Users/huangyingw/.ssh/id_dsa-cert type -1
debug1: identity file /Users/huangyingw/.ssh/id_ecdsa type -1
debug1: identity file /Users/huangyingw/.ssh/id_ecdsa-cert type -1
debug1: identity file /Users/huangyingw/.ssh/id_ed25519 type -1
debug1: identity file /Users/huangyingw/.ssh/id_ed25519-cert type -1
debug1: identity file /Users/huangyingw/.ssh/id_xmss type -1
debug1: identity file /Users/huangyingw/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.1
debug1: Remote protocol version 2.0, remote software version OpenSSH_8.2p1 Ubuntu-4ubuntu0.2
debug1: match: OpenSSH_8.2p1 Ubuntu-4ubuntu0.2 pat OpenSSH* compat 0x04000000
debug1: Authenticating to mini:22 as 'root'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:jujpF3v9y4BICKUWUni22IcQ4gfZiSmmyvlgtUpoZrA
Warning: Permanently added 'mini,192.168.31.140' (ECDSA) to the list of known hosts.
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Skipping ssh-dss key /Users/huangyingw/.ssh/id_dsa - not in PubkeyAcceptedKeyTypes
debug1: Will attempt key: /Users/huangyingw/.ssh/id_rsa RSA SHA256:EYHtlJ61GJ+TvioA3aQZqXXqomkcUmGK7W6uJmFaqYo
debug1: Will attempt key: /Users/huangyingw/.ssh/id_ecdsa
debug1: Will attempt key: /Users/huangyingw/.ssh/id_ed25519
debug1: Will attempt key: /Users/huangyingw/.ssh/id_xmss
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,[email protected],ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected]>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Offering public key: /Users/huangyingw/.ssh/id_rsa RSA SHA256:EYHtlJ61GJ+TvioA3aQZqXXqomkcUmGK7W6uJmFaqYo
debug1: Server accepts key: /Users/huangyingw/.ssh/id_rsa RSA SHA256:EYHtlJ61GJ+TvioA3aQZqXXqomkcUmGK7W6uJmFaqYo
debug1: Authentication succeeded (publickey).
Authenticated to mini ([192.168.31.140]:22).
debug1: Remote connections from LOCALHOST:8377 forwarded to local address localhost:8377
debug1: channel 0: new [client-session]
debug1: Requesting [email protected]
debug1: Entering interactive session.
debug1: pledge: network
debug1: client_input_global_request: rtype [email protected] want_reply 0
debug1: Remote: /root/.ssh/authorized_keys:1: key options: agent-forwarding port-forwarding pty user-rc x11-forwarding
debug1: Remote: /root/.ssh/authorized_keys:1: key options: agent-forwarding port-forwarding pty user-rc x11-forwarding
debug1: remote forward success for: listen 8377, connect localhost:8377
debug1: All remote forwarding requests processed
debug1: Sending environment.
debug1: Sending env LC_TERMINAL_VERSION = 3.4.6
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending env LC_TERMINAL = iTerm2
debug1: Sending env LC_ALL = en_US.UTF-8
Welcome to Ubuntu 20.04.2 LTS (GNU/Linux 5.8.0-55-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

18 updates can be applied immediately.
To see these additional updates run: apt list --upgradable

Your Hardware Enablement Stack (HWE) is supported until April 2025.
Last login: Fri Jun 11 23:08:36 2021 from 192.168.31.147
➜  ~ echo "hello from mini" | nc localhost 8377
debug1: client_input_channel_open: ctype forwarded-tcpip rchan 3 win 2097152 max 32768
debug1: client_request_forwarded_tcpip: listen localhost port 8377, originator 127.0.0.1 port 41210
debug1: connect_next: host localhost ([::1]:8377) in progress, fd=11
debug1: channel 1: new [127.0.0.1]
debug1: confirm forwarded-tcpip
debug1: channel 1: connected to localhost port 8377

答案1

您不需要~/.ssh/config在 mini ubuntu 上创建的条目。您可以安全地删除这些条目。要将 TCP 端口 8377 从远程主机 (mini ubuntu) 转发到本地主机 (mac 笔记本电脑) 上的 8377,请从 mac 笔记本电脑执行以下命令:

$ ssh -fnN -L 8377:localhost:8377 user@mini

其中-fnN将在没有 TTY 的情况下将 ssh 发送到后台, -L将端口转发到本地主机,user@mini是迷你 ubuntu 的用户帐户和主机名/IP 地址。

然后就可以在mini ubuntu上执行netcat命令了

$ echo "hello from mini" | nc localhost 8377

PS:mini ubuntu 上不需要禁用防火墙,只要可以访问 SSH 服务即可,默认情况下总是如此。

相关内容