Skip to main content

SSH工具连接虚拟机中的Linux系统

· 26 min read
CheverJohn

TL; DR :)

文章内容描述

本篇文章主要聚焦一个实操点:虚拟机(VMware)中Centos-7系统的网络配置以及如何从宿主机使用功能ssh工具连接到虚拟机(VMware)中的Centos-7系统。

本篇文章主要涉及到的原理知识为:

  • 计算机网络中的子网掩码网关是什么
  • VMware主要的网络通信方式:桥接、NAT

所以,阅读完本篇文章,你将会收获......

计算机网络中两个基础知识——子网掩码网关,以及VMware的网络方案架构

感慨啊~真的是好久没有接触这个东西,然后又因为我的计算机网络知识基础也太不牢固了叭,居然老是忘记配置centos的子网掩码和 网关,就用这篇文章好好的巩固一下吧。

看是解决不了问题的,要实操! 韶光易逝,劝君惜取少年时! ——笔者(CheverJohn)疯狂寄语hhhh

实际操作

本地环境说明

windows version: 21H1(OS Build 19043.1415)

远程连接工具: Mobaxterm

安装centos(主要是指网络配置)

我们在安装Centos的时候,会经历一个网络环境配置的问题。

当然安装Centos的过程我就不讲了,这边重点还是给Centos进行网络配置的过程。

VMware中的linux网络配置从VMware本身层面来讲,有三种

1. Bridged   桥接模式
2. NAT NAT模式
3. Host-only

就先这么叫着,我这边介绍一种方法——Bridged 桥接模式(我认为很简单的方法)。

桥接模式(wifi下也可以用)

简单介绍一下桥接模式

桥接网络是指本地物理网卡和虚拟网卡通过VMnet0虚拟交换机进行桥接,物理网卡和虚拟网卡在拓扑图上处于同等地位,那么物理网卡和虚拟网卡就相当于处于同一个网段,虚拟交换机就相当于一台现实网络中的交换机,所以两个网卡的IP地址也要设置为同一网段。

所以当我们要在局域网使用虚拟机,对局域网其他pc提供服务时,例如提供ftp,提供ssh,提供http服务,那么就要选择桥接模式。

例如大学宿舍里有一个路由器,宿舍里四个人连接这个路由器,路由器的wanip就不理会了,这个ip是动态获取的,而lanip默认是192.168.1.1,子网掩码是255.255.255.0。而其他四个人是自动获取ip,假设四个人的ip是:

A:192.168.1.100/255.255.255.0, B:192.168.1.101/255.255.255.0, C:192.168.1.102/255.255.255.0, D:192.168.1.103/255.255.255.0

那么虚拟机的ip可以设置的ip地址是192.168.1.2-192.168.1.99,192.168.1.104-192.168.1.254(网络地址全0和全1的除外,再除去ABCD四个人的ip地址)

那么虚拟机的ip地址可以设置为192.168.1.98/255.255.255.0,设置了这个ip地址,ABCD这四个人就可以通过192.168.1.98访问虚拟机了,如果虚拟机需要上外网,那么还需要配置虚拟机的路由地址,就是192.168.1.1了,这样,虚拟机就可以上外网了,但是,上网我们一般是通过域名去访问外网的,所以我们还需要为虚拟机配置一个dns服务器,我们可以简单点,把dns服务器地址配置为google的dns服务器:8.8.8.8,到此,虚拟机就可以上网了。

来源自链接

具体的原理见下面的详细讲解

配置桥接模式

emmmmm,还是看B站CodeSheep的视频叭,因为我发现我的配置好了,截图好像发出来不是桥接模式的截图,是NAT的,就很迷。

固定住centos的IP地址

当我们刚安装好,centos的网卡配置文件有几项是关闭的,我们需要打开一下,然后设置成如下图所示:

centos的网卡配置文件

没错这边,我的固定ip地址为192.168.2.233,然后我为其配置的网关为192.168.2.1。

至于我为什么这么配置,还请看下面的原理讲解。

然后配置完成后,就得重启网卡咯

# centos7
systemctl restart network

PS: 这边其实还需要注意一点,因为选择了桥接模式,那么我们的虚拟机其实是和宿主机是在同一个网段的,我宿主机的IP地址是192.168.2.228,然后我的虚拟机里的IP地址是192.168.2.233,前三个网段很明显,我设置的静态IP很明显。

测试是否ping通

在虚拟机里ping宿主机

虚拟机ping宿主机

在宿主机里ping虚拟机

宿主机ping虚拟机

原理讲述

VMware网络连接原理

来源

提前声明,本部分内容大部分来自于官方文档

