Troubleshooting Git local multi-ssh key

Source: Internet
Author: User
Tags ssh access ssh port ssh config

Recently, a project needs to use a server as a dedicated deployment server, encountered some problems in the implementation process, as follows:

1. The SSH default port of the server and the SSH port of the project Git repository are inconsistent 2. Deployment requires SSH key provided by the project and cannot use the default SSH key of the server itself

These problems have been solved successfully, here special record, prevent forgetting.

For the above issues, the following mainly from these three points to record the solution.

    1. How to generate SSH key

    2. How to pull items from a git repository using a specific SSH port

    3. How to pull a project from a git repository using a specific key file

First, generate SSH key

The default SSH key for the system is stored in the following directory:

[Email protected] ~]# CD ~/.ssh/[[email protected]. ssh]# Lsauthorized_keys id_rsa id_rsa.pub known_hosts

We will create a new. Git directory to hold the public key for GIT-related deployment key.

[[Email protected] .ssh]# mkdir ~/.git[[email protected] .ssh]# ssh-keygen  -t rsa -f ~/.git/pub_coding.id_rsa -C  "[email protected]" generating  Public/private rsa key pair. enter passphrase  (empty for no passphrase):  #  Enter enter same  passphrase again:                  #  return Your identification has been saved in /root/.git/pub_ Coding.id_rsa. Your public key has been saved in /root/.git/pub_coding.id_rsa.pub.the  key fingerprint is:b1:30:9c:9c:24:78:54:1e:b1:bb:d9:65:3a:44:8c:3b [email protected] The key ' s randomart image is:+--[ rsa 2048]----+|   oo.=.          | |   . .*  b         | |    .  @ +        | |        * o       | |       e s o      | |        * +       | |       o +        | |          .       | |                  |+------ -----------+[[email protected] .ssh]# ls ~/.git/pub_coding.id_rsa  pub_coding.id _rsa.pub

With these actions, the~/.git2 files were generated under the directory, wherepub_coding.id_rsaAs the private key,pub_coding.id_rsa.pubAs the public key.
We need to add the public key to the account of the relevant version control software.

Ii. using a specific SSH port to pull a project from a git repository

57832 is the default port for SSH, and the Git repository uses 22 as the default port for SSH.
In this case, users accessing the GIT repository using HTTPS will not be affected, but will result in users using SSH to access the Git repository to pull the project.

    1. Use ssh://to pull the project

      [[email protected] .ssh]# git clone ssh://[email protected]: Port number/user name/project name. Git 

      We can specify the port number of the corresponding SSH in the command above.

    2. Use SSH config to configure a custom port
      this way, and we'll put it in the section on managing multiple SSH keys.

Iii. using a specific key file to pull a project from a git repository 1, Introduction

> This question, in other words, is how Git uses multiple SSH keys. For the management of this multi-ssh key, we are currently implementing it primarily by defining the client configuration file for SSH.
We can define server aliases, server addresses, and some of the private connection configuration information used for specific servers in the client configuration file for SSH.
About SSH Client configuration file, we can through man config to get the relevant introduction, here is a simple part of the introduction.

NAME ssh_config-openssh SSH Client configuration filessynopsis ~/.ssh/config/etc/ssh/ssh_configdescription SSH (1) Obtains configuration data from the following sources in the following order:1. Command-Line Optio   NS 2.   User ' s configuration file (~/.ssh/config) 3. System-wide configuration file (/etc/ssh/ssh_config)

From the description, we can see that there are 2 client configuration files for SSH, namely~/.ssh/configAnd/etc/ssh/ssh_config。 They are a user-scoped configuration, and one is a system-wide configuration.
Since our operations are scoped to the user, use the~/.ssh/configFile.

2. Configuration

~/.ssh/config file does not exist by default and needs to be created by the user.
Sample file:

[[Email protected] ~]# touch ~/.ssh/config[[email protected]ame ~]# cat  ~/.ssh/config # github keyHost git-github    Port 22     user git        hostname git.github.com         PreferredAuthentications publickey         IdentityFile ~/.git/pub_github.id_rsa# coding keyHost  Git-coding        port 22    user git         HostName git.coding.net         PreferredAuthentications publickey         Identityfile ~/.git/pub_coding.id_rsa

host     It covers the configuration of the following segment, which we can use to replace the server address that will be connected.      You can use any field or wildcard character here.      when SSH, if the server address matches the value specified in this host, the hostname specified below host will be used as the final server address. The default '/etc/ssh/ssh_config ' configuration information will be overwritten with all the custom configurations configured under the Host field. port     a custom port user     a custom user name hostname     The truly connected server address, preferredauthentications    , specifies which method of authentication is preferred and supports password and key authentication methods identityfile      Specify the key file used by this connection 

With the above settings, we can use multiple SSH keys to connect to different git repositories.

3. Connection test

We can use it ssh to perform connection validation tests.

[email protected]. ssh]# ssh-t [email protected]hello user name you ' ve connected to coding.net by SSH successfully! [Email protected]me ssh]# ssh-t [email protected]hi user name! You've successfully authenticated, but GitHub does not provide shell access.
4. Pull Project Settings

with these settings, we can use different SSH keys for different git repositories and git projects with different hosts. However, it is also important to note that the SSH access address of the project that we pull from the Git repository normally, like this [email protected] Warehouse Address: User name/project name. Git . We must replace the GIT repository address here with the host set in our SSH config.
Example:

[email protected]. ssh]# git clone [email protected]: username/project name. Git is replaced with the following [[email protected]. ssh]# git clone [email protected ]_github: User name/project name. Git

We're done here!

Reference Links:
Management of multiple SSH keys
git generates ssh key and resolves multiple ssh key issues locally
SSH configuration explained Daquan
Ssh_config File Configuration Detailed
SSH config configuration update

This article is from the "not only Linux" blog, so be sure to keep this source http://nolinux.blog.51cto.com/4824967/1753470

Troubleshooting Git local multi-ssh key

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.