Answers: <p>Following Vagrant file address this problem.</p>
  
  <p>You can get all supporting key files along with this vagrant file at <a href="https://github.com/malyabee/IaaC/tree/master/ansible_lab" rel="nofollow noreferrer">https://github.com/malyabee/IaaC/tree/master/ansible_lab</a></p>
  
  <pre><code>$commonscript = &lt;&lt;-SCRIPT
  sudo yum update -y
  sudo yum install python2 epel-release -y
  sudo yum install -y ansible
  sudo echo "192.168.22.10    ansiblecontroller.example.com ansiblecontroller" &gt;&gt; /etc/hosts
  sudo echo "192.168.22.11   node01.example.com   node01" &gt;&gt; /etc/hosts
  sudo echo "192.168.22.12   node02.example.com      node02" &gt;&gt; /etc/hosts
  SCRIPT
  
  $nodescript = &lt;&lt;-SCRIPT
  cat /vagrant/ansible_lab.pub &gt;&gt; /home/vagrant/.ssh/authorized_keys
  SCRIPT
  
  $ansiblescript = &lt;&lt;-SCRIPT
  sudo yum install ansible -y
  sudo cp -r /vagrant/ansible_lab /home/vagrant/.ssh/id_rsa
  sudo chmod 400  /home/vagrant/.ssh/id_rsa
  sudo chown vagrant:vagrant /home/vagrant/.ssh/id_rsa
  SCRIPT
  
  Vagrant.configure("2") do |config|
    config.vm.provision "shell", inline: "echo Hello"
  
    config.vm.define "ansiblecontroller" do |ansiblecontroller|
      ansiblecontroller.vm.box = "centos/7"
      ansiblecontroller.vm.provider "virtualbox" do |v|
            v.memory = 512
            v.cpus = 1
         end
      ansiblecontroller.vm.network "private_network", ip: "192.168.22.10", virtualbox__intnet: "mynetwork01"
      ansiblecontroller.vm.hostname = "ansiblecontroller.example.com"
      # Installing required packages for ansible controller node
      ansiblecontroller.vm.provision "shell", inline: $commonscript
      ansiblecontroller.vm.provision "shell", inline: $ansiblescript
    end
  
    config.vm.define "node01" do |node01|
      node01.vm.box = "centos/7"
      node01.vm.provider "virtualbox" do |v|
            v.memory = 512
            v.cpus = 1
         end
      node01.vm.network "private_network", ip: "192.168.22.11", virtualbox__intnet: "mynetwork01"
      node01.vm.hostname = "node01.example.com"
      # Installing required packages for  node01
      node01.vm.provision "shell", inline: $commonscript
      node01.vm.provision "shell", inline: $nodescript
    end
    config.vm.define "node02" do |node02|
      node02.vm.box = "centos/7"
      node02.vm.provider "virtualbox" do |v|
            v.memory = 512
            v.cpus = 1
         end
      node02.vm.network "private_network", ip: "192.168.22.12", virtualbox__intnet: "mynetwork01"
      node02.vm.hostname = "node02.example.com"
      # Installing required packages for  node01
      node02.vm.provision "shell", inline: $commonscript
      node02.vm.provision "shell", inline: $nodescript
    end
  end
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-07-24T05:12:04.347Z
Answers: This is a fully functional answer, thanks!
Answers: moztemur
Answers: CC BY-SA 4.0
Answers: 2019-09-20T08:55:03.837Z
Answers: Moved code repository to https://github.com/malyabee/vagrant_ansible_lab
Answers: MalyaBee
Answers: CC BY-SA 4.0
Answers: 2019-04-17T17:52:37.413Z
Answers: 2019-04-17T17:52:37.413Z
Answers: MalyaBee
Answers: <p>According <a href="https://www.vagrantup.com/docs/cli/ssh.html" rel="nofollow noreferrer">to the docs</a> one should use:</p>
  
  <pre><code>vagrant ssh [name|id]
  </code></pre>
  
  <p>If there is a single node then use <code>vagrant ssh</code> and in case of multi-node define the name or id of the VM, e.g. <code>vagrant ssh box1</code></p>
  
  <p>If one would like to ssh between boxes then one could create an ssh key and provision the private key to each box and add the public key to authorized_keys file.</p>
  
  <p><a href="https://www.vagrantup.com/docs/provisioning/file.html" rel="nofollow noreferrer">https://www.vagrantup.com/docs/provisioning/file.html</a></p>
  
  <blockquote>
  <pre><code>Vagrant.configure("2") do |config|
    # ... other configuration
  
    config.vm.provision "file", source: "~/.gitconfig", destination: ".gitconfig"
  end
  </code></pre>
  </blockquote>
  