官方文档,yyds,永远的神!

此外我加入了我个人的理解,就是这样。

Bridged Networking(桥接网络)

原理讲解

首先介绍的就是我们的桥接网络配置。

先干上一大段来自官方文档的说明

When you install Workstation Pro on a Windows or Linux host system, a bridged network (VMnet0) is set up for you. Bridged networking connects a virtual machine to a network by using the network adapter on the host system. If the host system is on a network, bridged networking is often the easiest way to give the virtual machine access to that network.

With bridged networking, the virtual network adapter in the virtual machine connects to a physical network adapter in the host system. The host network adapter enables the virtual machine to connect to the LAN that the host system uses. Bridged networking works with both wired and wireless host network adapters.

Bridged networking configures the virtual machine as a unique identity on the network, separate from and unrelated to the host system. The virtual machine is a full participant in the network. It has access to other machines on the network, and other machines on the network can contact it as if it were a physical computer on the network.

看到大段英文不要怕哈,要么勇敢地读下去,要么直接转去百度翻译hhhh

上面来自官方文档的开场白,主要说的就是桥接模式是一种最简单的联网模式。而且使用了桥接模式之后,虚拟机就相当于独立的一台物理机在局域网中(如下图所示),就相当于和宿主机同时连接在了一台网络交换机上的两个不同的端口上。虽然这俩其实都在一台实体的电脑上hhhh,就很神奇是不是。但我想如果你能够理解网卡就相当于一台设备在网络中的“唯一标识“之后,这点就很容易理解啦!

Bridged Networking Configuration

Bridged Networking Configuration

You can view and change the settings for bridged networking on the host system, determine which network adapters to use for bridged networking, and map specific host network adapters to specific virtual switches.

你也可以在主机系统上查看和更改桥接网络的设置,确定使用哪些网络适配器进行桥接网络,并将特定的主机网络适配器映射到特定的虚拟交换机。

唉,就相当于是说在宿主机上进行网卡的相关配置。我命令哪些网卡去做与哪些虚拟机系统相关的操作尔尔。

提供各种玩法

Assigning IP Addresses in a Bridged Networking Environment

A virtual machine must have its own identity on a bridged network. For example, on a TCPIP network, the virtual machine needs its own IP address. Your network administrator can tell you whether IP addresses are available for virtual machines and which networking settings to use in the guest operating system.

虚拟机在桥接网络上必须有自己的标识。例如,在TCPIP网络中,虚拟机需要自己的IP地址。网络管理员可以告诉您虚拟机的IP地址是否可用,以及在客户操作系统中使用哪些网络设置。

我理解哈,如果要让虚拟机的网络能够正常使用,那么就得按照我上头讲的配置(固定系统IP),然后就能跑通啦。

关于这一大段话的详细解释在这个链接我觉得我的理解也没啥问题。

Add a Bridged Network

When you install Workstation Pro on a Windows or Linux host system, a bridged network (VMnet0) is set up for you. If you install Workstation Pro on a host system that has multiple network adapters, you can configure multiple bridged networks.

啊这这这这~总感觉这段是废话,有种那种说了半天还是废话的感觉。。。

Configure Bridged Networking for an Existing Virtual Machine

You can configure bridged networking for an existing virtual machine.[了解更多内容]

emmm,这个我该咋说呢?字面意思啊,你可以为一个已存在的虚拟机配置桥接,自己个点了解更多内容去看叭。

Change VMnet0 Bridged Networking Settings

By default, VMnet0 is set to use auto-bridging mode and is configured to bridge to all active network adapters on the host system. You can use the virtual network editor to change VMnet0 to bridge to one specific host network adapter, or restrict the host network adapters that VMnet0 auto-bridges to. The changes you make affect all virtual machines that use bridged networking on the host system.[了解更多内容]

我们还可以改变VMnet0的设置哦,去和自己想要的宿主机网卡适配器进行配置呢。详情请点击上方链接噻。

Network Address Translation(NAT模式)

原理讲解

字面意思哦,网络地址翻译,大家可以这么理解,相当于一种绑定,键值对的绑定hhhh。拿数据库的东西来理解呢。

首先开始一波官方的自我介绍

When you install Workstation Pro on a Windows or Linux host system, a NAT network (VMnet8) is set up for you. When you use the New Virtual Machine wizard to create a typical virtual machine, the wizard configures the virtual machine to use the default NAT network.

With NAT, a virtual machine does not have its own IP address on the external network. Instead, a separate private network is set up on the host system. In the default configuration, virtual machines get an address on this private network from the virtual DHCP server.

这边开始要出现“真东西”咯,虚拟出一个DHCP server

