本文已参与「新人创作礼」活动,一起开启掘金创作之路。
1、VMware提供3种网络设置
1)桥接模式
虚拟机直接接连接外部物理网络的模式,主机起到了桥接网络的作用,这种模式下,虚拟机可以直接访问外部网络,并且对外部网络是可见的。
2)NAT模式
虚拟机和主机构建一个专用网络,并通过虚拟机网络地址转换(NAT)设备对 IP 进行转换,虚拟机通过共享主机 IP 可以访问外部网络,但外部网络无法访问虚拟机。
3)仅主机模式
虚拟机只与主机共享一个专用网络,与外部网络无法通信。
2、虚拟机网段和网关
1)查看虚拟网络编辑器
2)虚拟机网络设置
如下图,需要选中列表中的VMnet8
3)DHCP设置
如下图,需要选中列表中的VMnet8
4)DHCP编辑保存
5)查看网关
3、配置网络ip地址
3.1、ifconfig查看网络信息
[root@test1 ~]# ifconfig
ns33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.200.135 netmask 255.255.255.0 broadcast 192.168.200.255
inet6 fe80::b84b:79f6:abb4:afa9 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:32:65:be txqueuelen 1000 (Ethernet)
RX packets 462144 bytes 639516380 (609.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 175430 bytes 14334643 (13.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
3.2、ping测试主机之间的网络
1)基本语法
用来测试当前服务器是否可以连接目标主机
ping 目标主机
2)案例
测试当前服务器是否可以连接百度,效果如下,表示正常
[root@test1 ~]# ping baidu.com
PING baidu.com (220.181.38.148) 56(84) bytes of data.
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=128 time=32.4 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=128 time=33.7 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=3 ttl=128 time=31.2 ms
3.3、修改IP地址为固定的
1)修改IP配置文件
修改/etc/sysconfig/network-scripts/ifcfg-ens33
文件内容,以下标红的项必须修改, 有值的按照下面的值修改, 没有该项的要增加
本机修改之后
2)重启网络
使用service network restart
命令重启网络,效果如下
[root@test1 network-scripts]# service network restart
Restarting network (via systemctl): [ 确定 ]
3.4、修改IP地址后可能遇到的问题
(1)物理机器能ping通虚拟机,但是虚拟机ping不通物理机,一般都是因为物理机的防火墙问题,把物理机防火墙关闭就行
(2)虚拟机能ping通物理机,但是虚拟机ping不通外网,一般都是因为DNS的设置有问题
(3)虚拟机ping www.baidu.com
显示域名未知等信息,一般查看GATEWAY和DNS设置是否正确
(4)如果以上全部设置完还是不行,需要关闭NetworkManager服务
- 关闭:
systemctl stop NetworkManager
- 禁用:
systemctl disable NetworkManager
(5)如果检查发现systemctl status network 有问题,需要检查ifcgf-ens33
4、配置主机名
4.1、hostname查案当前服务器主机名称
[root@test1 ~]# hostnametest1
4.2、修改主机名称
方式1:修改/etc/hostname文件,需重启
如果感觉主机名不合适,我们可以进行修改,通过编辑/etc/hostname
文件进行修改,修改完毕之后重启生效。
[root@test1 ~]# vi /etc/hostname
方式2:hostnamectl命令修改,无需重启
[root@testx ~]# cat /etc/hostname
testx
[root@testx ~]# hostnamectl set-hostname test1
[root@testx ~]# hostname
test1
[root@testx ~]# cat /etc/hostname
test1
4.3、修改linux的主机映射文件(hosts文件)
由于虚拟机比较多,配置时通常会采用主机名的方式配置,比较简单方便,不用刻意记ip地址。
编辑/etc/hosts
格式:ip 名称1 名称2 名称n
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.0.103 test103
这样我们就可以通过test103
来访问192.168.0.103
这台机器了,比如ping test103
效果如下,自动根据名称在/etc/hosts
文件中找到对应的ip
[root@testx ~]# ping test103
PING test103 (192.168.0.103) 56(84) bytes of data.
64 bytes from test103 (192.168.0.103): icmp_seq=1 ttl=128 time=0.646 ms
64 bytes from test103 (192.168.0.103): icmp_seq=2 ttl=128 time=0.499 ms
64 bytes from test103 (192.168.0.103): icmp_seq=3 ttl=128 time=1.89 ms
window上也也有这种文件,对应的位置如下
C:\Windows\System32\drivers\etc\hosts