{"id":5747,"date":"2024-06-29T11:28:58","date_gmt":"2024-06-29T03:28:58","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=5747"},"modified":"2024-06-29T11:30:59","modified_gmt":"2024-06-29T03:30:59","slug":"use-multiple-ssh-keys-for-git-host-websites-github-gitlab","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=5747","title":{"rendered":"Use Multiple SSH Keys for\u00a0Git\u00a0host websites (Github, Gitlab)"},"content":{"rendered":"\n<p>This is guide about how to configure multiple&nbsp;<em>SSH<\/em>&nbsp;keys for some&nbsp;<em>Git<\/em>&nbsp;host websites such as&nbsp;<em>Github<\/em>,&nbsp;<em>Gitlab<\/em>, among others.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"user-content-creating-ssh-keys\">Creating SSH keys<\/h2>\n\n\n\n<ol>\n<li>Create&nbsp;<em>SSH<\/em>&nbsp;directory:mkdir ~\/.ssh<\/li>\n\n\n\n<li>Move to created directory:cd ~\/.ssh<\/li>\n\n\n\n<li>To create a&nbsp;<em>SSH<\/em>&nbsp;key, type:ssh-keygen -t rsa -C &#8220;EMAIL@HOST.com&#8221;a message will be displayed:Generating public\/private rsa key pair. Enter file in which to save the key (\/home\/USER\/.ssh\/id_rsa):You should type someething of the default name of the file to distinguish service, such as:&nbsp;<code>id_rsa_myaccount_github<\/code>,&nbsp;<code>id_rsa_myaccount_gitlab<\/code>,&nbsp;<code>id_rsa_mycompanyaccount_gitlab<\/code>.After this step a&nbsp;<em>passphrase<\/em>&nbsp;is needed for security, which can be empty.<\/li>\n\n\n\n<li>Repeat previous step for every required account.<\/li>\n\n\n\n<li>To see if the keys were successful created:ls ~\/.sshwhich it is going to print all key files, for example:<code> id_rsa_myaccount_github id_rsa_myaccount_gitlab.pub id_rsa_myaccount_github.pub id_rsa_mycompanyaccount_gitlab id_rsa_myaccount_gitlab id_rsa_mycompanyaccount_gitlab.pub<\/code><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"user-content-creating-config-file-for-manage-ssh-keys\">Creating&nbsp;<em>config<\/em>&nbsp;file for manage&nbsp;<em>SSH<\/em>&nbsp;keys<\/h2>\n\n\n\n<ol>\n<li>To create&nbsp;<code>config<\/code>&nbsp;file:touch ~\/.ssh\/config<\/li>\n\n\n\n<li>Edit the file to configure&nbsp;<em>domains<\/em>&nbsp;for the keys:nano ~\/.ssh\/configfor example, if three accounts were added, should look like this:<code># github account Host github.com HostName github.com PreferredAuthentications publickey IdentityFile ~\/.ssh\/id_rsa_myaccount_github # gitlab account Host gitlab.com HostName gitlab.com PreferredAuthentications publickey IdentityFile ~\/.ssh\/id_rsa_myaccount_gitlab # gitlab company account Host gitlab.my_company.com HostName gitlab.my_company.com PreferredAuthentications publickey IdentityFile ~\/.ssh\/id_rsa_mycompanyaccount_gitlab<\/code><\/li>\n\n\n\n<li>Save file and exit.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"user-content-using-two-accounts-from-the-same-server-website-optional\">Using two accounts from the same server (website) [Optional]<\/h3>\n\n\n\n<p>A new host has to be created.<\/p>\n\n\n\n<ol>\n<li>Create a new entry on the&nbsp;<code>~\/.ssh\/config<\/code>&nbsp;file(example with&nbsp;<em>Github<\/em>):<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>Host other.github.com\nHostName github.com\nPreferredAuthentications publickey\nIdentityFile ~\/.ssh\/id_rsa_otheraccount_github<\/code><\/pre>\n\n\n\n<p>where&nbsp;<code>other.github.com<\/code>&nbsp;is the alias for the host, although the server(<code>HostName<\/code>) is&nbsp;<code>github.com<\/code>. The&nbsp;<code>IdentityFile<\/code>&nbsp;option points to a (previously) created key file configured with the required account.<\/p>\n\n\n\n<p>After this, a custom url can be used to clone the project.git clone git@other.github.com:USER\/REPOSITORY.git<\/p>\n\n\n\n<p>where&nbsp;<code>other.github.com<\/code>&nbsp;is the previously created domain.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"user-content-configure-ssh-on-repository-site\">Configure&nbsp;<em>SSH<\/em>&nbsp;on Repository site<\/h2>\n\n\n\n<p>To configure ssh on each repository website:<\/p>\n\n\n\n<ol>\n<li>Copy the content of the&nbsp;<code>id_rsa_X.pub<\/code>&nbsp;with&nbsp;<code>xclip<\/code>&nbsp;command(may not be installed) to the clipboard(where&nbsp;<code>id_rsa_X.pub<\/code>&nbsp;is the wanted key file):xclip -sel clip &lt; ~\/.ssh\/id_rsa_X.pub(content also can also be manually copied from the&nbsp;<code>*.pub<\/code>&nbsp;file)<\/li>\n\n\n\n<li>Paste the content on the repository site, check next section.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"user-content-configure-ssh-on-github\">Configure&nbsp;<em>SSH<\/em>&nbsp;on Github<\/h3>\n\n\n\n<ol>\n<li>Go to&nbsp;<a href=\"https:\/\/github.com\/\">https:\/\/github.com<\/a>.<\/li>\n\n\n\n<li>Go to Profile&nbsp;<em>Settings<\/em>&nbsp;&gt;&nbsp;<em>SSH and GPG Keys<\/em>&nbsp;&gt; click on button&nbsp;<em>New SSH Key<\/em>.<\/li>\n\n\n\n<li>On&nbsp;<em>Title<\/em>&nbsp;add a descriptive label, such as the&nbsp;<em>hostname<\/em>&nbsp;of the device\u2026\u200b<\/li>\n\n\n\n<li>On the&nbsp;<em>Key<\/em>&nbsp;field past the clip content with the key.<\/li>\n\n\n\n<li>Finally click on&nbsp;<em>Add SSH key<\/em>&nbsp;and after that the site ask for the user password.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"user-content-configure-ssh-on-gitlab\">Configure&nbsp;<em>SSH<\/em>&nbsp;on Gitlab<\/h3>\n\n\n\n<ol>\n<li>Go to&nbsp;<a href=\"https:\/\/gitlab.com\/\">https:\/\/gitlab.com<\/a>.<\/li>\n\n\n\n<li>Go to Profile&nbsp;<em>Settings<\/em>&nbsp;&gt;&nbsp;<em>SSH Keys<\/em>.<\/li>\n\n\n\n<li>On the&nbsp;<em>Key<\/em>&nbsp;field past the clip content with the key.<\/li>\n\n\n\n<li>On&nbsp;<em>Title<\/em>&nbsp;add a descriptive label, such as the&nbsp;<em>hostname<\/em>&nbsp;of the device\u2026\u200b<\/li>\n\n\n\n<li>Finally click on&nbsp;<em>Add key<\/em>.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"user-content-testing-ssh-keys\">Testing SSH Keys<\/h2>\n\n\n\n<ol>\n<li>Type(substitute&nbsp;<code>HOST<\/code>&nbsp;with the desired one(<em>github<\/em>,&nbsp;<em>gitlab<\/em>, \u2026\u200b)):ssh -T git@HOST.coma warning will appear, accept it with&nbsp;<code>yes<\/code>:<code>The authenticity of host 'HOST.com (IP ADDRESS)' can't be established. RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48. Are you sure you want to continue connecting (yes\/no)?<\/code>A successful message will appear:\n<ol style=\"list-style-type:lower-alpha\">\n<li>For&nbsp;<em>Github<\/em>:<code>Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.<\/code><\/li>\n\n\n\n<li>For&nbsp;<em>Gitlab<\/em>:<code>Welcome to GitLab, USERNAME!<\/code><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"user-content-delete-ssh-cache-and-add-keys\">Delete&nbsp;<em>SSH<\/em>&nbsp;Cache and add Keys<\/h3>\n\n\n\n<p>If the&nbsp;<em>SSH<\/em>&nbsp;does not work, maybe the keys need to be added with&nbsp;<code>ssh-add<\/code>&nbsp;command<\/p>\n\n\n\n<ol>\n<li>First delete keys cache:ssh-add -Dif a message appear:Could not open a connection to your authentication agent.use this command and after that retry:eval `ssh-agent -s`<\/li>\n\n\n\n<li>Add key file with&nbsp;<code>ssh-add<\/code>&nbsp;command:ssh-add ~\/.ssh\/id_rsa_file<\/li>\n\n\n\n<li>To see added keys, type:ssh-add -ldand something such as this will be displayed:<code>2048 SHA256:DXlgYQo1o\/65JQCSYQo\/L4RRP4i+wTouyEetkOIcn\/o EMAIL_1 (RSA) 2048 SHA256:4FPtZYDtHipZeHqP9KNB3Wslz9L5q\/JoAGT3g\/NW3O8 EMAIL_2 (RSA) 2048 SHA256:tXCoBI2dMtTFhUhE5oBT+XwwkrhkorkOHbSc1J22urQ EMAIL_3 (RSA)<\/code><\/li>\n\n\n\n<li>Retry testing connection.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"user-content-using-ssh-keys\">Using SSH keys<\/h2>\n\n\n\n<p>To use a git repository with the&nbsp;<em>SSH<\/em>, this url style has to be used for the repository:git@HOST:USERNAME\/REPOSITORY.git<\/p>\n\n\n\n<p>where&nbsp;<code>HOST<\/code>&nbsp;is the configured domain, which can be&nbsp;<em>github<\/em>,&nbsp;<em>gitlab<\/em>&nbsp;or a personalized one.<\/p>\n\n\n\n<p>If project origin is already configured with HTTPS, it has to be changed to the SSH url style (check next section).<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Warning<\/td><td>If you want to use the HTTPS url, other steps will be required.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"user-content-change-https-url-to-ssh-url-optional\">Change HTTPS url to SSH url [Optional]<\/h3>\n\n\n\n<ol>\n<li>List existing remotes in order to get the name of the repository:git remote -v<\/li>\n\n\n\n<li>Change remote url, substitute HOST for server domain or a previously create custom HOST:git remote set-url origin git@HOST:USERNAME\/REPOSITORY.git<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Note<\/td><td>It can be used the same method to change from SSH to HTTPS.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"user-content-important-about-git-config-user-name-and-email\">Important: About&nbsp;<code>git config<\/code>&nbsp;user&nbsp;<em>name<\/em>&nbsp;and&nbsp;<em>email<\/em><\/h2>\n\n\n\n<p>In spite&nbsp;<em>SSH<\/em>&nbsp;keys were configured for the access, the&nbsp;<em>Git<\/em>&nbsp;user&nbsp;<em>name<\/em>&nbsp;and&nbsp;<em>email<\/em>&nbsp;need to be configured, because these will be associated to the&nbsp;<em>commits<\/em>.<\/p>\n\n\n\n<p>To see actual configuration, type:git config &#8211;list<\/p>\n\n\n\n<p>If global user&nbsp;<em>name<\/em>&nbsp;and&nbsp;<em>email<\/em>&nbsp;were configured will be displayed at the beginning, if not, these values will not appear.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Note<\/td><td>If the command was run on a repository and this one has a local user&nbsp;<em>name<\/em>&nbsp;and&nbsp;<em>email<\/em>&nbsp;configured, these values will be displayed at the end of configuration.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"user-content-configure-user-name-and-email-on-all-repositories-globally\">Configure user&nbsp;<em>name<\/em>&nbsp;and&nbsp;<em>email<\/em>&nbsp;on all repositories (globally)<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Important<\/td><td>You may want to configure the most used user&nbsp;<em>name<\/em>&nbsp;and&nbsp;<em>email<\/em>&nbsp;globally, but watch out since every commit with not&nbsp;<em>local configuration<\/em>&nbsp;will use these values.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ol>\n<li>Go to the root directory of the repository.<\/li>\n\n\n\n<li>Type to configure user&nbsp;<em>name<\/em>:git config &#8211;global user.name &#8220;YOUR NAME&#8221;<\/li>\n\n\n\n<li>Type to configure user&nbsp;<em>email<\/em>:git config &#8211;global user.email &#8220;email@HOST.com&#8221;<\/li>\n\n\n\n<li>To see if the&nbsp;<strong>global<\/strong>&nbsp;fields were correctly configured, use the&nbsp;<code>git config --list<\/code>&nbsp;command or check global&nbsp;<em>Git<\/em>&nbsp;file.nano ~\/.gitconfigthis will display a section like:[user] name = YOUR NAME email = email@HOST.comNoteIf the&nbsp;<em>user name<\/em>&nbsp;or the&nbsp;<em>user email<\/em>&nbsp;were not configured this section will not appear.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"user-content-configure-user-name-and-email-on-a-unique-repository-locally\">Configure user&nbsp;<em>name<\/em>&nbsp;and&nbsp;<em>email<\/em>&nbsp;on a unique repository (locally)<\/h3>\n\n\n\n<ol>\n<li>Go to the root directory of the repository.<\/li>\n\n\n\n<li>Type to configure user&nbsp;<em>name<\/em>:git config user.name &#8220;YOUR NAME&#8221;<\/li>\n\n\n\n<li>Type to configure user&nbsp;<em>email<\/em>:git config user.email &#8220;email@HOST.com&#8221;<\/li>\n\n\n\n<li>To see if the&nbsp;<strong>global<\/strong>&nbsp;fields were correctly configured, use the&nbsp;<code>git config --list<\/code>&nbsp;command or check local&nbsp;<em>Git<\/em>&nbsp;file:nano .\/.git\/configthis will display a section like:[user] name = YOUR NAME email = email@HOST.comNoteIf the&nbsp;<em>user name<\/em>&nbsp;or the&nbsp;<em>user email<\/em>&nbsp;were not configured this section will not appear.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This is guide about how to configure multiple&nbsp;SSH&#038; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/5747"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5747"}],"version-history":[{"count":3,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/5747\/revisions"}],"predecessor-version":[{"id":5750,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/5747\/revisions\/5750"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}