Vagrant changes the insecure key pair to randomly generated key on first setup by default. To Change the insecure key pair to your own key for secure login.
Setup multiple keys for e.g keys/private and keys/public.
mkdir -p ./keys/private ./keys/publicchmod 700 ./keys/private ./keys/public
for i in {1..3}; do ssh-keygen -t ed25519 -f ./keys/private/id_ed25519_vagrant$i -C "vagrant$i" -N "" mv ./keys/private/id_ed25519_vagrant$i.pub ./keys/public/done
chmod 600 ./keys/private/*chmod 644 ./keys/public/*Define custom keys in order of priority:
config.ssh.private_key_path = ["keys/private", "~/.vagrant.d/insecure_private_key"]Default Insecure Key
Vagrant uses the default SSH private key i.e ~/.vagrant.d/insecure_private_key for initial provisioning. Append it as an fall back.
Disable automatic key generation and prevent vagrant from replacing the default insecure key with a new random key.
config.ssh.insert_key = falseCopy the public key into the box
config.vm.provision "file", source: "keys/public", destination: "~/.ssh/authorized_keys"Disable password authentication (default creds: vagrant:vagrant)
config.vm.provision "shell", inline: <<-EOC sudo sed -i -e "\\#PasswordAuthentication yes# s#PasswordAuthentication yes#PasswordAuthentication no#g" /etc/ssh/sshd_config sudo service ssh restartEOCvagrant up and connect the box with the private key.
Vagrant.configure(2) do |config| config.vm.box = "ubuntu/trusty64"
# ssh settings config.ssh.insert_key = false config.ssh.private_key_path = ["keys/private", "~/.vagrant.d/insecure_private_key"] config.vm.provision "file", source: "keys/public", destination: "~/.ssh/authorized_keys" config.vm.provision "shell", inline: <<-EOC sudo sed -i -e "\\#PasswordAuthentication yes# s#PasswordAuthentication yes#PasswordAuthentication no#g" /etc/ssh/sshd_config sudo service ssh restart EOCend