【Homestead Master】系列是Laravel底层核心技术实战揭秘这一课程里的第一章,因为考虑到该章内容学员们可能要经常来回查阅,同时也为了提高课程更新速度,所以这一章以文字的形式发布在此。

有时候你想让同事远程登陆到你的homestead虚拟机,以寻求帮助或建议,就像你本地执行了vagrant ssh进入虚拟机一样,或者像你ssh进了线上服务器一样,这个时候怎么办呢?

还是要用到我们之前提到的vagrant share命令,通过在其后面添加--ssh参数,我们可以为本地虚拟机开启远程ssh访问的通道,执行vagrant share --ssh试试:

$ vagrant share --ssh
==> homestead-7: Detecting network information for machine...
    homestead-7: Local machine address: 127.0.0.1
    homestead-7:
    homestead-7: Note: With the local address (127.0.0.1), Vagrant Share can only
    homestead-7: share any ports you have forwarded. Assign an IP or address to your
    homestead-7: machine to expose all TCP ports. Consult the documentation
    homestead-7: for your provider ('virtualbox') for more information.
    homestead-7:
    homestead-7: Local HTTP port: 44300
    homestead-7: Local HTTPS port: 44300
    homestead-7: SSH Port: 2222
    homestead-7: Port: 2222
    homestead-7: Port: 27017
    homestead-7: Port: 33060
    homestead-7: Port: 44300
    homestead-7: Port: 54320
    homestead-7: Port: 8000
    homestead-7: Port: 8025
==> homestead-7: Generating new SSH key...
    homestead-7: Please enter a password to encrypt the key:
		homestead-7: Repeat the password to confirm:

可以看到它会为你创建一个新的ssh key,同时让你输入一个password来加密你的ssh key,后期他人要远程ssh 连接你的虚拟机时,是需要输入这个密码的。

homestead-7: Inserting generated SSH key into machine...
==> homestead-7: Checking authentication and authorization...
==> homestead-7: Creating Vagrant Share session...
    homestead-7: Share will be at: joyful-hedgehog-6012
==> homestead-7: Your Vagrant Share is running! Name: joyful-hedgehog-6012
==> homestead-7: URL: //joyful-hedgehog-6012.vagrantshare.com
==> homestead-7:
==> homestead-7: You're sharing your Vagrant machine in "restricted" mode. This
==> homestead-7: means that only the ports listed above will be accessible by
==> homestead-7: other users (either via the web URL or using `vagrant connect`).
==> homestead-7:
==> homestead-7: You're sharing with SSH access. This means that another user
==> homestead-7: simply has to run `vagrant connect --ssh joyful-hedgehog-6012`
==> homestead-7: to SSH to your Vagrant machine.
==> homestead-7:
==> homestead-7: Because you encrypted your SSH private key with a password,
==> homestead-7: the other user will be prompted for this password when they
==> homestead-7: run `vagrant connect --ssh`. Please share this password with them
==> homestead-7: in some secure way.

输入并确认密码后,就会如上显示,跟之前我们vagrant share --http一样,这个时候也是给分配了一个公网访问的URL: //joyful-hedgehog-6012.vagrantshare.com,同时下面提示别人可以通过vagrant connect --ssh joyful-hedgehog-6012来登陆你的虚拟机。这个时候我们找另一台电脑执行一下试试:

$ vagrant connect --ssh joyful-hedgehog-6012
You must logged in to Vagrant Cloud in order to use `vagrant share`.
Please log in with `vagrant login`.

由于这个电脑是第一次使用vagrant shareconnect这些命令,所以提示需要vagrant login,这个之前讲过了。我们登陆以后再执行一下看看:

$ vagrant connect --ssh joyful-hedgehog-6012
Loading share 'joyful-hedgehog-6012'...
The SSH key to connect to this share is encrypted. You will require
the password entered when creating to share to decrypt it. Verify you
access to this password before continuing.

Press enter to continue, or Ctrl-C to exit now.
Password for the private key:
Executing SSH...
Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-66-generic x86_64)

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

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

Last login: Thu Jun 22 07:27:58 2017 from 10.0.2.2

可以看到中间它让我们输入一下之前设定的ssh password,输入了以后就成功登入了远端的虚拟机,就可以进行相应的协助或操作了。

可能你想在允许别人ssh connect的时候,不开启公网访问的URL,那么可以加一个--disable-http的参数,假设vagrant share --ssh --disable-http:

 homestead-7: Share will be at: subtle-springbok-9542
==> homestead-7: Your Vagrant Share is running! Name: subtle-springbok-9542

==> homestead-7: You're sharing with SSH access. This means that another user
==> homestead-7: simply has to run `vagrant connect --ssh subtle-springbok-9542`
==> homestead-7: to SSH to your Vagrant machine.

这个时候就只是给你分配了一个Name,没有公网URL了,别人就只能通过vagrant connect --ssh subtle-springbok-9542来访问你的虚拟机了,当然这期间你可以像之前说过的,添加一个--name=pilishen的参数,来自定义这个连接Name。

如果你还想着只让这个人连接一次,那么命令里再加上--ssh-once这个参数。

好了,以后再寻求他人远程协助,就更简单了~