Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable password ssh authentication for Vagrant VM?

I'd like to enable password ssh authentication (and keep key-based authentication enabled) for may Vagrant VM. How to set that?

Vagrantfile:

Vagrant.configure("2") do |config|
  config.vm.box = "fedora/26-cloud-base"
  config.vm.box_version = "20170705"

  config.ssh.username = 'vagrant'
  config.ssh.password = 'a'
  config.ssh.keys_only = false
end
$ sudo vagrant ssh-config 
Host default
  HostName 192.168.121.166
  User vagrant
  Port 22
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /home/jakub/src/kubernetes-vms/kubernetes/.vagrant/machines/default/libvirt/private_key
  LogLevel FATAL

Password a is not accepted with this settings.

I guess the might be PasswordAuthentication no in output of vagrant ssh-config. How can that option be switched on?

like image 925
czerny Avatar asked Aug 23 '17 13:08

czerny


3 Answers

On centos 7, using only below is not enough. By this way, I guess that it just make su vagrant become by password. I cannot find anything why below does not work in the official site.

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"

  config.ssh.username = 'vagrant'
  config.ssh.password = 'vagrant'
  config.ssh.insert_key = false
end

You should modify sshd_config manually.

  config.vm.provision "shell", inline: <<-SHELL
     sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config    
     systemctl restart sshd.service
  SHELL
like image 99
kyakya Avatar answered Oct 17 '22 08:10

kyakya


For me the following works. You need to ssh to the vm as usual and then edit /etc/ssh/sshd_config. There you need to set PasswordAuthentication to yes instead of no. This will allow password authentication.

like image 11
FreeLightman Avatar answered Oct 17 '22 06:10

FreeLightman


Vagrantfile:

Vagrant.configure("2") do |config|
  config.vm.box = "fedora/26-cloud-base"
  config.vm.box_version = "20170705"

  config.vm.provision 'shell', inline: 'echo "vagrant:a" | chpasswd'
end

Line config.vm.provision 'shell', inline: 'echo "vagrant:a" | chpasswd' invokes shell provisioning that changes password of vagrant user (provided the box comes with predefined user called vagrant).

Then one can connect not only by vagrant ssh but also

ssh vagrant@<vm-ip>
like image 6
czerny Avatar answered Oct 17 '22 07:10

czerny