macOS 命令速查
安装 Homebrew
Section titled “安装 Homebrew”新机第一件事。官方安装脚本会自行处理 Command Line Tools 依赖。
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Apple Silicon 上 Homebrew 装在 /opt/homebrew,需要把它写进当前 shell 的环境:
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofileeval "$(/opt/homebrew/bin/brew shellenv)"用 Brewfile 批量装机
Section titled “用 Brewfile 批量装机”把已装的东西导出成清单,换机时一条命令还原,比逐个 brew install 可靠得多。
# Export what is currently installedbrew bundle dump --describe --force --file=~/Brewfile
# Restore on a new machinebrew bundle install --file=~/Brewfile
# Remove anything not listed in the Brewfilebrew bundle cleanup --force --file=~/Brewfile生成 SSH 密钥
Section titled “生成 SSH 密钥”默认用 ed25519,比 RSA 更短也更快。注释里写清楚这把钥匙属于哪台机器。
ssh-keygen -t ed25519 -C "laptop-$(date +%Y%m)" -f ~/.ssh/id_ed25519
# Load it into the agent and the macOS keychainssh-add --apple-use-keychain ~/.ssh/id_ed25519
# Copy the public key to the clipboardpbcopy < ~/.ssh/id_ed25519.pub启动开发服务器之前先确认端口是空的。
# Who is listening on port 4321lsof -nP -iTCP:4321 -sTCP:LISTEN
# Kill it once you are surekill -15 <PID>连上 Wi-Fi 却上不了网
Section titled “连上 Wi-Fi 却上不了网”典型症状:能连上 Wi-Fi,但什么都打不开;同一个网络下别人正常;这台机器换个网络也正常。这说明网卡和路由器本身都没坏,问题出在「这台机器 × 这个网络」这一对组合上。
有网的时候(比如手机热点)直接跑脚本,它会从网卡往上逐层探测,指出第一个断掉的层:
curl -fsSL https://hub.skillnav.dev/net.sh | sh
# Same run, plus the two safe repairs: flush DNS, renew an existing DHCP leasecurl -fsSL https://hub.skillnav.dev/net.sh | sh -s -- --fix脚本下载不下来的时候(网全断了就是这种情况),手敲下面四条,按同样的顺序判断:
# 1. Address, and whether it came from DHCP or was typed in by handipconfig getifaddr en0networksetup -getinfo "Wi-Fi" | head -n 5
# 2. Can the router be reached at allnetstat -nrf inet | awk '/^default/ {print $2; exit}'ping -c 2 $(netstat -nrf inet | awk '/^default/ {print $2; exit}')
# 3. Public IP, bypassing DNS entirelyping -c 2 223.5.5.5
# 4. Name resolution, and which servers are configurednetworksetup -getdnsservers "Wi-Fi"dscacheutil -q host -a name www.apple.com怎么读这四条:
| 卡在哪 | 结论 |
|---|---|
拿不到地址,或是 169.254.x.x |
DHCP 没应答。可以试 sudo ipconfig set en0 DHCP,但注意它会先拆掉该接口上已有的 IPv4 服务再建一个临时服务(见 man ipconfig),不是无害的续租;仍是这个地址就是路由器侧拦了这台机器 |
第二行显示 Manual Configuration,且网关 ping 不通 |
静态 IP 是别的网络留下的残留,sudo networksetup -setdhcp "Wi-Fi" 改回自动 |
| 网关 ping 不通 | 路由器按 MAC 过滤。「私有 Wi-Fi 地址」开着时,这台机器每次都像陌生设备 |
网关通、223.5.5.5 不通 |
Portal 认证没过,或这台设备被上游拦了 |
223.5.5.5 通、域名解析失败 |
DNS 问题。networksetup -getdnsservers 有值就说明手工填过别处的 DNS |
| 全通但浏览器打不开 | 代理残留,查 networksetup -getwebproxy "Wi-Fi" |
# Update formulae and upgrade everythingbrew update && brew upgrade
# Reclaim disk space from old versionsbrew cleanup --prune=all
# Check for problems before filing a bugbrew doctor