Search code examples
linuxsshvagrant

Can't SSH into Vagrant VM (without using vagrant ssh) part 2


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 vagrant@192.168.33.10
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 vagrant@192.168.33.10
ssh: connect to host 192.168.33.10 port 2222: Connection refused

What am I missing here?


Solution

  • 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 vagrant@192.168.33.10
    

    or

    $ ssh -i "<vagranfile-path>/.vagrant/machines/default/virtualbox/private_key" \
    -p 2222 vagrant@127.0.0.1
    

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