简体   繁体   中英

How can I set up my desktop user inside a Linux Vagrant VM so that it behaves like a standard tools console with my usual username, ssh keys, etc.?

I am a Windows user who uses Linux a lot for development work.

In my company, developers' tastes for desktops vary (Mac, Windows, Arch Linux) but we use Vagrant VMs to make sure that everyone has a common environment for development.

One specific annoyance on Windows is the lack of Linux compatible tools. There are various ways around it, like msys, cygwin but nothing works better than a fully compatible tools console (Ubuntu 14.04 in our case).

So, I made a Vagrant VM for it but discovered that my standard login id, ssh keys inside C:/Users/devang/.ssh had to be created manually inside the VM.

Is there a standard way to build it in Vagrant?

There were some useful answers in stackoverflow related to configuration of the default vagrant user but in the end I had to make my own inline provisioner.

I am posting my solution here. It does not make any assumption about host OS but it does assume a Debian/Ubuntu Vagrant VM as the guest.

The key part is the inline shell provisioner which provides access to the variable Dir.home and ENV['USER'] from the host OS.

AFAIK, to have access to those variables from host OS, one has to use an inline provisioner.

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.hostname = "tools"
  config.vm.provision "shell" do |s|
    ssh_pub_key = File.open("#{Dir.home}/.ssh/id_rsa.pub", "rb").read
    ssh_key = File.open("#{Dir.home}/.ssh/id_rsa", "rb").read
    user_name = ENV['USER'].downcase
    home_dir = "/home/#{user_name}"
    s.inline = <<-SHELL
       sudo useradd -d "#{home_dir}" -m "#{user_name}" -s /bin/bash
       sudo chmod 755 "#{home_dir}"
       sudo mkdir "#{home_dir}/.ssh"
       sudo chmod 700 "#{home_dir}/.ssh"
       sudo echo "#{ssh_pub_key}" > "#{home_dir}/.ssh/id_rsa.pub"
       sudo echo "#{ssh_key}" > "#{home_dir}/.ssh/id_rsa"
       sudo echo "#{ssh_pub_key}" > "#{home_dir}/.ssh/authorized_keys"
       sudo chown -R "#{user_name}.#{user_name}" "#{home_dir}/.ssh"
       sudo chmod -R 600 "#{home_dir}/.ssh/id_rsa"
       sudo usermod -a -G sudo "#{user_name}"
       echo "#{user_name} ALL=(ALL) NOPASSWD: ALL" | sudo cat > "/etc/sudoers.d/#{user_name}"
    SHELL
end
  config.vm.provision :shell, path: "contrib/build-server.sh"
  config.vm.box = "debian/contrib-jessie64"
  config.vm.network "private_network", ip: "192.168.70.150"

  config.vm.provider "virtualbox" do |v|
        v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
        v.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
        v.cpus = 4
  end

end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM