Ssh

在 mac 上啟動 sshd

  • August 13, 2018

我無法在我的 Mac 上通過 ssh 連接到 localhost。我發現機器中沒有執行 sshd(沒有程序在埠 22 上執行)。

lsof -i:22

我發現一些文章要求Remote LoginSystem Preferences -> Sharing. 即使它已啟用,我也無法 ssh 到 localhost。我得到的錯誤是這個

$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

我在這裡有什麼遺漏嗎?

我找到了解決方案

我嘗試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

這為我解決了這個問題。

引用自:https://unix.stackexchange.com/questions/461851