Vagrant更改默认的SSH端口

栏目: 服务器 · 发布时间: 6年前

内容简介:Vagrant更改默认的SSH端口

Vagrant默认转发宿主的2222端口到虚拟机的22端口(默认设置,无须配置)。在有多个虚拟机并存的情况下,2222端口将不好使。具体表现在:

  1. 启动第二个虚拟机的时候,会报端口占用错误:
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
Vagrant cannot forward the specified ports on this VM, since they would collide with some other application that is already listening on these ports. The forwarded port to 2222 is already in use on the host machine.
 
To fix this, modify your current project's Vagrantfile to use another port. Example, where '1234' would be replaced by a unique host port:
 
  config.vm.network :forwarded_port, guest: 22, host: 1234
 
Sometimes, Vagrant will attempt to auto-correct this for you. In this case, Vagrant was unable to. This is usually because the guest machine is in a state which doesn't allow modifying port forwarding. You could try 'vagrant reload' (equivalent of running a halt followed by an up) so vagrant can attempt to auto-correct this upon booting. Be warned that any unsaved work might be lost.
  1. 连接不同机器的SSH,会提示密钥不符:
$ ssh vagrant@localhost -p2222
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!    @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that a host key has just been changed. The fingerprint for the RSA key sent by the remote host is
  SHA256:OmHXzdSe6B+pO6+xfvvFqKk40HAgzruemVeYbWYdF8c.
Please contact your system administrator. Add correct host key in /home/tlanyan/.ssh/known_hosts to get rid of this message. Offending ECDSA key in /home/tlanyan/.ssh/known_hosts:6 RSA host key for [localhost]:2222 has changed and you have requested strict checking.
Host key verification failed.

为了解决这个烦人的问题,只有为不同的虚拟机分配不同的主机端口。

根据提示,直接在Vagrantfile中更改转发端口:

config.vm.network "forwarded_port", guest: 22, host: 3333

然后启动机器,发现2222端口没有直接更改为3333,而是新增了3333端口:

$ vagrant reload
==> default: Attempting graceful shutdown of VM...
    default: Guest communication could not be established! This is usually because
    default: SSH is not running, the authentication information was changed,
    default: or some other networking issue. Vagrant will force halt, if
    default: capable.
==> default: Forcing shutdown of VM...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 3333 (host) (adapter 1)
    default: 22 (guest) => 2222 (host) (adapter 1)
    ....

没办法,只好上网查查怎么回事。在官方的GitHub上,有相关 issue ,标题很有意思:“give a chance to disable default ssh port forwarding”。根据回复,解决办法是先禁用默认的ssh转发,再添加自定义转发:

config.vm.network "forwarded_port", guest: 22, host: 2222, id: "ssh", disabled: "true"
config.vm.network "forwarded_port", guest: 22, host: 3333

注意:第一条规则必须有 id: "ssh" ,否则会报如下错误:

Forwarded port ‘2222’ (host port) is declared multiple times with the protocol ‘tcp’.

重启后,即可看到转发端口已经更改为3333。

参考

  1. https://www.vagrantup.com/docs/vagrantfile/machine_settings.html
  2. https://github.com/mitchellh/vagrant/issues/1922

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

游戏测试精通

游戏测试精通

舒尔茨 / 周学毛 / 清华大学出版社 / 2007-9 / 48.00元

《游戏测试精通》来自3位在游戏测试领域都有着极其丰富经验的专业人员,是亚马逊“五星级”畅销书,也是国内第一本专业级游戏测试经典之作,不仅内容全面、实例丰富,而且讲解透彻、可读性强,并提供多个资源下载和技术支持站点。现如今,游戏产业发展迅猛,游戏测试已成为游戏产品、游戏软件、游戏程序设计与开发不可或缺的环节。《游戏测试精通》主要揭示了如何将软件测试的专业方法运用到游戏产业中,全面涵盖了游戏测试的基本......一起来看看 《游戏测试精通》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

MD5 加密
MD5 加密

MD5 加密工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具