Answers: CC BY-SA 3.0
Answers: 2017-10-03T15:55:10.793Z
Answers: How would you use `vagrant ssh name` within a box ? The question is about connection by ssh from node1.local to master.local who are both machines spin up by vagrant.
Answers: Tensibai
Answers: CC BY-SA 3.0
Answers: 2017-10-03T15:55:56.087Z
Answers: which was already what was stated under your deleted answer below...
Answers: Tensibai
Answers: CC BY-SA 3.0
Answers: 2017-10-03T16:19:08.063Z
Answers: I will update the answer
Answers: 030
Answers: CC BY-SA 3.0
Answers: 2017-10-03T15:44:30.207Z
Answers: 2017-10-03T16:20:11.203Z
Answers: 2017-10-03T16:20:11.203Z
Answers: 030
Answers: <p>Try this <a href="https://serverfault.com/questions/747622/cant-access-my-dedicated-server-via-ssh-permission-denied-publickey-gssapi-key">link</a>. You need to either execute one of the following:</p>
  
  <ul>
  <li><code>ssh -i &lt;pathto/private_key&gt; &lt;vagrant&gt;@&lt;ip&gt;</code></li>
  <li><code>ssh -o PreferredAuthentications=password user@server-ip</code> (if you haven’t disabled password based authentication)</li>
  </ul>
  
Answers: CC BY-SA 3.0
Answers: 2018-04-21T14:52:47.250Z
Answers: 2018-04-21T14:52:47.250Z
Answers: Coffee Bean
Body: <p>I created a three node multi-machine vagrant environment and am having issues ssh'ing from one vagrant vm to another.  </p>
  
  <p>Here is the Vagrantfile: </p>
  
  <pre class="lang-rb prettyprint-override"><code>Vagrant.configure("2") do |config| 
    config.vm.box = "centos/7"
  
    config.vm.define "master" do |master|
      master.vm.hostname = "master.local" 
      master.vm.network "private_network", type: "dhcp"
    end 
  
    config.vm.define "node1" do |node1|
       node1.vm.hostname = "node1.local" 
       node1.vm.network "private_network", type: "dhcp" 
    end 
  
    config.vm.define "node2" do |node2|
      node2.vm.hostname = "node2.local" 
      node2.vm.network "private_network", type: "dhcp" 
    end  
  end 
  </code></pre>
  
  <p>The hosts file (same on each node):  </p>
  
  <pre class="lang-bash prettyprint-override"><code>$ cat /etc/hosts
  172.28.128.3    master.local    master
  172.28.128.4    node1.local     node1
  172.28.128.5    node2.local     node2
  </code></pre>
  
  <p>I can ping back and forth all day from any machine to the other but I cannot ssh from one vagrant vm to the other.  The typical error message is (from node1 to master):  </p>
  
  <pre class="lang-bash prettyprint-override"><code>[vagrant@node1.local] $ ssh vagrant@172.28.128.3
  Permission denied (publickey,gssapi-keyex,gssapi-with-mic) 
  </code></pre>
  
  <p>SSH is running and the port is open. </p>
  
  <p>The firewall is not running.  </p>
  
  <p>I am sure this has to do with ssh keys.  I readily admit I am not an expert.<br>
  What am I doing wrong here folks?    </p>
  
