用 security find-generic-password 读取钥匙串密码

无需打开"钥匙串访问",直接在命令行读取 macOS 钥匙串里保存的应用密码。

macOS密码钥匙串安全中风险
运行 `security find-generic-password -s "MyService" -w`,即可在终端打印该服务对应的密码。

01命令

命令
$ security find-generic-password -s "MyService" -w

02作用说明

security 是钥匙串的命令行接口。find-generic-password 可以按服务(-s)、账户(-a)或两者组合匹配条目;加 -w 会直接输出密码明文,不加则只显示创建时间、访问控制等元数据。这是脚本获取凭据的标准做法,避免把密码硬编码在脚本里。

03风险

风险中风险
使用条件Built-in on macOS; the Keychain item must exist and (on first access) you must approve the prompt.
!注意: 密码会以明文出现在屏幕上,并可能被记录到 shell 历史或日志中,请勿在共享环境中执行。

04撤销

✓无需撤销——该命令不会改变持久状态。

05来源

最后验证: 2026-09-17 · 分类: 安全与隐私

06相关方案