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

Vagrant更改默认的SSH端口的更多相关文章

  1. 阿里云修改默认的ssh端口

    Linux服务器的ssh服务支持远程访问服务器,默认的ssh端口号是22.为了安全起见,很多用户会将端口号由22改为其他的端口号.  如果遇到修改端口号并重启ssh服务后,新的端口号不生效,请参考以下 ...

  2. 如何更改Linux的ssh端口

    1. 修改/etc/ssh/sshd_config里的Port字段 Port 22改为Port 1000(你自定义的端口) 2. 重启sshd服务 #service sshd restart

  3. Ubuntu 16.04修改ssh端口

    1 安装防火墙 sudo apt-get install ufw启用 sudo ufw enable sudo ufw default deny 作用:开启了防火墙并随系统启动同时关闭所有外部对本机的 ...

  4. Spring Boot - 修改Tomcat默认的8080端口

    前言 默认情况下,Spring Boot内置的Tomcat服务会使用8080端口启动,我们可以使用以下任何技巧去更改默认的Tomcat端口: 注:我们可以通过server.port=0配置,去自动配置 ...

  5. 修改ssh端口

    centos7.5修改默认SSH端口 linux SSH默认端口是22,不修改的话存在一定的风险,要么是被人恶意扫描,要么会被人破解或者攻击,所以我们需要修改默认的SSH端口 1.修改22端口为620 ...

  6. ssh更改默认端口号及实现免密码远程登陆

    近来在复习防火墙管理工具 iptables 的基本使用方法,涉及到对端口添加或删除防火墙策略的内容,之前对ssh更改默认端口号及免密码登录的方法不熟悉,这次做一个基本的总结防止自己遗忘. 错误偏差及其 ...

  7. LINUX SSH修改默认22/添加端口

    通常ssh远程登录的默认端口是22,但是因为端口22属于高危端口,因此很多时候作为服务器会被关掉,不过这个端口一般是可以更改或者添加的,这样除了22端口,也可以通过别的端口进行访问. 1.首先修改配置 ...

  8. webstorm 10 更改默认端口

    快捷键 ctrl + Alt + s :打开配置 如图下可以更改 默认端口.快捷键可能跟qq的快捷键冲突,可以关闭qq来设置端口

  9. centos 7.0 修改ssh默认连接22端口 和 添加防火墙firewalld 通过端口

    首先 先做的就是 修改ssh的默认端口22 需要修改文件 /etc/ssh/sshd_config 使用命令 vi /etc/ssh/sshd_config [root@localhost ~]# v ...

随机推荐

  1. Elasticsearch-PHP 概述

    最近在学习使用Elasticsearch,并且是和PHP一起使用的,看到了Elasticsearch-PHP,其实是Elasticsearch为PHP提供的客户端,那么我们来学习一下API文档,如何在 ...

  2. C#中打开设计视图时报"未将对象引用设置到对象的实例"

    通常情况下,若是你将用户控件写好了放入窗体中,若是有不合理的代码,则会弹出错误提示框,不让你放.若是你之前只是随便加了一个用户控件,并且没有什么问题,但后来你又把控件改坏掉了,那么你打开就会报错(在窗 ...

  3. Linux实战教学笔记32:企业级Memcached服务应用实践

    一, Memcached介绍 1.1 Memcached与常见同类软件对比 (1)Memcached是什么? Memcached是一个开源的,支持高性能,高并发的分布式内存缓存系统,由C语言编写,总共 ...

  4. Scrapy Test

    (flappbird) luo@luo-ThinkPad-W540:~$ scrapy startproject myspider0315New Scrapy project 'myspider031 ...

  5. 系统性能信息模块之psutil模块

    一.psutil模块介绍 官方网址:https://pypi.org/ psutil模块安装:https://github.com/giampaolo/psutil/blob/master/INSTA ...

  6. code1006 等差数列

    我绞尽脑汁想一个更好的算法,然而不能如愿,只好写一个n^3的了 很简单,就是暴力搜索(还好n<100) 先排序,然后循环i=1ton,j=i+1ton 把a[i]a[j]确定为等差数列开始的两个 ...

  7. 设置ctp文件按html文件解析

  8. [Fiddler] 在 Composer 中对HTTP报文进行编辑,重新执行

    如果想要对某个HTTP请求进行编辑,然后重新执行观察结果,可以将这个报文从左侧拖放到右侧的Composer里面 这样就可以很方便的修改HTTP请求的方法,报文头,报文体 在用 SoapUI 做 API ...

  9. CNN和GAN 比较nice的介绍

    CNN是个什么鬼:https://www.zhihu.com/question/52668301 就是这样GAN : https://zhuanlan.zhihu.com/p/27199954

  10. Mosquitto 单向SSL配置

    Mosquitto 单向SSL配置 摘自:https://blog.csdn.net/a_bcd_123/article/details/70167833 2017年04月14日 06:56:06 s ...