Skip to content

Instantly share code, notes, and snippets.

@dacodekid
Created September 25, 2017 17:50
Show Gist options
  • Save dacodekid/57a7ac3ff69cc1665bfa6ef3539cd923 to your computer and use it in GitHub Desktop.
Save dacodekid/57a7ac3ff69cc1665bfa6ef3539cd923 to your computer and use it in GitHub Desktop.
Vagrant Root Login On Provision
Vagrant.configure(2) do |config|
config.vm.box = "debian/jessie64"
# --- IF private_key FILE EXISTS, THEN LOGIN AS root
if File.exist?(Dir.getwd + "/.vagrant/machines/default/virtualbox/private_key")
config.ssh.username = "root"
config.ssh.private_key_path = "~/.ssh/id_rsa"
end
# --- COPY PUBLIC KEY INTO MACHINE ---
ssh_pub_key = File.readlines("#{Dir.home}/.ssh/id_rsa.pub").first.strip
config.vm.provision "private_key", type: "shell" do |s|
s.inline = <<-SCRIPT
mkdir -p /root/.ssh/
echo #{ssh_pub_key} > /root/.ssh/authorized_keys
SCRIPT
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment