{"id":1746,"date":"2019-07-26T15:40:02","date_gmt":"2019-07-26T15:40:02","guid":{"rendered":"http:\/\/robermb.com\/blog\/?p=1746"},"modified":"2024-02-23T18:54:30","modified_gmt":"2024-02-23T17:54:30","slug":"best-way-to-distribute-users-public-ssh-key-to-many-hosts-no-prompt-password","status":"publish","type":"post","link":"https:\/\/robermb.com\/blog\/geeks\/best-way-to-distribute-users-public-ssh-key-to-many-hosts-no-prompt-password\/","title":{"rendered":"Best way to distribute users&#8217;s Public ssh Key to many hosts (no prompt password)"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Lab information<\/h3>\n\n\n\n<p>For this lab, we will <strong>distribute<\/strong> the <strong>ssh public key<\/strong> of our main server, Ansible Master to two different servers. That way we will be able to connect via ssh from the Ansible Master to the other two nodes without asking us for the password.<\/p>\n\n\n\n<ul><li>Ansible Master: 192.168.152.135<\/li><li>Node1: 192.168.152.136<\/li><li>Node2: 192.168.152.137<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">1. Create a file with your password<\/h3>\n\n\n\n<p>Create <strong>pass.txt<\/strong> file and introduce the <strong>password<\/strong> of the user that is the owner of the public key, in my example the user is remote and its password is password1234. Save the file and exit.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;remote@ansible]$ vi pass.txt\npassword1234<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. Distribute the public key<\/h3>\n\n\n\n<p>To <strong>distribute<\/strong> the <strong>public key<\/strong> we are going to use <strong>sshpass<\/strong> and <strong>ssh-copy-id<\/strong> <strong>commands<\/strong>. To do that, execute the following commands:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for host in 192.168.152.136 192.168.152.137\ndo\nsshpass -f pass.txt ssh-copy-id ${host}\ndone<\/code><\/pre>\n\n\n\n<p>Or in a single line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for host in 192.168.152.136 192.168.152.137; do sshpass -f pass.txt ssh-copy-id ${host}; done<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Result<\/h3>\n\n\n\n<p>As you can see in the <strong>output<\/strong> below, the public key is distributed correctly on all servers and you don&#8217;t need to enter your the password for each server. Is a complete automation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/bin\/ssh-copy-id: INFO: Source of key(s) to be installed: \"\/home\/remote\/.ssh\/id_rsa.pub\"\n\/bin\/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed\n\/bin\/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys\n\nNumber of key(s) added: 1\n\nNow try logging into the machine, with:   \"ssh '192.168.152.136'\"\nand check to make sure that only the key(s) you wanted were added.\n\n\/bin\/ssh-copy-id: INFO: Source of key(s) to be installed: \"\/home\/remote\/.ssh\/id_rsa.pub\"\n\/bin\/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed\n\/bin\/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys\n\nNumber of key(s) added: 1\n\nNow try logging into the machine, with:   \"ssh '192.168.152.137'\"\nand check to make sure that only the key(s) you wanted were added.<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lab information For this lab, we will distribute the ssh public key of our main server, Ansible Master to two &hellip; <a href=\"https:\/\/robermb.com\/blog\/geeks\/best-way-to-distribute-users-public-ssh-key-to-many-hosts-no-prompt-password\/\" class=\"more-link\">More <span class=\"screen-reader-text\">Best way to distribute users&#8217;s Public ssh Key to many hosts (no prompt password)<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1813,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[103,2,8],"tags":[106,109,110,35,108,107],"_links":{"self":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/1746"}],"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=1746"}],"version-history":[{"count":10,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/1746\/revisions"}],"predecessor-version":[{"id":1969,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/1746\/revisions\/1969"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/media\/1813"}],"wp:attachment":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/media?parent=1746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/categories?post=1746"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/tags?post=1746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}