用 sshpass 自动化 SSH 登录

在脚本或管道中提供 SSH 密码。

LinuxSSH 远程连接自动化安全低风险
运行 sshpass -p '密码' ssh user@host,无需提示即可登录。

01命令

命令
$ sshpass -p 'secret' ssh user@host

02作用说明

sshpass 把密码送入 ssh 的 TTY,适用于无法部署密钥或 askpass 辅助程序的旧式自动化。优先用 sshpass -f 文件或 -e 配合环境变量,避免在进程列表中暴露密码。

03风险

风险低风险
使用条件sudo apt install sshpass
!注意: 写在命令行上的密码对其他用户可见,也会进入 shell 历史;条件允许请改用 SSH 密钥。

04撤销

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

05来源

  • Debian package: sshpass
    Debian · vendor-doc · 2026-09-20
    依据: Debian packages sshpass, which supplies a password to SSH non-interactively.
  • sshpass
    Official project · vendor-doc · 2026-09-20
    依据: Debian packages sshpass, which supplies a password to SSH non-interactively.

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

06相关方案