[英]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. 我是Windows用户,经常使用Linux进行开发工作。
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. 在我的公司中,开发人员对台式机(Mac,Windows,Arch Linux)的喜好各不相同,但我们使用Vagrant VM来确保每个人都有共同的开发环境。
One specific annoyance on Windows is the lack of Linux compatible tools. Windows上的一个特殊烦恼是缺少Linux兼容工具。 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).
有多种解决方法,例如msys,cygwin,但是没有什么比完全兼容的工具控制台(在我们的示例中为Ubuntu 14.04)更好。
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. 因此,我为此制作了一个Vagrant VM,但发现必须在VM内部手动创建标准登录ID,即C:/Users/devang/.ssh中的ssh密钥。
Is there a standard way to build it in Vagrant? 在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. 在stackoverflow中,有一些有用的答案与默认的无业游民用户的配置有关,但最后,我必须自己创建内联供应商。
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.
它没有对主机操作系统做任何假设,但确实将Debian / Ubuntu Vagrant VM作为来宾。
The key part is the inline shell provisioner which provides access to the variable Dir.home and ENV['USER'] from the host OS. 关键部分是嵌入式外壳配置程序,它可从主机OS访问变量Dir.home和ENV ['USER']。
AFAIK, to have access to those variables from host OS, one has to use an inline provisioner. AFAIK要想从主机OS访问这些变量,就必须使用内联配置器。
# -*- 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.