本文介绍了使用不同的用户名通过 SSH 连接到 Vagrant Box的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与其使用vagrant"用户名和密码通过 ssh-ing 连接到我的 Vagrant 虚拟机,我更愿意使用 kevin/kevin.

Rather than ssh-ing onto my Vagrant virtual machine with a "vagrant" user-name and password, I'd like to use kevin/kevin.

我修改了我的 Vagrantfile 以包含:

I modified my Vagrantfile to include:

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.

然而,我仍然可以使用 vagrant/vagrant ssh 到我的 vagrant 盒子,但我无法使用用户名和密码 kevin/kevin 或 kevin/vagrant ssh 到盒子.

However, I could still ssh onto my vagrant box using vagrant/vagrant, yet I I couldn't ssh onto the box with a user-name and password of kevin/kevin or kevin/vagrant.

请注意,我也尝试过这个答案(https://stackoverflow.com/a/9924122/409976),但我只能使用用户名 vagrant SSH 到盒子上,而不是 kevin(即使它在 Vagrantfile 中指定).

Note that I also tried this answer (https://stackoverflow.com/a/9924122/409976), but I could only ssh onto the box with user-name vagrant, not kevin (even though it's specified in the Vagrantfile).

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

How can I configure my Vagrantfile so that I can ssh using user-name kevin?

推荐答案

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

You can ssh to the box using vagrant but NOT kevin, that's expected.

大多数 Vagrant 基本框只有 2 个具有 SSH 访问权限的用户,rootvagrant.他们都使用 vagrant 作为密码,此外,vagrant 被配置为使用不安全的公钥认证(为什么?参见 Vagrant 默认不安全?)GitHub 上的 Vagrant 项目中提供的密钥对.

Most Vagrant base boxes have only 2 users with SSH access, root and vagrant. They both use vagrant as password, in addition, vagrant is configured for public key authentication using the insecure (why? see Vagrant insecure by default?) key pair provided in the Vagrant project on GitHub.

为了能够以 kevin 身份登录,您必须通过 ssh 进入框并创建用户 (useradd -m -s/bin/bash -U kevin>) 首先,配置公钥认证(很多方法比如ssh-copy-id,我会留给你.)

To be able to login as kevin, you'll have to ssh into the box and create the user (useradd -m -s /bin/bash -U kevin) first, configure public key authentication (many ways e.g ssh-copy-id, I'll leave it to you.)

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

You should be able to ssh into the box after creating the user using vagrant ssh if you properly set config.ssh.username in Vagrantfile.

当然您可以通过(假设正在使用 NAT)手动 ssh 进入盒子

Of course you can manually ssh into the box by (assume NAT is in use)

ssh -p 2222 kevin@localhost

或(在 Linux 上)

or (on Linux)

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

这篇关于使用不同的用户名通过 SSH 连接到 Vagrant Box的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 14:02