我无法在 Mac 上通过 ssh 访问本地主机。我发现sshd没有在机器中运行(端口22上没有进程运行)。
lsof -i:22
我发现一些帖子要求Remote Login
启用System Preferences -> Sharing
.尽管已启用,但我无法 ssh 到本地主机。我收到的错误是这样的
$ssh -v localhost
OpenSSH_7.6p1, LibreSSL 2.6.2
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 48: Applying options for *
debug1: Connecting to localhost port 22.
debug1: Connection established.
debug1: identity file /Users/gkumar6/.ssh/id_rsa type 0
debug1: key_load_public: No such file or directory
debug1: identity file /Users/gkumar6/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/gkumar6/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/gkumar6/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/gkumar6/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/gkumar6/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/gkumar6/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/gkumar6/.ssh/id_ed25519-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.6
ssh_exchange_identification: read: Connection reset by peer
我在这里缺少什么吗?
答案1
我找到了这个问题的解决方案
我尝试sshd
手动运行该进程,监听不同的端口来调试问题。
sudo /usr/sbin/sshd -d -p 2222
它显示了实际的问题。
debug1: sshd version OpenSSH_7.6, LibreSSL 2.6.2
debug1: private host key #0: ssh-rsa SHA256:oECJtUeeA3sNrAQj3phBiuWJoVl00dzLiXi20tlWF/o
debug1: private host key #1: ssh-dss SHA256:uVnKrOK+4V+y3QalyFdqDz+9eBN4oi2E3wb1MDBcgzc
debug1: private host key #2: ecdsa-sha2-nistp256 SHA256:CWapRrGUSJx2doJkDf2YR/aZy4BJ4j9K1/ZwX4eUZcg
debug1: private host key #3: ssh-ed25519 SHA256:sjzAks0Hud+Ah941pd8ZRNO6MWENdhO8wW4NMNDL2Ns
/var/empty must be owned by root and not group or world-writable.
该目录的权限/var/empty
是
drwxr-xr-x 7 gkumar6 sys 224B Aug 12 21:02 empty
所以我把它改成了sudo chown root:wheel /var/empty
drwxr-xr-x 7 root wheel 224B Aug 12 21:02 empty
这已经为我解决了问题。
答案2
您发布的日志显示安全 shell 守护进程是跑步:
debug1: Connection established.
如果不是的话,你就不会看到那条线;相反,会看到以 结尾的更简洁的日志Connection refused
。
您没有看到任何输出lsof
很可能是因为您没有以足够的权限运行该命令:
$ lsof -i:22
$ echo $?
1
$ sudo lsof -i:22
sudo lsof -i:22
Password:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
launchd 1 root 12u IPv6 0x2feb0cecf91e551b 0t0 TCP *:ssh (LISTEN)
launchd 1 root 16u IPv4 0x2feb0cecf91ec3a3 0t0 TCP *:ssh (LISTEN)
launchd 1 root 22u IPv6 0x2feb0cecf91e551b 0t0 TCP *:ssh (LISTEN)
launchd 1 root 23u IPv4 0x2feb0cecf91ec3a3 0t0 TCP *:ssh (LISTEN)
第二次尝试之前的语句echo
显示前一个命令返回退出代码1
,表示错误状态。
答案3
你加载sshd了吗?
launchctl load -w /System/Library/LaunchDaemons/ssh.plist