git使用ssh协议,生成公钥和私钥,并指定私钥
查看版本
$ ssh -V
OpenSSH_7.6p1, OpenSSL 1.0.2n 7 Dec 2017
How to tell git which private key to use?
In ~/.ssh/config
, add:
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa_github
If the config file is new, you might need to do chmod 600 ~/.ssh/config
Now you can do git clone git@github.com:{ORG_NAME}/{REPO_NAME}.git
- Where
{ORG_NAME}
is your GitHub user account (or organization account)'s GitHub URI name.- Note that there is a colon
:
aftergithub.com
instead of the slash/
- as this is not a URI.
- Note that there is a colon
- And
{REPO_NAME}
is your GitHub repo's URI name - For example, for the Linux kernel this would be
git clone git@github.com:torvalds/linux.git
).
NOTE: On Linux and macOS, verify that the permissions on your IdentityFile
are 400. SSH will reject, in a not clearly explicit manner, SSH keys that are too readable. It will just look like a credential rejection. The solution, in this case, is:
chmod 400 ~/.ssh/id_rsa_github
Set up an SSH key
Set up SSH for Git on Windows
Use this section to create a default identity and SSH key when you're using Git on Windows. By default, the system adds keys for all identities to the /Users/<username>/.ssh
directory.
-
For Windows 7 or earlier
You can only enter
ssh-keygen
into the Git Bash window. It won't work in the Command prompt.The command prompts you for a file to save the key in:
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/emmap1/.ssh/id_rsa): -
Press enter to accept the default key and path,
/c/Users/<username>/.ssh/id_rsa
.
-
We recommend you keep the default key name unless you have a reason to change it.
To create a key with a name or path other than the default, specify the full path to the key. For example, to create a key called
my-new-ssh-key
, you would enter the Windows path, shown here:$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/emmap1/.ssh/id_rsa): c:\Users\emmap1\.ssh\my-new-ssh-key -
Enter and re-enter a passphrase when prompted.
The command creates your default identity with its public and private keys. The whole interaction looks similar to this:
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/emmap1/.ssh/id_rsa):
Created directory '/c/Users/emmap1/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/emmap1/.ssh/id_rsa.
Your public key has been saved in /c/Users/emmap1/.ssh/id_rsa.pub.
The key fingerprint is: e7:94:d1:a3:02:ee:38:6e:a4:5e:26:a3:a9:f4:95:d4 emmap1@EMMA-PC -
List the contents of
.ssh
to view the key files.You should see something like the following:
$ dir .ssh
id_rsa id_rsa.pubThe command displays two files, one for the public key (for example
id_rsa.pub
) and one for the private key (for example,id_rsa
).
Step 2. Add the key to the ssh-agent
If you don't want to type your password each time you use the key, you'll need to add it to the ssh-agent.
-
To start the agent, run the following:
$ eval $(ssh-agent)
Agent pid 9700 -
Enter
ssh-add
followed by the path to the private key file:$ ssh-add ~/.ssh/<private_key_file>
Step 3. Add the public key to your Account settings
- From Bitbucket, choose Personal settings from your avatar in the lower left.
The Account settings page opens. - Click SSH keys.
If you've already added keys, you'll see them on this page. -
Open your
.ssh/id_rsa.pub
file (or whatever you named the public key file) and copy its contents.
You may see an email address on the last line. It doesn't matter whether or not you include the email address. -
From Bitbucket, click Add key.
-
Enter a Label for your new key, for example,
Default public key
. - Paste the copied public key into the SSH Key field.
-
Click Save.
Bitbucket sends you an email to confirm the addition of the key.
-
Edit an SSH key
After you add a key, you can edit the key's Label but not the key itself. To change the key's contents, you need to delete and re-add the key.
-
Return to the command line and verify your configuration and username by entering the following command:
$ ssh -T git@bitbucket.org
The command message tells you which of your Bitbucket accounts can log in with that key.
conq: logged in as emmap1. You can use git or hg to connect to Bitbucket. Shell access is disabled.
If you get an error message with
Permission denied (publickey)
, check the Troubleshoot SSH issues page for help.
Now that you've got an SSH key set up, use the SSH URL the next time you clone a repository. If you already have a repository that you cloned over HTTPS, change the remote URL for your repository to its SSH URL.
GIT select private key to use
If you are connecting via SSH then the key will be controlled by an SSH parameter, not a git parameter.
SSH looks in the ~/.ssh/config
file for configuration parameters. Modify that file and add IdentityFile entries for the two Git servers like this:
Host server1.whatever.com
IdentityFile /path/to/key_1
Host server2.whatever.com
IdentityFile /path/to/key_2
This article has some more details.
TortoiseGit使用ssh
Appendix F. Tips and tricks for SSH/PuTTY
PuTTY comes with a great session management, where you can save attributes of connections (e.g. ssh key, username, port). This page describes how to make use of it - partly in form of a FAQ. For this to work, you need the PuTTY.exe-application.
How to use a default key for all SSH connections
Start PuTTY, go to Connection->SSH->Auth and select your key. Then go to Session, select Default Settings and hit
Now PuTTY (TortoiseGit and plink) will try to use this key for all new connections (no need to configure it in TortoiseGit). If the PuTTY agent is running, putty and plink try to use an already loaded key, but will ask for the password themselves (as a fallback).
配置单个repository的私钥
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了