SSH到具有不同用户名的Vagrant Box

Kev*_*ith 41 ssh vagrant

我不想使用"流浪"用户名和密码ssh到我的Vagrant虚拟机上,而是想使用kevin/kevin.

我修改Vagrantfile了包括:

config.ssh.username = "kevin"

然后,我跑了vagrant reload.

以下输出显示:

[default] Waiting for machine to boot. This may take a few minutes...
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period. This can
mean a number of things.
Run Code Online (Sandbox Code Playgroud)

但是,我仍然可以使用ssh到我的流浪盒上vagrant/vagrant,但我不能使用kevin/kevin或kevin/vagrant的用户名和密码ssh到盒子上.

请注意,我也尝试了这个答案(/sf/answers/694688571/),但我只能使用用户名ssh到框中vagrant,而不是kevin(尽管在其中指定Vagrantfile).

如何配置我的Vagrantfile以便我可以使用用户名进行ssh kevin

Ter*_*ang 53

您可以使用vagrant但不是kevin,这是预期的.

大多数流浪基地盒只有2个用户提供SSH访问,rootvagrant.它们都vagrant用作密码,另外,vagrant配置为使用GitHub上的Vagrant项目中提供的不安全(为什么?默认情况下看到Vagrant不安全?)密钥对进行公钥认证.

为了能够登录kevin,你必须首先在框中创建用户(useradd -m -s /bin/bash -U kevin),配置公钥认证(许多方式,例如ssh-copy-id,我会留给你.)

vagrant ssh如果正确设置config.ssh.username,您应该能够在创建用户后使用ssh进入框中Vagrantfile.

当然你可以手动ssh到框中(假设NAT正在使用中)

ssh -p 2222 kevin@localhost

或(在Linux上)

ssh -p 2222 -i /opt/vagrant/embedded/gems/gems/vagrant-1.5.1/keys/vagrant.pub vagrant@localhost

  • 您可以将ssh放入带有用户流浪者的框,成为root用户,然后将文件/home/vagrant/.ssh/authorized_keys复制到/home/kevin/.ssh/authorized_keys。确保/home/kevin/.ssh/authorized_keys的所有权和许可权确定(600,所有者kevin:kevin组)。 (2认同)

Ben*_*y K 23

通过您的配置脚本将用户添加到Vagrant之后的另一个解决方案:

## add kevin
useradd -m -s /bin/bash -U kevin -u 666 --groups wheel
cp -pr /home/vagrant/.ssh /home/kevin/
chown -R kevin:kevin /home/kevin
echo "%kevin ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/kevin
Run Code Online (Sandbox Code Playgroud)

将此添加到您的Vagrant文​​件:

VAGRANT_COMMAND = ARGV[0]
if VAGRANT_COMMAND == "ssh"
  config.ssh.username = 'kevin'
end
Run Code Online (Sandbox Code Playgroud)

现在,Vagrant将使用默认vagrant用户来配置您的VM,但是一旦启动,您可以使用simple vagrant sshkevin通过默认的Vagrant ssh-key登录.

通过这种方式,您可以发送您想要的Vagrantfile用户,只需说出vagrant upkevin自动准备使用.

  • 您知道如何存储密码吗?执行 config.ssh.password 似乎并没有达到目的。谢谢! (2认同)

Aid*_*len 6

创建一个像下面这样的流浪文件。请注意,我们正在引导 vagrant 用户立即更改为我们创建的 'kevin' 用户。

bootstrap = <<SCRIPT
  useradd -m kevin --groups sudo
  su -c "printf 'cd /home/kevin\nsudo su kevin' >> .bash_profile" -s /bin/sh vagrant
SCRIPT

Vagrant.configure("2") do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.host_name = "kevin"
  config.vm.provision "shell", inline: "#{bootstrap}", privileged: true
end
Run Code Online (Sandbox Code Playgroud)

现在通过 ssh 进入虚拟机:

$ vagrant ssh
Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-83-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

0 packages can be updated.
0 updates are security updates.


To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

kevin@kevin:/home/vagrant$
Run Code Online (Sandbox Code Playgroud)