3

This is a follow-up question to an earlier question. I've used the same Vagrantfile, but have commented out two lines I don't think are necessary.

I'm trying to ssh into my Vagrant box without using vagrant ssh. Below is my Vagrantfile and ssh configuration information:

Vagrantfile:

Vagrant.configure(2) do |config|

  config.vm.provider "virtualbox" do |v|
    v.memory = 6144
    v.cpus = 2
    v.name = "mb_vagrant"
  end

  config.vm.box = "ubuntu/trusty64"

  config.vm.network :private_network, ip: "192.168.33.10"

  config.ssh.forward_agent = true

  # config.vm.provision :shell, path: "bootstrap.sh"
  # config.vm.network :forwarded_port, host: 8001, guest: 8001

end

The output from vagrant ssh-config:

Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile "/Users/mbigras/Google Drive/tmp/chef-repo/.vagrant/machines/default/virtualbox/private_key"
  IdentitiesOnly yes
  LogLevel FATAL
  ForwardAgent yes

I've tried sshing into my machine with the following command:

$ ssh -i "/Users/mbigras/Google Drive/tmp/chef-repo/.vagrant/machines/default/virtualbox/private_key" -p 2222 [email protected]
ssh: connect to host 192.168.33.10 port 2222: Connection refused

Also, as per the solution described in another answer, I've tried removing ~/.ssh/known_hosts before attempting to connect; however, it also doesn't work:

$ rm ~/.ssh/known_hosts
$ ssh -i "/Users/mbigras/Google Drive/tmp/chef-repo/.vagrant/machines/default/virtualbox/private_key" -p 2222 [email protected]
ssh: connect to host 192.168.33.10 port 2222: Connection refused

What am I missing here?

1
  • If you're wanting to use 16.04 I can recommend bento/ubuntu-16.04 because they use the standard username: vagrant, password: vagrant convention
    – mbigras
    Commented Apr 20, 2017 at 16:48

3 Answers 3

8

Ssh service is bound to host machine's (i.e: 127.0.0.1) port 2222, but in the VM (guest machine) is still listening on port 22 (as the default port). So, you should connect to port 22 on 192.168.33.10 or 2222 on 127.0.0.1. I.e:

$ ssh -i "<vagranfile-path>/.vagrant/machines/default/virtualbox/private_key" \
-p 22 [email protected]

or

$ ssh -i "<vagranfile-path>/.vagrant/machines/default/virtualbox/private_key" \
-p 2222 [email protected]

Also, it is not required to remove ~/.ssh/known_hosts file. Adding the following option will avoid host fingerprint check: -o UserKnownHostsFile=/dev/null

2
  • What's the difference between guest machine and VM? I thought they are the same. Do you have any book recommendation to understand what you have written here (port binding, guest/host machine and their connectivity/networking, etc)
    – SenG
    Commented Aug 17, 2020 at 15:19
  • You are right. Guest machine and VM are the same. Actually it was a mistake on the answer wording. Just fixed that. Thanks Commented Aug 17, 2020 at 17:42
0

set the port forwarding section of vagrantfile like so:

  # using a specific IP.
    config.vm.network "private_network", ip: "192.168.56.101"
    config.vm.network "forwarded_port", guest: 22, host: 2290

this will allow "normal" ssh into the the guest without go through vagrant ssh

0

by the way if anyone is stuck in a wsl prompt and testing other than using the vagrant-ssh command then you can simply use what the ip address of eth0 is bound to, you can find this out using ip route you can then plug in the ip there to your ssh command and you should have no issues so my output from ip route is:

default via 172.19.192.1 dev eth0 proto kernel
172.19.192.0/20 dev eth0 proto kernel scope link src 172.19.197.225

so just use 172.19.192.1 instead of 127.0.0.1

Soo

ssh [email protected] -p 2222 -i ".\vagrant\<path>"

Not the answer you're looking for? Browse other questions tagged or ask your own question.