我直接说 好处:独立出一个DHCP服务器来分配域名,虚拟机不会占用宿主机的IP,不会有IP冲突的风险,当然你的使用程度过少(用到的虚拟机太少,体现不了这种差距) 缺点:内网中的其他人无法和虚拟机通讯(或者说很难进行通信,实际上我还没有成功配置过NAT模式下宿主机与虚拟之间的通信呢)

NAT Configuration

NAT Configuration

The virtual machine and the host system share a single network identity that is not visible on the external network. NAT works by translating the IP addresses of virtual machines in the private network to the IP address of the host system. When a virtual machine sends a request to access a network resource, it appears to the network resource as if the request is coming from the host system.

虚拟机和主机系统共享一个在外部网络中不可见的网络标识。NAT的工作原理是将私有网络中的虚拟机的IP地址转换为主机系统的IP地址。当虚拟机发送访问网络资源的请求时,网络资源会认为这个请求来自主机系统。

NAT 模式主要还是起到了一个翻译的中转站功能,和桥接相比,他比较能够避免跟宿主机抢IP地址。桥接模式可是直接跟宿主机“”IP地址了呢。

The host system has a virtual network adapter on the NAT network. This adapter enables the host system and virtual machines to communicate with each other. The NAT device passes network data between one or more virtual machines and the external network, identifies incoming data packets intended for each virtual machine, and sends them to the correct destination.

主机系统在NAT网络中存在一个虚拟网卡。这个适配器使主机系统和虚拟机能够相互通信。NAT设备负责在一台或多台虚拟机与外部网络之间传递网络数据,识别接收到每个虚拟机的数据包,并将这些数据包发送到正确的目的地。

详细举了个例子解释了NAT的工作能力。

提供各种玩法

Features and Limitations of NAT Configurations

NAT is useful when the number of IP addresses is limited or the host system is connected to the network through a non-Ethernet adapter.[了解更多内容]

当IP地址数量有限或主机系统通过非以太网适配器连接到网络时,NAT很有用

这不就正好呼应了我的开头嘛,NAT不跟宿主机抢IP地址,是位好同志!

Change NAT Settings

You can change the gateway IP address, configure port forwarding, and configure advanced networking settings for NAT networks. [了解更多内容]

您可以修改网关地址、配置端口转发、配置NAT网络的高级组网设置。

自己去探索琢磨叭,我也是新手,不了解呢。

Editing the NAT Configuration File

If you are an advanced user, you can edit the NAT configuration file to modify NAT settings. [了解更多内容]

如果您是高级用户,您可以通过编辑NAT配置文件来修改NAT设置。

这不是废话嘛。没试过,你们加油。

Using NAT with NetLogon

If you use NAT networking in a Windows virtual machine running on a Windows host system, you can use NetLogon to log in to a Windows domain from the virtual machine and access file shares that the WINS server knows.[了解更多内容]

如果在Windows主机系统的Windows虚拟机上使用NAT组网,可以通过NetLogon在虚拟机上登录Windows域,访问WINS服务器知道的文件共享。

确实,能够访问windows,而windows无法访问虚拟机,这个我确实实操过,就挺离谱的。

Specifying Connections from Source Ports Below 1024

If a virtual machine that uses NAT attempts to connect to a server that requires the client to use a source port below 1024, the NAT device must forward the request from a port below 1024. For security reasons, some servers accept connections only from source ports below 1024.[了解更多内容]

如果使用NAT的虚拟机尝试连接服务器,而服务器要求客户端使用1024以下的源端口,NAT设备必须转发1024以下的源端口请求。出于安全原因,一些服务器只接受来自源端口小于1024的连接。

长知识了呢!

Host-Only Networking Configuration

原理讲解

When you install Workstation Pro on a Windows or Linux host system, a host-only network (VMnet1) is set up for you. Host-only networking is useful if you need to set up an isolated virtual network. In a host-only network, the virtual machine and the host virtual network adapter are connected to a private Ethernet network. The network is completely contained within the host system.

当您在Windows或Linux主机系统上安装Workstation Pro时,会为您设置一个仅供主机使用的网络(VMnet1)。如果您需要设置一个隔离的虚拟网络,那么仅供主机使用的网络非常有用。在仅供主机使用的网络中,虚拟机和主机虚拟网卡连接到专用以太网网络。网络完全包含在主机系统中。

The network connection between the virtual machine and the host system is provided by a virtual network adapter that is visible on the host operating system. The virtual DHCP server provides IP addresses on the host-only network.

虚拟机和主机系统之间的网络连接是由主机操作系统上可见的虚拟网络适配器提供的。虚拟DHCP服务器在主机专用网络中提供IP地址。

