Home  >  Article  >  System Tutorial  >  Comprehensive analysis of Linux IP configuration: three network connection modes of virtual machines and precautions

Comprehensive analysis of Linux IP configuration: three network connection modes of virtual machines and precautions

王林
王林Original
2024-07-11 22:07:55556browse

全面解析 Linux IP 配置:虚拟机三种网络连接模式及注意事项

Preface

Purpose of the article: brief, clear, and comprehensive introduction to Linux IP configuration

Note: The article is long. If you are interested in a certain linkLinux configuration ip address eth1, you can click on the table of contents and go straight to the topic.

1. Three network connection modes of virtual machines (common sense)

linux配置ip地址 eth1_配置地址源_配置地址最新

VMnet1 and VMnet8 are two network cards that are virtualized after installing the virtual machine VMware.

VMnet1 is the host network card, used to connect to the network in host mode;

VMnet8 is a NAT network card, used to connect to the Internet in NAT form, but their IP addresses are randomly generated.

配置地址源_配置地址最新_linux配置ip地址 eth1

1.Bridged bridge mode

Just like an independent "physical machine" that exists alone in a LAN, it can access any machine in the same LAN, or it can access the intranet through a LAN segment or route alone.

You can automatically configure IP, network segment, network segment, DNS, etc.; you can also set the IP address and DNS to "automatically obtain"

Explanation: The network connection is almost equivalent to the chemical machine.

2.NAT mode (Network Address Translation)

Let the virtual machine use the NAT (Network Address Translation) function to access the network segment through the network where the host machine is located.

Use the VMnet8 network card of the math host to access the Internet. (This knowledge point will be used when configuring static IP)

Explanation: The host is equivalent to a router with the DHCP function enabled, and the virtual machine needs to access the intranet through this route.

Note: Generally, the IP and DNS in this mode are set to “obtain automatically”.

3.Host-Only

The TCP/IP configuration information of the virtual machine (such as network segment address, DNS server, etc.) is dynamically allocated by the DHCP server of the VMnet1 (host-only) virtual network.

Features: The virtual machine can only communicate with the virtual machine and the host. The virtual machine is isolated from the external network and cannot access the internal network.

2. The difference between the three modes

Bridge Mode

Features

The virtual machine and the host are both in a gateway

It is equivalent to a virtual host and can be used as a host

Use

Can communicate with other hosts on the local area network where the host is located

缺点

同一个网关中,IP地址有限(0~255之内),可能导致ip冲突;虚拟机可能会占用了人家主机的IP

NAT模式

特征

接入网路十分简单,和宿主机共享同一个内网,还能通过VMnet8与宿主机通信(访问网路时,相当于把主机当成了一个路由器)

用处

虚拟机不占用window笔记本的IP,不会冲突;

缺点

未能和宿主机的所在局域网的其他主机通信

仅主机模式

特征

仅能通过VMnet1网卡与宿主机进行通信

仅主机模式和NAT模式差不多,区别就在于少了虚拟NAT网段。

缺点

只有IP地址的分配,不会进行NAT地址转换。因而其难以访问内网(某种程度上,这也是一个优点)。

一、临时更改IP地址

#ifconfig 查看ip和网卡名称{虚拟机(第一个)网卡大部分名称是eno16777736}
#ifconfig eno16777736 192.168.1.101给指定网卡设定IP

二、静态设置IP(nmtui图形界面)2.1、准备工作

#ls /usr/lib/systemd/system/NetworkManager.service 网络适配器服务文件位置
#systemctl status NetWorkManager查看网络适配器是否在运行,运行状态下,才可以使用nmtui命令
注:如果没有开启,需重启一下网络服务
#systemctl restart NetWorkManager 

配置地址源_配置地址最新_linux配置ip地址 eth1

2.2、进入图形界面

#nmtui 打开图形界面,修改IP

配置地址最新_配置地址源_linux配置ip地址 eth1

2.3、图形界面操作步骤

依次选择:
Edit a connection--->
Ethernet-eno16777736(选择网卡)-->Edit(界面右侧选项)--->
IPv4 CONFIGURATION (注:这里默认可能是Automatic,需要调整)--->)--->
Addresses 在此配入自己的IP、网关、DNS,例如:192.168.1.1--->
最后选择底部的依次保存并退出图形界面

配置地址最新_配置地址源_linux配置ip地址 eth1

配置地址最新_配置地址源_linux配置ip地址 eth1

配置地址源_linux配置ip地址 eth1_配置地址最新

2.4、最后,重启网路服务

#systemctl restart NetworkManager
#systemctl restart network 提示:CentOS8不支持该写法,应该用 nmcli c reload
#ifconfig

三、静态IP设置(更改配置文件)3.1、查看虚拟机(VMnet8网卡)网段

打开虚拟机,在菜单栏依次点击:

编辑--->虚拟网路编辑器--->NAT模式(窗口上方)--->NAT设置(按键在界面中部两侧)--->网段IP(弹出界面,可以见到虚拟网关)。

设置的IP要和虚拟机网关的前三个段位一致。

