用 security find-generic-password 读取钥匙串密码
无需打开"钥匙串访问",直接在命令行读取 macOS 钥匙串里保存的应用密码。
运行 `security find-generic-password -s "MyService" -w`,即可在终端打印该服务对应的密码。
01命令
命令
$ security find-generic-password -s "MyService" -w02作用说明
security 是钥匙串的命令行接口。find-generic-password 可以按服务(-s)、账户(-a)或两者组合匹配条目;加 -w 会直接输出密码明文,不加则只显示创建时间、访问控制等元数据。这是脚本获取凭据的标准做法,避免把密码硬编码在脚本里。
03风险
!注意: 密码会以明文出现在屏幕上,并可能被记录到 shell 历史或日志中,请勿在共享环境中执行。
04撤销
✓无需撤销——该命令不会改变持久状态。
05来源
- security — Keychain and CMS command interface (macOS)依据: Documents find-generic-password with -s (service), -a (account) and -w (write password to stdout).
最后验证: 2026-09-17 · 分类: 安全与隐私