看了一大段官方的开场白

直接上我的结论:

Host-Only网络配置能实现的网络效果描述如下: 单独的一台机器,全封闭的网络,虚拟机唯一能够访问的就是主机。当然多个虚拟机之间也可以互相访问。如果想要虚拟机上外网则需要主机联网并且网络共享。

Host-Only Networking Configuration

Host-Only Networking Configuration

In the default configuration, a virtual machine in a host-only network cannot connect to the Internet. If you install the proper routing or proxy software on the host system, you can establish a connection between the host virtual network adapter and a physical network adapter on the host system to connect the virtual machine to a Token Ring or other non-Ethernet network.

默认情况下,主机专用网络中的虚拟机不能连接到Internet。如果在主机系统上安装了合适的路由或代理软件,可以在主机虚拟网卡和主机系统的物理网卡之间建立连接,将虚拟机连接到令牌环或其他非以太网网络。

这一块触及到我的盲区了,令牌环emmmm,应该是token连接那一块的,希望以后补充。

On a Windows host computer, you can use host-only networking in combination with the Internet Connection Sharing feature in Windows to allow a virtual machine to use the dial-up networking adapter or other connection to the Internet on the host system. See Microsoft documentation for information on configuring Internet Connection Sharing.

在Windows主机计算机上,您可以结合使用Windows中的Internet连接共享特性,以允许虚拟机使用拨号网络适配器或其他连接到主机系统上的Internet。有关配置Internet连接共享的信息,请参阅Microsoft文档。

我直接直译咯!

提供各种玩法

Add a Host-Only Network

When you install Workstation Pro on a Windows or Linux host system, a host-only network (VMnet1) is set up for you. You might want to configure multiple host-only networks to manage network traffic between virtual machines in specific ways. [了解更多内容]

配置多个Host-Only方式的虚拟机实现共通通讯。

Configure Host-Only Networking for an Existing Virtual Machine

You can configure host-only networking for an existing virtual machine. You can connect a virtual network adapter to the default host-only network (VMnet1) or to a custom host-only network. If a virtual machine has two virtual network adapters, you can connect it to two host-only networks. [了解更多内容]

默认的或自定义的网络。一个虚拟机还可以同时连接至两个Host-Only模式的网络呢。新增玩法,GET!

Set Up Routing Between Two Host-Only Networks

If you are setting up a complex test network that uses virtual machines, you might want to have two independent host-only networks with a router between them. [了解更多内容]

一种Host-Only的应用场景

Avoiding IP Packet Leakage in Host-Only Networks

Each host-only network should be confined to the host system on which it is set up. Packets that virtual machines send on this network should not leak out to a physical network attached to the host system. Packet leakage can occur only if a machine actively forwards packets.[了解更多内容]

每个Host-Only网络应该被限制在它所建立的主机系统中。虚拟机在这个网络上发送的数据包不应该泄漏到连接到主机系统的物理网络上。只有当机器主动转发数据包时,才会发生数据包泄漏。

网络被限制住了噻。非允许,不可以向物理机(宿主机)发送数据包(数据包泄露)

Controlling Routing Information for Host-Only Networks on Linux

A host-only network has a network interface associated with it (vmnet1) that is marked up when the host operating system is booted. Routing server processes that operate on the host operating system automatically discover the host-only network and propagate information on how to reach the network, unless you explicitly configure them not to do so.[了解更多内容]

仅供主机使用的网络有一个与之关联的网络接口(vmnet1),该接口在主机操作系统启动时被标记出来。在主机操作系统上运行的路由服务器进程会自动发现主机专用网络,并传播关于如何到达网络的信息,除非您显式地将它们配置为不这样做。

Using DHCP and DDNS with Host-Only Networking on Linux

The virtual DHCP server in Workstation Pro cannot update a DNS server by using a Dynamic Domain Name Service (DDNS). For this reason, you should use DHCP to supply IP addresses as well as other information, such as the identity of a host running a name server and the nearest router or gateway. [了解更多内容]

“工作站专业版”中的虚拟DHCP服务器无法通过DDNS (Dynamic Domain Name Service)更新DNS服务器。由于这个原因,您应该使用DHCP来提供IP地址以及其他信息,例如运行名称服务器的主机的标识以及最近的路由器或网关。

计网基础知识

唉,太晚了不写了,2022年1月12日00:49:04

放个我颇受感慨,且认为还不错的视频链接叭

https://www.bilibili.com/video/BV1xu411f7UW?spm_id_from=333.1007.top_right_bar_window_history.content.click

yyds,通俗易懂呢!