SSH Doctor
当 SSH 连接在认证前关闭、远程登录似乎已开启但无法使用、或需要对本地/远程 Mac 的 SSH 进行诊断时使用。
规则
- 不要打印密钥、令牌、完整环境变量或大范围 grep 输出。
- 先在本地验证:本地环回失败意味着服务端 sshd/launchd/配置问题;本地环回成功但远程失败意味着网络/防火墙/过滤/监听路径问题。
- 在修改
/etc/ssh/sshd_config之前,先报告可疑的配置行。 - 优先使用非交互式 SSH:
ssh -o RequestTTY=no -o RemoteCommand=none HOST 'hostname; id -un'
基线
hostname; id -un; sw_vers
ipconfig getifaddr en0
ipconfig getifaddr en1 2>/dev/null || true
ipconfig getifaddr en7 2>/dev/null || true
sudo systemsetup -getremotelogin
sudo systemsetup -setremotelogin on
sudo launchctl print system/com.openssh.sshd 2>&1 | head -80
sudo launchctl kickstart -k system/com.openssh.sshd
sudo lsof -nP -iTCP:22 -sTCP:LISTEN
nc -vz 127.0.0.1 22
ssh -4 -F /dev/null -o RequestTTY=no -o RemoteCommand=none [email protected] 'hostname; id -un'
仅在密码回退会导致挂起或提示时使用 BatchMode=yes。
配置
sudo sshd -T 2>&1 | egrep -i '^(allowusers|denyusers|allowgroups|denygroups|listenaddress|maxstartups|logingracetime|usepam|passwordauthentication|pubkeyauthentication|authenticationmethods)'
sudo egrep -n '^[[:space:]]*(AllowUsers|DenyUsers|AllowGroups|DenyGroups|Match|MaxStartups|LoginGraceTime|ListenAddress|AuthenticationMethods|UsePAM|PasswordAuthentication|PubkeyAuthentication)\b' /etc/ssh/sshd_config /etc/ssh/sshd_config.d/* 2>/dev/null || true
可疑项:
DenyUsers匹配了目标用户- 限制性
AllowUsers/AllowGroups Match块意外应用MaxStartups值过小LoginGraceTime过小ListenAddress缺少目标接口
日志
sudo log show --last 30m --predicate 'process == "sshd" OR process == "launchd"' --style compact | tail -160
重要的 Mac 症状:
- 客户端:
kex_exchange_identification: Connection closed by remote host - 服务器日志:
Could not create new instance of inetd service: 67: Too many processes launchctl print system/com.openssh.sshd:copy count过高- 大量 PID 为 1 的
sshd-session: USER进程
这意味着 launchd 接受了 TCP 但拒绝生成更多 sshd inetd 副本。
修复陈旧 sshd-session
先检查:
sudo launchctl print system/com.openssh.sshd 2>&1 | egrep 'active count|copy count|state =|last exit code|runs ='
ps -axo pid,ppid,uid,user,state,lstart,etime,comm,args | awk '/sshd-session:/ && !/awk/ {print}'
sudo lsof -nP -c sshd-session -iTCP 2>/dev/null | head -120
如果陈旧会话明显滞留并阻塞新 SSH,通过匹配命令行来终止:
ps -axo pid=,args= | awk '/sshd-session: / && !/awk/ {print $1}' | xargs sudo kill -TERM
sleep 2
ps -axo pid=,args= | awk '/sshd-session: / && !/awk/ {print}'
如果 TERM 留下阻塞进程,在检查所有权和活动 shell 后使用 KILL。
防火墙
仅在环回成功但远程失败时执行:
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --listapps | grep -i ssh -A2 -B2 || true
sudo pfctl -sr 2>/dev/null | head -80
sudo pfctl -si 2>/dev/null | head -80
同时检查监听地址和目标接口:
ifconfig | awk '/^[a-z0-9]+:/{iface=$1; sub(":","",iface)} iface ~ /^en[0-9]+$/ && /inet / {print iface, $2}'
sudo lsof -nP -iTCP:22 -sTCP:LISTEN
OP Profile 块
如果要求确保 ~/.profile 包含从另一台主机复制的由 Codex 管理的 OP_SERVICE_ACCOUNT_TOKEN:
- 验证确切的变量和标记,不打印值
- 仅复制匹配的行/块
- 通过
chmod 600的临时文件重定向 - 绝不回显令牌
存在性检查:
awk 'BEGIN{b=0;e=0;x=0} /BEGIN Codex-managed OP_SERVICE_ACCOUNT_TOKEN/ {b=1} /END Codex-managed OP_SERVICE_ACCOUNT_TOKEN/ {e=1} /^[[:space:]]*(export[[:space:]]+)?OP_SERVICE_ACCOUNT_TOKEN=/ {x=1} END{print "marker_begin", b; print "marker_end", e; print "exact_var", x}' ~/.profile
从远程主机追加:
tmpfile=$(mktemp /tmp/codex-op-token.XXXXXX)
chmod 600 "$tmpfile"
ssh -o RequestTTY=no -o RemoteCommand=none HOST 'awk '\''/^[[:space:]]*(export[[:space:]]+)?OP_SERVICE_ACCOUNT_TOKEN=/ {print; exit}'\'' ~/.profile' > "$tmpfile"
if [ -s "$tmpfile" ]; then
{
printf '\n# BEGIN Codex-managed OP_SERVICE_ACCOUNT_TOKEN\n'
sed -n '1p' "$tmpfile"
printf '# END Codex-managed OP_SERVICE_ACCOUNT_TOKEN\n'
} >> ~/.profile
fi
rm -f "$tmpfile"
关闭报告
报告:
- 根本原因
- 确切修改的命令
- 验证输出(根据需要省略敏感信息)
- 是否应重试远程连接