Windows使用者的多數程式員喜歡安裝Git bash,用指令行控制。最近發現如果要操作多個git repos,需要生成多個MD5密鑰。在執行git操作時需要指定使用哪個密碼,通過以下設定可以實作。
Check that you are connecting to the correct server
使用下面的指令檢查是否連接配接到正确的domain:
$ ssh -vT ssh -vT [email protected]
OpenSSH_7.p2, OpenSSL .h May
debug1: Reading configuration data /c/Users/Amy Zhao/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to github.com [.] port .
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/Amy Zhao/.ssh/id_rsa type -
debug1: key_load_public: No such file or directory
...
Always use the “git” user
All connections must be made as the “git” user. If you try to connect with your GitHub username, it will fail:
$ ssh -T [email protected]
Permission denied (publickey)
Instead, you should verify your connection by typing:
$ ssh -T [email protected]
Hi your_username! You've successfully authenticated, but GitHub does not provide shell access.
Make sure you have a key that is being used
First, you should turn on ssh-agent:
# start the ssh-agent in the background
$ eval "$(ssh-agent -s)"
Agent pid
Verify that you have a private key generated and loaded into SSH. If you’re using OpenSSH 6.7 or older, typing:
$ ssh-add -l
a0:dd::c:a:d:e4:a:::e:::e:c8:d /Users/you/.ssh/id_rsa (RSA)
Or if you’re using OpenSSH 6.8 or newer:
$ ssh-add -l -E md5
MD5:a0:dd::c:a:d:e4:a:::e:::e:c8:d /Users/you/.ssh/id_rsa (RSA)
The
ssh-add
command should print out a long string of numbers and letters. If it doesn’t print anything, you’ll need to [generate a new SSH key][generate SSH keys] and associate it the GitHub.
Tip: On most systems the default private keys (~/.ssh/id_rsa, ~/.ssh/id_dsa and ~/.ssh/identity) are automatically added to the SSH authentication agent. You shouldn’t need to run ssh-add path/to/key unless you override the file name when you generate a key.
Auto-launching ssh-agent on Git for Windows
If you want to auto-start ssh-agent and add the private key you wanted, you can config your
~/.bashrc
file like below:
env=~/.ssh/agent.env
agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }
agent_start () {
(umask ; ssh-agent >| "$env")
. "$env" >| /dev/null ; }
agent_load_env
# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running
agent_run_state=$(ssh-add -l >| /dev/null >&; echo $?)
if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = ]; then
agent_start
ssh-add ~/.ssh/github-amy
ssh-add ~/.ssh/github_rsa
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = ]; then
ssh-add ~/.ssh/github-amy
ssh-add ~/.ssh/github_rsa
fi
unset env
Tip: If your private keys are not stored in ~/.ssh/id_rsa or ~/.ssh/id_dsa, you must add their paths with the ssh-add command so that your SSH authentication agent knows where to find them. For example:
ssh-add ~/.my_other_ssh/id_rsa
Now, when you first run Git Bash, your ssh-agent and identities was started automatically.
The
ssh-agent
process will continue to run until you log out, shut down your computer or kill the process.
If you want
ssh-agent
to forget your key after some time, you can configure it to do so by running
ssh-add -t <seconds>
.
參考
GitHub Help
Working with SSH key passphrases
Error: Permission denied (publickey)
Amy’s Blog