git使用ssh协议,生成公钥和私钥,并指定私钥

查看版本

$ ssh -V
OpenSSH_7.6p1, OpenSSL 1.0.2n 7 Dec 2017

http://superuser.com/questions/232373/how-to-tell-git-which-private-key-to-use

In ~/.ssh/config, add:

host github.com
 HostName github.com
 IdentityFile ~/.ssh/id_rsa_github
 User git

Now you can do git clone git@github.com:username/repo.git.

NOTE: Verify that the permissions on 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

https://confluence.atlassian.com/bitbucket/set-up-ssh-for-git-728138079.html

Step 1. Ensure you have an SSH client installed

SSH should be included with the version of Git you installed. To make sure, do the following to verify your installation:

  1. From the Git Bash window, enter the following command to verify that SSH client is available:

    $ ssh -v 
    OpenSSH_4.6p1, OpenSSL 0.9.8e 23 Feb 2007
    usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]
    [-D [bind_address:]port] [-e escape_char] [-F configfile]
    [-i identity_file] [-L [bind_address:]port:host:hostport]
    [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
    [-R [bind_address:]port:host:hostport] [-S ctl_path]
    [-w local_tun[:remote_tun]] [user@]hostname [command]

    If you have ssh installed, the terminal returns version information.

    If you don't have ssh installed, install it now with your package manager.

  2. List the contents of your ~/.ssh directory.
    If you have not used SSH on Git Bash yet, you might see something like this:

    $ ls -a ~/.ssh 
    ls: /c/Users/emmap1/.ssh: No such file or directory

    If you have a default identity already, you'll see two id_* files:

    $ ls -a ~/.ssh 
    .    ..    id_rsa    id_rsa.pub  known_hosts

    In this case, the default identity uses RSA encryption (id_rsa.pub). If you want to use an existing default identity for your Bitbucket account, skip the next section and go to create a config file.

Step 2. Set up your default identity 升成公钥和私钥

By default, the system adds keys for all identities to the /Users/<yourname>/.ssh directory. The following procedure creates a default identity.

  1. Open a terminal in your local system.
  2. Enter ssh-keygen at the command line. 
    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):
  3. Press enter to accept the default key and path, /c/Users/<yourname>/.ssh/id_rsa, or you can create a key with another name.
    To create a key with a name 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 a path like this at the prompt:

    $ ssh-keygen 
    Generating public/private rsa key pair.
    Enter file in which to save the key (/c/Documents and Settings/emmap1/.ssh/id_rsa): /c/Users/emmap1/My Documents/keys/my-new-ssh-key
  4. Enter and renter a passphrase when prompted.
    Unless you need a key for a process such as script, you should always provide a passphrase. 
    The command creates your default identity with its public and private keys. The whole interaction looks similar to the following: 

    $ 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
  5. List the contents of ~/.ssh to view the key files.
    You should see something like the following:

    $ ls ~/.ssh 
    id_rsa id_rsa.pub

    The command created two files, one for the public key (for example id_rsa.pub) and one for the private key (for example, id_rsa).

Step 3. Create a SSH config file  配置私钥路径

  1. Using your favorite text editor, create a new file (at ~/.ssh/config) or edit the file if it already exists.

  2. Add an entry to the configuration file using the following format:

     
     
     
     
     
    1
    Host bitbucket.org
    2
     IdentityFile ~/.ssh/<privatekeyfile>
     
     

    The second line is indented. That indentation (a single space) is important, so make sure you include it. The second line is the location of your private key file.  If you are following along with these instructions, enter id_rsa for<privatekeyfile>.

    When you are done editing, your configuration looks similar to the following:

     
     
     
     
     
    1
    Host bitbucket.org
    2
     IdentityFile ~/.ssh/id_rsa
     
     
  3. Save and close the file.
  4. Restart the GitBash terminal.

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 Save.

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的私钥

 

原文地址:https://www.cnblogs.com/chucklu/p/5082429.html