例如:虚拟机网关是192.168.31.2(这个就是路由的GATEWAY网段,静态IP地址须要配这个),自己配置的静态IP就必须是192.168.31.开头。

NAT模式的GATEWAY请查看右图。

linux配置ip地址 eth1_配置地址源_配置地址最新

3.2、配置静态IP

提示,本方式虚拟机的网路适配器使用NAT形式(和主机共享网路)就可以,其实你也可以选择Bridge模式

配置地址最新_linux配置ip地址 eth1_配置地址源

#cd /etc/sysconfig/network-scripts/进入网卡配置文件所在目录
#ls查看网卡名称(#ifconfig 也可以查看网卡名称)
#vim /etc/sysconfig/network_scripts/ifcfg-eno16777736编辑网卡配置文件(有虚拟网卡是eth0、ens33)

BOOTPROTO=static  此处务必设置为static或者none
ONBOOT="yes"         开机启动网卡设置
IPADDR=192.168.31.3  IP地址的前三段,必须和网关的前三段相同
NETMASK=255.255.255.0
GATEWAY=192.168.31.2  此网关就是上个步骤查看的VMnet8网卡的网关
DNS1=8.8.8.8 #有时候,单独配8888不管用
DNS2=223.5.5.5 #配置阿里的域名解析,还不行,尝试一下223.6.6.6

注:其他参数可以保持不变,假如配置静态IP后,不能上网,通常是DNS问题(调整DNS)。仍然不能上网可参考步骤六。

3.3、重启网路服务

service network restart#RHEL6和RHEL7通用命令
ifconfig

#CentOS8高版本写法
systemctl restart NetworkManager
systemctl restart network     #提示:CentOS8不支持该写法,应该用 nmcli c reload 或者 nmcli connection reload 
ifconfig #或者nmcli device show

四、动态设置IP(更改配置文件)

后面打算工作,参考静态IP配置(不再累述)

4.1、核心配置参数

BOOTPROTO='dhcp'修改为BOOTPROTO='dhcp' 
ONBOOT='yes' 修改为 ONBOOT="yes"

4.2、注释掉其他和IP相关参数

例如:IPADDR、GATE_WAY、PREFIX、DNS等

4.3、保存退出,重启network服务

systemctl restart NetworkManager
systemctl restart network   #注:该写法在CentOS8中不支持,应该用 nmcli c reload
service  network restart  #注:RHEL6.5的重启方式
ping www.baidu.com

注:如各类方式尝试后,仍然未能ping通,网路联接请采用NAT模式后,重启网卡服务。

linux配置ip地址 eth1_配置地址最新_配置地址源

仍然不能ping通,继续看步骤六。

五、网卡参数说明5.1、网卡参数

1.DEVICE

DEVICE=eth0网卡的名子

2.HWADDR

HWADDR=00:0c:29:90:89:d9HWADDRHardWareAddress硬件MAC地址

3.TYPE

TYPE=Ethernet网路类型以太网

4.NM_CONTROLLED

NM_CONTROLLED=yes是否受network程序管理

5.BOOTPROTO

网络配置,它有三个可选参数none,static,dhcp,需根据自身实际情况,选其一。
BOOTPROTO=static 静态IP(需要在当前配置文件中手动指定IP、网关和DNS)
BOOTPROTO=dhcp 动态IP(不需要手动指定IP)
BOOTPROTO=none 引导时不使用协议

6.ONBOOT

ONBOOT=yes在开机或重启网卡的时侯是否启动网卡

7.DEFROUTE

DEFROUTE="yes"是否设置默认路由

注:系统路由默认选择的是eth0网卡,假如实际使用的是eth1网卡,须要在eth1的网卡配置内容中配置为yes

配置地址最新_配置地址源_linux配置ip地址 eth1

8.UUID

虚拟主机嵌入式linux培训,会给每位硬件一个标示

9.IPADDR

IPADDR=192.168.31.1静态配置IP地址标示主机身分

10.GATEWAY

GATEWAY=192.168.31.2一个网路到另一个网路的必经之路,整个大厦的房门(虚拟机中所有机器的房门)

11.DNS

DNS1=8.8.8.8阿里的域名解析器:223.5.5.5223.6.6.6

可以添加两个DNS服务器DNS1和DNS2。

注:网卡配置文件的DNS优先于/etc/resolv.conf

12.PEERDNS

PEERDNS=[yes|no]是否将DNS信息写入/etc/resolv.conf(覆盖)

13.PREFIX网段宽度

PREFIX=24定义网路中可以有多少主机屋子上面可以有多少人(默认值24)

14.NETMASK

NETMASK=255.255.255.0子网网段决定这个局域网中最多有多少台机器

15.USERCTL

USERCTL=no普通用户是否能控制网卡

5.2、网卡配置模板

倘若有多块网卡,本配置文件内容可以配置多份。

TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eth0
UUID=c5300c89-c3e4-4a3a-8344-5194975eacfb
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.1.170
PREFIX=24
#GATEWAY=192.168.1.79
IPADDR1=192.168.0.170
DNS1=114.114.114.114
DNS2=202.96.128.166

