为使用NAT网络的KVM虚机配置静态IP

系统rhel 8.5,以默认的default网络为例:

  1. 查看default网络

    virsh net-dumpxml default
    <network connections='1'>
      <name>default</name>
      <uuid>50b4c6fc-9bd0-45e9-94dd-1d9e7279199c</uuid>
      <forward mode='nat'>
        <nat>
          <port start='1024' end='65535'/>
        </nat>
      </forward>
      <bridge name='virbr0' stp='on' delay='0'/>
      <mac address='52:54:00:70:e0:ca'/>
      <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>
    
  2. 创建虚机(使用default网络)

    virt-install --autostart --name cluster1-master1 --memory 32768 --vcpus 8 --import --os-variant rhel8.5 --disk path=/var/cluster1/master1/os.qcow2,bus=virtio --network network=default,model=virtio --noautoconsole
    
  3. 查看虚机状态

    virsh list --all
     Id   Name               State
    ----------------------------------
     1    cluster1-master1   running
    
  4. 获取虚机NAT网卡的MAC地址

    virsh dumpxml cluster1-master1
    ……
    <interface type='network'>
      <mac address='52:54:00:03:8b:c4'/>
      <source network='default' portid='7dad577d-f3c0-475c-bf20-12a1504e4e13' bridge='virbr0'/>
    ……
    
  5. 查看虚机自动获取的IP地址

    arp -a | grep 52:54:00:03:8b:c4
    ? (192.168.122.2) at 52:54:00:03:8b:c4 [ether] on virbr0
    
  6. 登录到虚机

    ssh [email protected]
    
  7. 在宿主机上更新dhcp资源池的范围

    virsh net-update default delete ip-dhcp-range "" --live --config
    virsh net-update default add ip-dhcp-range "" --live --config
    
  8. 在宿主机上配置虚机的静态IP

    virsh net-update default add-last ip-dhcp-host "" --live --config
    

    其中,
    host mac:第四步获取的虚机NAT网卡的MAC地址
    name:虚机名
    ip:要配置的静态IP

  9. 确认配置更新

    virsh net-dumpxml default
    <network>
      <name>default</name>
      <uuid>50b4c6fc-9bd0-45e9-94dd-1d9e7279199c</uuid>
      <forward mode='nat'>
        <nat>
          <port start='1024' end='65535'/>
        </nat>
      </forward>
      <bridge name='virbr0' stp='on' delay='0'/>
      <mac address='52:54:00:70:e0:ca'/>
      <ip address='192.168.122.1' netmask='255.255.255.0'>
        <dhcp>
          <range start='192.168.122.100' end='192.168.122.254'/>
          <host mac='52:54:00:03:8b:c4' name='cluster1-master1' ip='192.168.122.10'/>
        </dhcp>
      </ip>
    </network>
    
  10. 在虚机上重新发送DHCP请求

    dhclient -r && dhclient
    
  11. 宿主机上查看arp

    arp -a | grep 52:54:00:03:8b:c4
    ? (192.168.122.10) at 52:54:00:03:8b:c4 [ether] on virbr0
    ? (192.168.122.2) at 52:54:00:03:8b:c4 [ether] on virbr0
    
  12. 确认配置成功

    ping 192.168.122.10
    PING 192.168.122.10 (192.168.122.10) 56(84) bytes of data.
    64 bytes from 192.168.122.10: icmp_seq=1 ttl=64 time=0.291 ms
    64 bytes from 192.168.122.10: icmp_seq=2 ttl=64 time=0.504 ms
    ^C
    --- 192.168.122.10 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 1026ms
    

参考资料:
https://tqdev.com/2020-kvm-network-static-ip-addresses

你可能感兴趣的:(linux)