How to Connect One Vagrant Machine to Another Using SSH

I will summarize this article as possible. We have those who get bored reading long epistle before getting answers. So, this is going to be short and précised. Let's dive into it.

  1. Edit your Vagrantfile

```Vagrant.configure("2") do |config| config.vm.provision "shell", inline: "echo Hello"

config.vm.define "web" do |w| w.vm.network "private_network", type: "dhcp" w.vm.box = "ubuntu/focal64" end

config.vm.define "db" do |d| d.vm.network "private_network", type: "dhcp" d.vm.box = "ubuntu/focal64" end

end



2. We have 2 vagrant machines. One is called 'web' while the other is called 'db'. Run 


```

vagrant up


on your local machine. To make things easy, I will advise that you open 2 terminals. So, you can vagrant up on the 2 terminals

```vagrant ssh web



on terminal and


```vagrant ssh db

on the other terminal.

We want to ssh into 'db' from 'web'. Copy the ip address from 'db' by running

```ip addr


![article--1.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1664231624562/gYu040x8d.PNG align="left")

3. Run 

```ssh-copy-id -i vagrant@ip addr

on your 'web' terminal. The image below will be your response.

article1.PNG

  1. On your 'db' terminal, type

```sudo nano /etc/ssh/sshd_config



Edit this file by scrolling down to the PasswordAuthentication. It is always NO but change it to YES. 

![article2.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1664232016300/h3RFXZrwi.PNG align="left")

5. Restart the 'db' machine by typing below snippet on the terminal

```sudo systemctl restart sshd

this will help your system to activate the new changes in it.

  1. In your 'web' machine terminal. Type this again

```ssh-copy-id -i vagrant@ip addr



Type vagrant as the password. Vagrant default password and username name is vagrant. 
it will show "added: 1"

7. In other to confirm if this is working. Go back to your 'db' machine terminal. Change the PasswordAuthentication to yes. 

8. Restart your systemctl in the 'db' machine terminal

9. Go back to your 'web' machine terminal. Run 


```vagrant@ip addr

It will take you to 'db' ssh environment.

Thank you for reading. I hope you find this useful.