Comments: CC BY-SA 3.0
Comments: 2017-04-25T16:26:04.507Z
Comments: Updated.   Yes, from one vm to the other in a vagrant environment.  It doesn't matter from which vm to another, something is not correct.
Comments: HBach
Comments: CC BY-SA 3.0
Comments: 2017-04-25T16:50:45.417Z
Comments: You need vagrant's private key within the machine, can't remember where it is store in vagrant host but the vagrant user is configured to allow only key based access. (using the default key is not recommended out of tests obviously)
Comments: Tensibai
Comments: CC BY-SA 3.0
Comments: 2017-04-25T16:53:53.510Z
Comments: @Tensibai One could also login without the keys `ssh vagrant@host` and use `vagrant` as a password as well.
Comments: 030
Comments: CC BY-SA 3.0
Comments: 2017-04-25T17:04:19.417Z
Comments: Please add the output of `ip a` of all boxes to the question and check whether the IP addresses (172.28.128.3-5) are available when the boxes are down.
Comments: 030
Comments: CC BY-SA 3.0
Comments: 2017-04-25T17:10:05.957Z
Comments: @030 according to the message in the question,  centos boxes,  unlike Ubuntu ones,  doesn't set a vagrant password, allowing only key authentication
Comments: Tensibai
Comments: CC BY-SA 3.0
Comments: 2017-04-25T19:37:09.647Z
Comments: @Tensibai, I am using the latest available box, centos/7 (virtualbox, 1703.01).
Comments: HBach
Comments: CC BY-SA 3.0
Comments: 2017-04-25T19:37:55.207Z
Comments: @Tensibai, The private key is stored at ~/VirtualBox VMs/nifi/.vagrant/machines/master/virtualbox/private_key, modifying the path accordingly for each vagrant vm.  The Authorized Keys file is used in /etc/ssh/sshd_config, AuthorizedKeysFile .ssh/authorized_keys.  I tried cp'ing the private_key for master as well as node1 and pasting it in the .ssh/authorized_keys file on each host respectively, restarting sshd on both instances, and tried to ssh to the other vm, but nogo.  We may be on the right track, but not certain.
Comments: HBach
Comments: CC BY-SA 3.0
Comments: 2017-04-25T19:38:35.143Z
Comments: @030, the IPs are fine.  This is not the issue.
Comments: HBach
Comments: CC BY-SA 3.0
Comments: 2017-04-25T19:46:21.460Z
Comments: @hbach authorized_keys file store the signature of authorized keys,  by default ssh use $HOME/.ssh/id_rsa (IIRC)  as default private key,  you can specify it with -i but the key has to be owned by the  user running ssh client and mode 0600 (read write for owner only)
Comments: Tensibai
Comments: CC BY-SA 3.0
Comments: 2017-04-26T03:35:17.997Z
Comments: Often adding one or (progressively) more `v` arguments to ssh helps figure out what's going on by increasing verbosity, especially when comparing with a working setup: `ssh -v ...` then `ssh -vv ...`, etc. `Permission denied` could mean a bunch of things...
Comments: Dan Cornilescu
Comments: CC BY-SA 3.0
Comments: 2017-09-21T16:14:16.277Z
Comments: @HBach, do not copy the private key for authorized_keys. Copy the public key instead.
Comments: Isaac A. Nugroho
Comments: CC BY-SA 3.0
Comments: 2017-09-28T13:43:17.740Z
Comments: did you solve this issue?
Comments: 030
Tags: <vagrant>
Title: Unable to ssh in a multi-machine vagrant environment
UserDisplayName: HBach
ThreadText: # Unable to ssh in a multi-machine vagrant environment
  
  (Asked by: username_0 on 2017-04-25)
  
  I created a three node multi-machine vagrant environment and am having issues ssh'ing from one vagrant vm to another.
  
  Here is the Vagrantfile:
  
  ```rb
  Vagrant.configure("2") do |config| 
    config.vm.box = "centos/7"
  
    config.vm.define "master" do |master|
      master.vm.hostname = "master.local" 
      master.vm.network "private_network", type: "dhcp"
    end 
  
    config.vm.define "node1" do |node1|
       node1.vm.hostname = "node1.local" 
       node1.vm.network "private_network", type: "dhcp" 
    end 
  
    config.vm.define "node2" do |node2|
      node2.vm.hostname = "node2.local" 
      node2.vm.network "private_network", type: "dhcp" 
    end  
  end 
  
  ```
  
  The hosts file (same on each node):
  
  ```bash
  $ cat /etc/hosts
  172.28.128.3    master.local    master
  172.28.128.4    node1.local     node1
  172.28.128.5    node2.local     node2
  
  ```
  
  I can ping back and forth all day from any machine to the other but I cannot ssh from one vagrant vm to the other. The typical error message is (from node1 to master):
  
  ```bash
  [vagrant@node1.local] $ ssh vagrant@172.28.128.3
  Permission denied (publickey,gssapi-keyex,gssapi-with-mic) 
  
  ```
  
  SSH is running and the port is open.
  
  The firewall is not running.
  
  I am sure this has to do with ssh keys. I readily admit I am not an expert.  
  What am I doing wrong here folks?
  
  **Tags:** vagrant
  
  > **Comments:**
  > **username_0**: Updated.   Yes, from one vm to the other in a vagrant environment.  It doesn't matter from which vm to another, something is not correct.  
  > **username_1**: You need vagrant's private key within the machine, can't remember where it is store in vagrant host but the vagrant user is configured to allow only key based access. (using the default key is not recommended out of tests obviously)  
  > **username_2**: @username_1 One could also login without the keys `ssh vagrant@host` and use `vagrant` as a password as well.  
  > **username_2**: Please add the output of `ip a` of all boxes to the question and check whether the IP addresses (172.28.128.3-5) are available when the boxes are down.  
  > **username_1**: @username_2 according to the message in the question,  centos boxes,  unlike Ubuntu ones,  doesn't set a vagrant password, allowing only key authentication  
  > **username_0**: @username_1, I am using the latest available box, centos/7 (virtualbox, 1703.01).  
  > **username_0**: @username_1, The private key is stored at ~/VirtualBox VMs/nifi/.vagrant/machines/master/virtualbox/private_key, modifying the path accordingly for each vagrant vm.  The Authorized Keys file is used in /etc/ssh/sshd_config, AuthorizedKeysFile .ssh/authorized_keys.  I tried cp'ing the private_key for master as well as node1 and pasting it in the .ssh/authorized_keys file on each host respectively, restarting sshd on both instances, and tried to ssh to the other vm, but nogo.  We may be on the right track, but not certain.  
  > **username_0**: @username_2, the IPs are fine.  This is not the issue.  
  > **username_1**: @username_0 authorized_keys file store the signature of authorized keys,  by default ssh use $HOME/.ssh/id_rsa (IIRC)  as default private key,  you can specify it with -i but the key has to be owned by the  user running ssh client and mode 0600 (read write for owner only)  
  > **username_3**: Often adding one or (progressively) more `v` arguments to ssh helps figure out what's going on by increasing verbosity, especially when comparing with a working setup: `ssh -v ...` then `ssh -vv ...`, etc. `Permission denied` could mean a bunch of things...  
  > **username_4**: @username_0, do not copy the private key for authorized_keys. Copy the public key instead.  
  > **username_2**: did you solve this issue?  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_5 on 2019-04-17. Score: 3
  
  Following Vagrant file address this problem.
  
  You can get all supporting key files along with this vagrant file at <https://github.com/username_5/IaaC/tree/master/ansible_lab>
  
  ```
  $commonscript = <<-SCRIPT
  sudo yum update -y
  sudo yum install python2 epel-release -y
  sudo yum install -y ansible
  sudo echo "192.168.22.10    ansiblecontroller.example.com ansiblecontroller" >> /etc/hosts
  sudo echo "192.168.22.11   node01.example.com   node01" >> /etc/hosts
  sudo echo "192.168.22.12   node02.example.com      node02" >> /etc/hosts
  SCRIPT
  
  $nodescript = <<-SCRIPT
  cat /vagrant/ansible_lab.pub >> /home/vagrant/.ssh/authorized_keys
  SCRIPT
  
  $ansiblescript = <<-SCRIPT
  sudo yum install ansible -y
  sudo cp -r /vagrant/ansible_lab /home/vagrant/.ssh/id_rsa
  sudo chmod 400  /home/vagrant/.ssh/id_rsa
  sudo chown vagrant:vagrant /home/vagrant/.ssh/id_rsa
  SCRIPT
  
  Vagrant.configure("2") do |config|
    config.vm.provision "shell", inline: "echo Hello"
  
    config.vm.define "ansiblecontroller" do |ansiblecontroller|
      ansiblecontroller.vm.box = "centos/7"
      ansiblecontroller.vm.provider "virtualbox" do |v|
            v.memory = 512
            v.cpus = 1
         end
      ansiblecontroller.vm.network "private_network", ip: "192.168.22.10", virtualbox__intnet: "mynetwork01"
      ansiblecontroller.vm.hostname = "ansiblecontroller.example.com"
      # Installing required packages for ansible controller node
      ansiblecontroller.vm.provision "shell", inline: $commonscript
      ansiblecontroller.vm.provision "shell", inline: $ansiblescript
    end
  
    config.vm.define "node01" do |node01|
      node01.vm.box = "centos/7"
      node01.vm.provider "virtualbox" do |v|
            v.memory = 512
            v.cpus = 1
         end
      node01.vm.network "private_network", ip: "192.168.22.11", virtualbox__intnet: "mynetwork01"
      node01.vm.hostname = "node01.example.com"
      # Installing required packages for  node01
      node01.vm.provision "shell", inline: $commonscript
      node01.vm.provision "shell", inline: $nodescript
    end
    config.vm.define "node02" do |node02|
      node02.vm.box = "centos/7"
      node02.vm.provider "virtualbox" do |v|
            v.memory = 512
            v.cpus = 1
         end
      node02.vm.network "private_network", ip: "192.168.22.12", virtualbox__intnet: "mynetwork01"
      node02.vm.hostname = "node02.example.com"
      # Installing required packages for  node01
      node02.vm.provision "shell", inline: $commonscript
      node02.vm.provision "shell", inline: $nodescript
    end
  end
  
  ```
  
  > **Comments:**
  > **username_6**: This is a fully functional answer, thanks!  
  > **username_5**: Moved code repository to https://github.com/username_5/vagrant_ansible_lab  
  
  ---
  
  ### Answer by username_2 on 2017-10-03. Score: 2
  
  According [to the docs](https://www.vagrantup.com/docs/cli/ssh.html) one should use:
  
  ```
  vagrant ssh [name|id]
  
  ```
  
  If there is a single node then use `vagrant ssh` and in case of multi-node define the name or id of the VM, e.g. `vagrant ssh box1`
  
  If one would like to ssh between boxes then one could create an ssh key and provision the private key to each box and add the public key to authorized\_keys file.
  
  <https://www.vagrantup.com/docs/provisioning/file.html>
  
  > ```
  > Vagrant.configure("2") do |config|
  >   # ... other configuration
  >
  >   config.vm.provision "file", source: "~/.gitconfig", destination: ".gitconfig"
  > end
  >
  > ```
  
  > **Comments:**
  > **username_1**: How would you use `vagrant ssh name` within a box ? The question is about connection by ssh from node1.local to master.local who are both machines spin up by vagrant.  
  > **username_1**: which was already what was stated under your deleted answer below...  
  > **username_2**: I will update the answer  
  
  ---
  
  ### Answer by username_7 on 2018-04-21. Score: 0
  
  Try this [link](https://serverfault.com/questions/747622/cant-access-my-dedicated-server-via-ssh-permission-denied-publickey-gssapi-key). You need to either execute one of the following:
  
  * `ssh -i <pathto/private_key> <vagrant>@<ip>`
  * `ssh -o PreferredAuthentications=password user@server-ip` (if you haven’t disabled password based authentication)
  
  
  ---
  
  