一、虚拟机网络模式介绍
1.Net模式
默认情况下,虚拟网络交换机以NAT模式运行。他们使用IP伪装而不是SNAT(Source-NAT)或DNAT(Destination-NAT)。IP伪装使得连接的guest虚拟机可以使用主机物理机器IP地址与任何外部网络进行通信。默认情况下,虚拟网络交换机在NAT模式下运行时,放置在主机物理机外部的计算机无法与其中的guest虚拟机进行通信
2.桥接模式
在使用桥接模式时,所有虚拟机都好像与主机物理机器在同一个子网内。同一物理网络中的所有其他物理机器都知道这些虚拟机,并可以访问这些虚拟机。桥接操作在OSI网络模型的第2层。
在中间件(hypervisor )中可以使用多个物理接口,通过绑定把他们连接在一起,然后把它添加的网桥,同时虚拟机也被添加到这个网桥,绑定操作有多种模式,只有少数这些模式可以与虚拟客机正在使用的桥接器配合使用。
二、桥接网络搭建实战
1.创建一个桥接网络
[root@kvm01-server /opt]# virsh iface-bridge eth0 br0 Created bridge br0 with attached device eth0 Bridge interface br0 started [root@kvm01-server /opt]# ip a 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP group default qlen 1000 link/ether 00:0c:29:0e:90:4d brd ff:ff:ff:ff:ff:ff 20: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000 link/ether 00:0c:29:0e:90:4d brd ff:ff:ff:ff:ff:ff inet 10.0.0.11/24 brd 10.0.0.255 scope global br0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe0e:904d/64 scope link valid_lft forever preferred_lft forever [root@kvm01-server /opt]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 ONBOOT=yes BRIDGE="br0" [root@kvm01-server /opt]# cat /etc/sysconfig/network-scripts/ifcfg-br0 DEVICE="br0" ONBOOT="yes" TYPE="Bridge" BOOTPROTO="none" IPADDR="10.0.0.11" NETMASK="255.255.255.0" GATEWAY="10.0.0.2" STP="on" DELAY="0"
2.修改虚拟机的配置文件
[root@kvm01-server /opt]# virsh edit web01 74 <interface type='bridge'> #修改网络类型 75 <mac address='52:54:00:6b:e9:89'/> 76 <source bridge='br0'/> #修改网卡名称
3.启动并连接虚拟机
[root@kvm01-server /opt]# virsh start web01 Domain web01 started [root@kvm01-server /opt]# virsh console web01 Connected to domain web01 Escape character is ^]
4.虚拟机操作测试
#修改网络信息 [root@kvm ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 TYPE=Ethernet BOOTPROTO=none NAME=eth0 DEVICE=eth0 ONBOOT=yes IPADDR=10.0.0.101 PREFIX=24 GATEWAY=10.0.0.2 DNS1=10.0.0.2 #重启网络 [root@kvm ~]# systemctl restart network [root@kvm ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 52:54:00:6b:e9:89 brd ff:ff:ff:ff:ff:ff inet 10.0.0.101/24 brd 10.0.0.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet6 fe80::5054:ff:fe6b:e989/64 scope link valid_lft forever preferred_lft forever [root@kvm ~]# cat /etc/resolv.conf # Generated by NetworkManager nameserver 10.0.0.2 [root@kvm ~]# 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=27.3 ms 64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=128 time=28.3 ms 64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=4 ttl=128 time=27.3 ms 64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=5 ttl=128 time=31.1 ms --- baidu.com ping statistics --- 5 packets transmitted, 4 received, 20% packet loss, time 13046ms rtt min/avg/max/mdev = 27.346/28.536/31.141/1.562 ms #关闭NetworkManager [root@kvm ~]# systemctl stop NetworkManager [root@kvm ~]# systemctl disable NetworkManager #远程连接虚拟机 [C:\~]$ ssh root@10.0.0.101 Connecting to 10.0.0.101:22... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. Last login: Thu Jul 30 16:50:01 2020 [root@kvm ~]#