六、本地笔记本网路配置6.1、Win+i调出系统设置(以Win10为例)1.打开windows系统设置

配置地址最新_配置地址源_linux配置ip地址 eth1

2.打开网路适配器

点击界面右边的WALN,依次点击右下发的”相关设置“下面的”更改适配器设置“

linux配置ip地址 eth1_配置地址最新_配置地址源

3.虚拟机网路适配器,服务状态都调整为“已启用”

linux配置ip地址 eth1_配置地址最新_配置地址源

4.无线/有线网路设置为共享

在网路适配器中,

假如笔记本联接的是无线网路,用键盘右键点击WLAN;

假如笔记本联接的是有线网路,用键盘右键点击“以太网”;

之后在弹出窗口,底部选择共享

配置地址最新_linux配置ip地址 eth1_配置地址源

之后,勾选上上下两个对钩,并选择“家庭网路联接”,选VMnet1就可以。

配置地址最新_linux配置ip地址 eth1_配置地址源

6.2、查看虚机(VMnet1和VMnet8)IP1.键盘右键查看状态

配置地址最新_linux配置ip地址 eth1_配置地址源

2.点击详尽信息,并查看虚拟机适配器(VMwareNetworkAdapterVMnet8)IP

假如是桥接模式,IP地址配置时linux配置ip地址 eth1,勿必和VMnet1IP地址的前三段保持一致。

假如是NAT模式,IP地址配置时,勿必和VMnet8IP地址的前三段保持一致。

例如都是:192.168.31,开头的

假如配置为动态(BOOTPROTO='dhcp')获取IP,这么

#ifconfig 看到的虚拟机动态IP,开头也是192.168.31,开头的

配置地址源_配置地址最新_linux配置ip地址 eth1

配置地址源_配置地址最新_linux配置ip地址 eth1

下边是NAT模式

配置地址最新_linux配置ip地址 eth1_配置地址源

2.在虚拟机中Linux系统,网路联接是NAT模式,IP配置的时侯,要和这儿(VMwareNetworkAdapterVMnet8)的网路是一个网关。

linux配置ip地址 eth1_配置地址最新_配置地址源

6.3、services.msc启用两个虚拟机服务1.打开服务设置

Win+R调出运行,输入services.msc

linux配置ip地址 eth1_配置地址最新_配置地址源

2.虚拟机的2个服务都设置为启动

VMwareDHCPService

VMwareNATService

注:这个两个服务,假如常常用,建议设置为开启手动启动。

配置地址源_配置地址最新_linux配置ip地址 eth1

配置地址最新_配置地址源_linux配置ip地址 eth1

6.4、虚拟机桥接模式

linux配置ip地址 eth1_配置地址源_配置地址最新

在虚拟机中,带水晶头的有线网路,使用桥接模式:注意下边的对钩也勾选上。

配置地址最新_linux配置ip地址 eth1_配置地址源

假如经过各类尝试,网路不能ping通,把桥接模式,变更为自定义模式

配置地址最新_linux配置ip地址 eth1_配置地址源

七、关于关掉防火墙、关闭SELINUX安全策略

以上步骤,都操作后,仍然不能ping通内网,此时看一下。

配置地址最新_配置地址源_linux配置ip地址 eth1

防火墙是否关掉、selinux安全策略是否关掉。

7.1、防火墙的关掉1.查看防火墙状态

systemctl status firewalld
#或者
systemctl status firewalld.service

linux配置ip地址 eth1_配置地址最新_配置地址源

2.临时关掉防火墙

systemctl stop firewalld

3.严禁防火墙开机自启

systemctl disable firewalld

配置地址最新_配置地址源_linux配置ip地址 eth1

7.2、安全策略的关掉

SELinux主要作用就是最大限度地减少系统中服务进程可访问的资源(最小权限原则)。

假如开启会造成好多端口不能访问。

1.查看seLinux的开启状态

getenforce
enforcing #强制执行 (该显示说明安全策略是开启状态)

2.临时关掉seLinux

setenforce 0 #临时关闭
getenforce 查看SElinux的状态
permissive #放任的;许可的 

3.通过配置文件永久关掉selinux

vim /etc/sysconfig/selinux 

linux配置ip地址 eth1_配置地址源_配置地址最新

在配置文件中,更改配置为:

SELINUX=disabledlinux怎么读,就可以关掉安全策略了。

注:该配置只有重启后才能生效。

#reboot重启服务器

八、开启手动联接网路,解决unknowhost

#ifconfig

Linkencap:LocalLoopback

inetaddr:127.0.0.1Mask:255.0.0.0

#ping

ping:unknownhost

#ping

ping:unknownhost

假如历经以上步骤,仍然未能上网,这个步骤是最后的大招!!

尾言

至此,花费了大量的时间,精力,希望本文对你有所帮助,假如认为还不错,赶紧点赞收藏吧!

The above is the detailed content of Comprehensive analysis of Linux IP configuration: three network connection modes of virtual machines and precautions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn