如何在zsh环境下自动运行ssh-agent?

79
我使用zsh和oh-my-zsh。 当我使用GitHub并想要使用密钥上传时, 我总是找不到

#git push
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

因为我还没有添加密钥

#ssh-add -l
Could not open a connection to your authentication agent.

所以我在想要推送或拉取时需要启动ssh-agent并添加密钥

#ssh-agent zsh
#ssh-add ~/.ssh/id_rsa

我该如何将这些命令添加到脚本中,以便无需手动输入命令?

1个回答

154

在文本编辑器中打开 .zshrc 文件:

vim ~/.zshrc

将ssh-agent添加到插件列表并保存:

plugins=(git ssh-agent)

您可能想立即重新加载您的.zshrc设置:

source ~/.zshrc

4
现在它可以运行ssh-agent,但是如何实现自动"ssh-add -l"呢? - wcc526
16
您可以使用以下命令:zstyle :omz:plugins:ssh-agent identities id_rsa id_rsa2 id_github(在此处找到:https://github.com/robbyrussell/oh-my-zsh/blob/master/plugins/ssh-agent/ssh-agent.plugin.zsh)。 - Olivier 'Ölbaum' Scherler
9
README 文档已经更加清晰易懂。https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/ssh-agent - Cobman
3
很遗憾,这在powerlevel10k上不起作用。 - Anutrix
1
这里有一些与Powerlevel10k有关的细节:https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/ssh-agent#powerline-10k-specific-settings - Natetronn
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接