简体   繁体   English

如何在Linux Vagrant VM内设置桌面用户,使其表现为具有通常用户名,ssh键等的标准工具控制台?

[英]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.

相关问题 如何设置我的Linux X终端以便Emacs可以访问256种颜色? - How do I set up my Linux X terminal so that Emacs has access to 256 colors? 如何为Linux设置应用程序的桌面图标:KDE,Gnome等? - How to set my application's desktop icon for Linux: KDE, Gnome etc? 退出非root用户的ssh后,如何保持Linux程序运行? - How can I keep my Linux program running after I exit ssh of my non-root user? 如何获取桌面上像素的颜色? (Linux)的 - How can I grab the color of a pixel on my desktop? (Linux) 无法通过 SSH 连接到 Vagrant VM(不使用 vagrant ssh)第 2 部分 - Can't SSH into Vagrant VM (without using vagrant ssh) part 2 在Zenoss上通过snmp / WMI或在Linux屏幕/ ssh会话/ w等上监视和捕获有关远程VM的活动会话详细信息。 - Monitor and capture active session details on Zenoss about remote vm via snmp/WMI or on linux screen/ssh session/w etc. 如何在 Linux 中添加具有不同 SSH 密钥的用户? - How can I add users with different SSH keys in Linux? AWS:为我的用户添加 SSH 密钥 - AWS: Adding SSH keys for my user 关闭PuTTY SSH会话后,如何运行仍在运行的Linux命令? - How can I run a Linux command that still runs after I close my PuTTY SSH session? 如何将exisintg Linux VM克隆到Azure中的规模集 - How can I clone an exisintg Linux VM to a scale set in Azure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM