{"id":1727,"date":"2019-07-25T09:32:30","date_gmt":"2019-07-25T09:32:30","guid":{"rendered":"http:\/\/robermb.com\/blog\/?p=1727"},"modified":"2020-12-17T22:18:36","modified_gmt":"2020-12-17T21:18:36","slug":"configure-ssh-trust-relationship","status":"publish","type":"post","link":"https:\/\/robermb.com\/blog\/geeks\/configure-ssh-trust-relationship\/","title":{"rendered":"Configure SSH trust relationship"},"content":{"rendered":"\n<p>To configure a SSH trust relationship providing authentication via RSA public keys is necessary to follow the next steps:<\/p>\n\n\n\n<ol><li>Generate a Key Pair<\/li><li>Upload your Public Key to remote server<\/li><li>Checking the authorized_keys file in remote server<\/li><li>Checking the authentication via RSA<\/li><\/ol>\n\n\n\n<p>In my lab I&#8217;m currently using my <strong>Ansible master<\/strong> server like the <strong>origin<\/strong> server and my <strong>Ansible node1<\/strong> like the <strong>destination<\/strong> server. <\/p>\n\n\n\n<p><strong>Lab Example<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Ansible master: 192.168.152.135\nAnsible node1: 192.168.152.136<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Before the configuration<\/h3>\n\n\n\n<p>If we try to connect via <strong>ssh<\/strong> <strong>from<\/strong> Ansible <strong>master<\/strong> to <strong>node1<\/strong> with the remote user, we will need to <strong>introduce<\/strong> the <strong>password<\/strong> in the prompt:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;remote@ansible]$ ssh remote@192.168.152.136\nremote@192.168.152.136's password: \n\nLast login: Wed Jul 24 09:40:44 2019 from 192.168.152.135\n&#91;remote@node1 ~]$ <\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Starting the configuration:<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">1. Generate a Key Pair<\/h3>\n\n\n\n<p>We have to use <strong>ssh-keygen<\/strong> command with -b flag. <\/p>\n\n\n\n<p>The <strong>-b<\/strong> <strong>flag<\/strong> instructs ssh-keygen to increase the number of bits used to generate the key pair, and is suggested for <strong>additional security<\/strong>.<\/p>\n\n\n\n<p><strong>Important!<\/strong>: keep in mind that if you execute ssh-keygen command, it will <strong>overwrite<\/strong> an <strong>existing RSA key pair<\/strong>, potentially locking you out of other systems.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;remote@ansible]$ ssh-keygen -b 4096<\/code><\/pre>\n\n\n\n<p>Then <strong>press Enter<\/strong> to use the <strong>default<\/strong> names <strong>id_rsa<\/strong> and <strong>id_rsa.pub<\/strong> in the <strong>\/home\/your_username\/.ssh<\/strong> directory before entering your passphrase.<\/p>\n\n\n\n<p>While creating the key pair, we will be given the option to encrypt the private key with a <strong>passphrase<\/strong>. This means that the key pair cannot be used without entering the passphrase (unless we save that passphrase to our local machine\u2019s keychain manager). Is possible <strong>leave this field blank<\/strong>, that is what we will do in the example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Generating public\/private rsa key pair.\n\nEnter file in which to save the key (\/home\/remote\/.ssh\/id_rsa): \nEnter passphrase (empty for no passphrase): \nEnter same passphrase again: \n\nYour identification has been saved in \/home\/remote\/.ssh\/id_rsa.\nYour public key has been saved in \/home\/remote\/.ssh\/id_rsa.pub.\n\nThe key fingerprint is:\nSHA256:zllyF+t37cWgJAkzn38Gec1Jsp7SNtvm3Ws0Rzip9Hz remote@ansible.localdomain\nThe key's randomart image is:\n+---&#91;RSA 4096]----+\n|                 |\n|        +    . . |\n|         = o  +=o|\n|          = o.A.=|\n|        S .=o=.B |\n|       o . ++*=oB|\n|      . * o oo=++|\n|       o + ....++|\n|          o.  +o+|\n+----&#91;SHA256]-----+<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. Upload your Public Key to remote server<\/h3>\n\n\n\n<p>We can do this step <strong>just copying the public key<\/strong>(\/home\/remote\/.ssh\/id_rsa.pub) to the <strong>remote<\/strong> <strong>server<\/strong> over <strong>authorized_keys<\/strong> file located in \/home\/remote\/.ssh\/.<\/p>\n\n\n\n<p><strong>Or<\/strong> <strong>using<\/strong> the specific utility for that task, called <strong>ssh-copy-id<\/strong>. The utility ssh-copy-id can copy a SSH public key to a remote server over SSH. And we can use it executing the following commands:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;remote@ansible]$ ssh-copy-id remote@192.168.152.136<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/usr\/bin\/ssh-copy-id: INFO: Source of key(s) to be installed: \"\/home\/remote\/.ssh\/id_rsa.pub\"\nThe authenticity of host '192.168.152.136 (192.168.152.136)' can't be established.\nECDSA key fingerprint is SHA256:lYyLFG9lhEsTmhoYB5zEdKVS1+jflyMO\/ymyNKaNxRo.\nECDSA key fingerprint is MD5:21:59:d7:13:36:2f:0c:ba:10:37:79:70:f6:da:78:b5.\nAre you sure you want to continue connecting (yes\/no)? yes\n\/usr\/bin\/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed\n\/usr\/bin\/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys\nremote@192.168.152.136's password: \n\nNumber of key(s) added: 1\n\nNow try logging into the machine, with:   \"ssh 'remote@192.168.152.136'\"\nand check to make sure that only the key(s) you wanted were added.<\/code><\/pre>\n\n\n\n<p>There is a method if we <strong>don&#8217;t <\/strong>want to <strong>introduce<\/strong> the <strong>password<\/strong> for each server. Follow the steps with this article:<\/p>\n\n\n\n<p><a href=\"https:\/\/robermb-blog.000webhostapp.com\/2019\/07\/best-way-to-distribute-users-public-ssh-key-to-many-hosts-no-prompt-password\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Best way to distribute users\u2019s Public ssh Key to many hosts (no prompt password) (opens in a new tab)\">Best way to distribute users\u2019s Public ssh Key to many hosts (no prompt password)<\/a><br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Checking the authorized_keys file in remote server<\/h3>\n\n\n\n<p>We already copied the <strong>public key<\/strong> from Ansible master to node1 in the path \/home\/remote\/.ssh\/ with a different file name, <strong>authorized_keys<\/strong>.<\/p>\n\n\n\n<p>And now we can see the new file in node1:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;remote@node1]$ cd \/home\/remote\/.ssh\/\n\n&#91;remote@node1 .ssh]$ cat authorized_keys \nssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCWBB+y0J8o5xYqsSfv6jc2fIu1UPW8YWEBOnAHG1Wnrp2tZEjaAysFPV+UNaRTL9kEclpOJ4L1zGMVTL5Va9dqVNVgH4kqw2sTnYTqmIXURBcvxi5nwVo+m5o6Hh96KSTJy5dD\/EmnMkkBznMoUCwdld0a4c8jZ1chWUTzae\/3\/yQG5rfYpE7Ht0MJ2\/vaqHpPOlaEKfXUjOTCY\/u8TFtDt89w5\/t8RivdGH2KSNZ5PtAVCI7uCzcEE7e1bhQjroba37htDNwJ7wGQgCJYJWd1Vy5pLe3aKNrhnjC3M5ZXb7YI1TgH4zRzkHKUH1dTIQ6cOsUE4LNhyn9AwCqLzf\/1 remote@ansible.localdomain<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. Checking the authentication via RSA<\/h3>\n\n\n\n<p>Now, if we <strong>try again the ssh<\/strong> connection, we will connect without provide any password, because <strong>we are providing authentication via RSA public keys<\/strong> :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;remote@ansible]$ ssh remote@192.168.152.136\nLast login: Wed Jul 24 06:53:40 2019 from 192.168.152.135\n&#91;remote@node1 ~]$ <\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>To configure a SSH trust relationship providing authentication via RSA public keys is necessary to follow the next steps: Generate &hellip; <a href=\"https:\/\/robermb.com\/blog\/geeks\/configure-ssh-trust-relationship\/\" class=\"more-link\">More <span class=\"screen-reader-text\">Configure SSH trust relationship<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1812,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,8],"tags":[35,107],"_links":{"self":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/1727"}],"collection":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/comments?post=1727"}],"version-history":[{"count":11,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/1727\/revisions"}],"predecessor-version":[{"id":1803,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/1727\/revisions\/1803"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/media\/1812"}],"wp:attachment":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/media?parent=1727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/categories?post=1727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/tags?post=1727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}