KVM虚拟机的桥接网络

简介: 文章主要介绍了KVM虚拟机的NAT和桥接网络类型的工作原理、配置方法以及如何进行网络模式的切换。

作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.KVM虚拟机的NAT网络类型

1.KVM虚拟机的NAT网络类型工作原理

KVM默认的虚拟机网络是NAT模式,网段为"192.168.122.0/24"。

KVM虚拟机访问外网的流程如上图所示。


实操案例输出:
[root@yinzhengjie-kvm189 ~]# virsh list 
 Id    Name                           State
----------------------------------------------------
 37    yinzhengjie-k8s                running
 42    c1                             running

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# brctl show
bridge name    bridge id        STP enabled    interfaces
virbr0        8000.525400abb36e    yes        virbr0-nic
                            vnet0
                            vnet1
[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# iptables -t nat -L -n
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
RETURN     all  --  192.168.122.0/24     224.0.0.0/24        
RETURN     all  --  192.168.122.0/24     255.255.255.255     
MASQUERADE  tcp  --  192.168.122.0/24    !192.168.122.0/24     masq ports: 1024-65535
MASQUERADE  udp  --  192.168.122.0/24    !192.168.122.0/24     masq ports: 1024-65535
MASQUERADE  all  --  192.168.122.0/24    !192.168.122.0/24    
[root@yinzhengjie-kvm189 ~]#

2.查看默认的网络类型

[root@yinzhengjie-kvm189 ~]# virsh net-list 
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# cat /etc/libvirt/qemu/networks/default.xml 
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
  virsh net-edit default
or other application using the libvirt API.
-->

<network>
  <name>default</name>
  <uuid>87bc919c-68c0-4fd8-8c8b-30ff51794935</uuid>
  <forward mode='nat'/>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='52:54:00:ab:b3:6e'/>
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.2' end='192.168.122.254'/>
    </dhcp>
  </ip>
</network>
[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# ifconfig virbr0
virbr0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:ab:b3:6e  txqueuelen 1000  (Ethernet)
        RX packets 3598  bytes 714219 (697.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2872  bytes 180487 (176.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@yinzhengjie-kvm189 ~]#

二.KVM虚拟机的桥接网络类型

1.KVM虚拟机的桥接网络类型工作原理

如上图所示,KVM虚拟机的桥接网卡会接管宿主机的网卡IP地址。此时宿主机的网卡充当了交换机功能,数据通过br0网卡访问外网。

2.创建桥接网卡

    1.关掉NetworkMnagaer,firewalld服务,否则可能会出现断网的情况
[root@yinzhengjie-kvm189 ~]# systemctl disable --now NetworkManager firewalld

    2.禁用selinux
[root@yinzhengjie-kvm189 ~]# sed -i '/SELINUX=/s#enforcing#disabled#g' /etc/selinux/config 
[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# setenforce 0
[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# getenforce 
Permissive
[root@yinzhengjie-kvm189 ~]# 

    3.修改网卡的配置文件,配置好静态IP
[root@yinzhengjie-kvm189 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=10.0.0.189
NETMASK=255.255.255.0
GATEWAY=10.0.0.254
DNS1=223.5.5.5
DNS2=223.6.6.6
[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# systemctl restart network
[root@yinzhengjie-kvm189 ~]# 

    4.创建桥接网卡
[root@yinzhengjie-kvm189 ~]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.189  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::20c:29ff:feaa:4063  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:aa:40:63  txqueuelen 1000  (Ethernet)
        RX packets 831  bytes 65414 (63.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 526  bytes 70060 (68.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 48  bytes 4080 (3.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 48  bytes 4080 (3.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:ab:b3:6e  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh iface-bridge eth0 br0  # 创建桥接前观察eth0的网卡地址。
Created bridge br0 with attached device eth0
Bridge interface br0 started

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# brctl show
bridge name    bridge id        STP enabled    interfaces
br0            8000.000c29aa4063    yes        eth0
virbr0        8000.525400abb36e    yes        virbr0-nic
[root@yinzhengjie-kvm189 ~]# 


    5.桥接成功后多出了br0网卡且eth0网卡的IP地址转移到br0上去了
[root@yinzhengjie-kvm189 ~]# ifconfig 
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.189  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::20c:29ff:feaa:4063  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:aa:40:63  txqueuelen 1000  (Ethernet)
        RX packets 10  bytes 596 (596.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 33  bytes 4837 (4.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:aa:40:63  txqueuelen 1000  (Ethernet)
        RX packets 859  bytes 67412 (65.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 577  bytes 78059 (76.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 48  bytes 4080 (3.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 48  bytes 4080 (3.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:ab:b3:6e  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@yinzhengjie-kvm189 ~]#

3.新建虚拟机使用桥接模式

    1.使用桥接模式
virt-install --virt-type kvm \
    --os-type=linux \
    --os-variant rhel7 \
    --name yinzhengjie-kvm02 \
    --memory 2048 \
    --vcpus 2 \
    --disk /opt/yinzhengjie-kvm02.qcow2,format=qcow2,size=30 \
    --cdrom /opt/CentOS-7-x86_64-DVD-2009.iso \
    --network  bridge=br0 \
    --graphics vnc,listen=0.0.0.0 \
    --noautoconsole


    2.使用NAT模式
virt-install --virt-type kvm \
    --os-type=linux \
    --os-variant rhel7 \
    --name yinzhengjie-kvm03 \
    --memory 2048 \
    --vcpus 2 \
    --disk /opt/yinzhengjie-kvm03.qcow2,format=qcow2,size=30 \
    --cdrom /opt/CentOS-7-x86_64-DVD-2009.iso \
    --network  network=default \
    --graphics vnc,listen=0.0.0.0 \
    --noautoconsole


温馨提示:
    使用"--network bridge=br0"表示使用br0进行桥接网络配置。

4.将已有的虚拟网络修改为桥接模式

    1.关闭虚拟机,如果虚拟机开机修改网络模式可能不会生效
[root@yinzhengjie-kvm189 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     yinzhengjie-kvm02              running
 2     yinzhengjie-kvm01              running

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh shutdown yinzhengjie-kvm01 
Domain yinzhengjie-kvm01 is being shutdown

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     yinzhengjie-kvm02              running
 -     yinzhengjie-kvm01              shut off

[root@yinzhengjie-kvm189 ~]# 


    2.修改虚拟机的配置文件
[root@yinzhengjie-kvm189 ~]# virsh edit yinzhengjie-kvm01 
<domain type='kvm'>
...(大概在69行左右,进行修改,原有的配置如下)
     69     <interface type='network'>
     70       <mac address='52:54:00:d1:32:de'/>
     71       <source network='default'/>
     72       <model type='virtio'/>
     73       <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
     74     </interface>
...(我们对接口进行修改后的内容如下,其实仅需要修改69和71这两行即可!!!)
     69     <interface type='bridge'>
     70       <mac address='52:54:00:d1:32:de'/>
     71       <source bridge='br0'/>
     72       <model type='virtio'/>
     73       <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
     74     </interface>


    3.启动虚拟机并验证是否生效
[root@yinzhengjie-kvm189 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     yinzhengjie-kvm02              running
 -     yinzhengjie-kvm01              shut off

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh start yinzhengjie-kvm01 
Domain yinzhengjie-kvm01 started

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01 
Connected to domain yinzhengjie-kvm01
Escape character is ^]

CentOS Linux 7 (Core)
Kernel 3.10.0-1160.el7.x86_64 on an x86_64

yinzhengjie-kvm01 login: root
Password: 
Last login: Tue Apr  9 01:48:36 on tty1
[root@yinzhengjie-kvm01 ~]# 
[root@yinzhengjie-kvm01 ~]# 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:d1:32:de brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.134/24 brd 10.0.0.255 scope global noprefixroute dynamic eth0
       valid_lft 1677sec preferred_lft 1677sec
    inet6 fe80::ae66:b680:585a:e5c2/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@yinzhengjie-kvm01 ~]#

5.取消桥接模式

[root@yinzhengjie-kvm189 ~]# ifconfig 
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.189  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::20c:29ff:feaa:4063  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:aa:40:63  txqueuelen 1000  (Ethernet)
        RX packets 2437  bytes 142193 (138.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1673  bytes 177747 (173.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:aa:40:63  txqueuelen 1000  (Ethernet)
        RX packets 3766  bytes 289851 (283.0 KiB)
        RX errors 0  dropped 1  overruns 0  frame 0
        TX packets 3140  bytes 339865 (331.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 48  bytes 4080 (3.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 48  bytes 4080 (3.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:ab:b3:6e  txqueuelen 1000  (Ethernet)
        RX packets 18  bytes 1350 (1.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5  bytes 607 (607.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh iface-unbridge br0
Device eth0 un-attached from bridge br0
Interface eth0 started

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.189  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::20c:29ff:feaa:4063  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:aa:40:63  txqueuelen 1000  (Ethernet)
        RX packets 3916  bytes 302829 (295.7 KiB)
        RX errors 0  dropped 1  overruns 0  frame 0
        TX packets 3253  bytes 354609 (346.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 48  bytes 4080 (3.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 48  bytes 4080 (3.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:ab:b3:6e  txqueuelen 1000  (Ethernet)
        RX packets 18  bytes 1350 (1.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5  bytes 607 (607.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@yinzhengjie-kvm189 ~]#
目录
相关文章
|
2月前
|
Docker 容器
docker中默认桥接网络
【10月更文挑战第7天】
142 62
|
2月前
|
Ubuntu Linux 虚拟化
Linux虚拟机网络配置
【10月更文挑战第25天】在 Linux 虚拟机中,网络配置是实现虚拟机与外部网络通信的关键步骤。本文介绍了四种常见的网络配置方式:桥接模式、NAT 模式、仅主机模式和自定义网络模式,每种模式都详细说明了其原理和配置步骤。通过这些配置,用户可以根据实际需求选择合适的网络模式,确保虚拟机能够顺利地进行网络通信。
107 1
|
2月前
|
虚拟化 网络虚拟化 网络架构
虚拟机 VMware Workstation 16 PRO 的网络配置
虚拟机 VMware Workstation 16 PRO 的网络配置
91 2
|
3月前
|
KVM 虚拟化
KVM虚拟机的热迁移
这篇文章详细介绍了KVM虚拟机的热迁移过程,包括临时迁移和永久迁移的步骤,以及可能遇到的故障和解决方案。
138 1
KVM虚拟机的热迁移
|
3月前
|
KVM 虚拟化 数据安全/隐私保护
KVM虚拟机安装实战
本文讲述了如何创建并使用VNC连接KVM虚拟机的详细教程,包括安装图解和命令行参数说明。
131 8
|
3月前
|
KVM 虚拟化
KVM虚拟机的冷迁移
这篇文章详细描述了KVM虚拟机的冷迁移过程,包括无依赖环境迁移、有链接克隆虚拟机迁移、多块磁盘迁移的案例,以及可能遇到的错误和解决方案。
120 3
|
4天前
|
SQL 安全 网络安全
网络安全与信息安全:知识分享####
【10月更文挑战第21天】 随着数字化时代的快速发展,网络安全和信息安全已成为个人和企业不可忽视的关键问题。本文将探讨网络安全漏洞、加密技术以及安全意识的重要性,并提供一些实用的建议,帮助读者提高自身的网络安全防护能力。 ####
41 17
|
14天前
|
存储 SQL 安全
网络安全与信息安全:关于网络安全漏洞、加密技术、安全意识等方面的知识分享
随着互联网的普及,网络安全问题日益突出。本文将介绍网络安全的重要性,分析常见的网络安全漏洞及其危害,探讨加密技术在保障网络安全中的作用,并强调提高安全意识的必要性。通过本文的学习,读者将了解网络安全的基本概念和应对策略,提升个人和组织的网络安全防护能力。
|
15天前
|
SQL 安全 网络安全
网络安全与信息安全:关于网络安全漏洞、加密技术、安全意识等方面的知识分享
随着互联网的普及,网络安全问题日益突出。本文将从网络安全漏洞、加密技术和安全意识三个方面进行探讨,旨在提高读者对网络安全的认识和防范能力。通过分析常见的网络安全漏洞,介绍加密技术的基本原理和应用,以及强调安全意识的重要性,帮助读者更好地保护自己的网络信息安全。
39 10
|
17天前
|
SQL 安全 网络安全
网络安全与信息安全:关于网络安全漏洞、加密技术、安全意识等方面的知识分享
在数字化时代,网络安全和信息安全已成为我们生活中不可或缺的一部分。本文将介绍网络安全漏洞、加密技术和安全意识等方面的内容,并提供一些实用的代码示例。通过阅读本文,您将了解到如何保护自己的网络安全,以及如何提高自己的信息安全意识。
44 10