Vagrant SSH key pair Setup: Essential Guide for DevOps Professionals

Introduction

In this tutorial, I will guide you through setting up an Vagrant ssh key pair. We’ll generate the SSH keys, where vagrant_rsa will the private key and vagrant_rsa.pub will serve as the public key. This allows you to log into the Virtual Machine without needing a password. Setting up Vagrant is crucial for those in DevOps roles.

Understanding SSH Key Management in Vagrant

When working with Vagrant, a tool that streamlines the creation and management of virtual development environments, it’s crucial to understand how SSH keys are handled. SSH keys play a vital role in securing access to your Vagrant virtual machines (VMs).

Vagrant SSH Key Location

By default, when you initiate a new Vagrant environment, Vagrant automatically generates an SSH key pair if none exists. This is done to ensure secure, password-less access to the created VM. The location of these SSH keys is typically within the Vagrant project directory .vagrant/machines/<machine-name>/virtualbox/private_key for VirtualBox users. This path might vary slightly depending on the provider you are using, such as VMware or Hyper-V.

Managing Vagrant SSH Keys

It’s important to know that Vagrant configures its VMs to use these automatically generated keys. However, for enhanced security or personal preference, you can configure Vagrant to use a custom SSH key pair. This involves specifying your private key in the Vagrantfile and ensuring the corresponding public key is authorized in the VM. Managing these keys properly ensures that access to your VM is both secure and restricted to authorized users only.

Below is the folder structure for the Vagrant project:

/home/huupv/project
/home/huupv/project/keys/.ssh

Vagrant SSH key pair

The first is to create a vagrant SSH key

Using the ssh-keygen command to create the private key and public key for a vagrant.

ssh-keygen

The output private key and public key files in “/home/huupv/project/keys/.ssh” folder as below:

vagrant_rsa vagrant_rsa.pub

To configure vagrant ssh key in Vagrantfile

To add the lines in the Vagrantfile file as below:

Vagrant.configure("2") do |config|
config.vm.box = "centos/6"
config.ssh.insert_key = false
config.vm.boot_timeout = 800
config.ssh.private_key_path = ["keys/.ssh/vagrant_rsa", "~/.vagrant.d/insecure_private_key"]
config.vm.provision "file", source: "keys/.ssh/vagrant_rsa.pub", destination: "~/.ssh/authorized_keys"
end
  • ~/.vagrant.d/insecure_private_key: You should append this default key. The use config.ssh.insert_key = false to Vagrant not generate a random key.
  • config.ssh.private_key_path: Changing Insecure Key To My Own Key On Vagrant box.
Vagrant SSH key pair

Conclusion

Finishing, We are customizing the vagrant SSH key with a Private/Public key. What you need to Private key saves in the host and the Public key copies authorized_keys into a vagrant box for Virtual Machine. Reference to configure vagrant SSH of the vagrant main site. Thank you for reading the DevopsRoles page!

, ,

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

2 thoughts on “Vagrant SSH key pair Setup: Essential Guide for DevOps Professionals

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.