Created
February 10, 2021 02:55
-
-
Save carthegian/9d024f2e042685b9eaf7e2f8d5e4ce9d to your computer and use it in GitHub Desktop.
Base VagrantFile to use ssh with custom username & custom public key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Vagrant.configure("2") do |config| | |
# Define vm box | |
config.vm.box = "gbailey/amzn2" | |
# Constraint vm box version | |
config.vm.box_version = "20210127.0.0" | |
# Copy public key from host to guest | |
# Prerequisite: need to have id_rsa.pub in host machine | |
# Use ssh-keygen to create keys in path e.g. Windows C:\Users\<USER_NAME>\.ssh\id_rsa/pub | |
config.vm.provision "file", source: "~/.ssh/id_rsa.pub", destination: "~/.ssh/id_rsa.pub" | |
# Append copied public key to target ssh username authorized_keys | |
config.vm.provision "shell", inline: <<-SHELL | |
sudo cat /home/vagrant/.ssh/id_rsa.pub >> /home/ec2-user/.ssh/authorized_keys | |
SHELL | |
# Ssh using target username | |
VAGRANT_COMMAND = ARGV[0] | |
if VAGRANT_COMMAND == "ssh" | |
config.ssh.username = "ec2-user" | |
config.ssh.private_key_path = "C:/Users/<USER_NAME>/.ssh/id_rsa" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment