Skip to content

Instantly share code, notes, and snippets.

@maxim
Created June 13, 2014 01:36
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save maxim/dafc3b6da5754419babb to your computer and use it in GitHub Desktop.
Save maxim/dafc3b6da5754419babb to your computer and use it in GitHub Desktop.
def authorize_key_for_root(config, *key_paths)
[*key_paths, nil].each do |key_path|
if key_path.nil?
fail "Public key not found at following paths: #{key_paths.join(', ')}"
end
full_key_path = File.expand_path(key_path)
if File.exists?(full_key_path)
config.vm.provision 'file',
run: 'once',
source: full_key_path,
destination: '/home/vagrant/root_pubkey'
config.vm.provision 'shell',
privileged: true,
run: 'once',
inline:
"echo \"Creating /root/.ssh/authorized_keys with #{key_path}\" && " +
'rm -f /root/.ssh/authorized_keys && ' +
'mv /home/vagrant/root_pubkey /root/.ssh/authorized_keys && ' +
'chown root:root /root/.ssh/authorized_keys && ' +
'chmod 600 /root/.ssh/authorized_keys && ' +
'rm -f /home/vagrant/root_pubkey && ' +
'echo "Done!"'
break
end
end
end
@bearproof
Copy link

Hi, I just added this in my fork to make sure the /root/.ssh directory exists:

 'mkdir -p /root/.ssh/ && ' +

The change is here If you're interested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment