Score:0

How to use specific host ip address on kvm?

br flag

Have a dedicated Ubuntu Server host with a single network interface but with two ip address:

root@server:~# cat /etc/netplan/50-cloud-init.yaml 
network:
    version: 2
    ethernets:
        eno3:
            dhcp4: true
            match:
                macaddress: 00:00:00:00:00:00
            set-name: eno3
            addresses: [IP-A/24, IP-B/32]

When IP-A is the native ip address assigned to the server and IP-B is an additional ip address that I have contracted. When configuring using netplan apply everything works very well, the WEB server exits through the IP address IP-A and from curl I can see how it is possible to access the internet from IP-B:

wget -qO- http://checkip.dyndns.com/ --bind-address IP-A
... Current IP Address: IP-A
wget -qO- http://checkip.dyndns.com/ --bind-address IP-B
... Current IP Address: IP-B

The problem is that I also have a virtual machine with kvm (virsh), how can I make the virtual machine go out to the internet using IP-B and not IP-A as it does by default?

My network interfaces:

root@yhojann:~# ip addr
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
2: eno3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 11:11:11:11:11:11 brd ff:ff:ff:ff:ff:ff
    inet IP-A/24 brd x.x.x.255 scope global dynamic eno3
       valid_lft 63801sec preferred_lft 63801sec
    inet IP-B/32 scope global eno3
       valid_lft forever preferred_lft forever
    inet6 ffff::fff:ffff:ffff:ffff/64 scope link 
       valid_lft forever preferred_lft forever
3: eno4: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 22:22:22:22:22:22 brd ff:ff:ff:ff:ff:ff
17: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 33:33:33:33:33:33 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
18: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000
    link/ether 44:44:44:44:44:44 brd ff:ff:ff:ff:ff:ff
20: vnet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel master virbr0 state UNKNOWN group default qlen 1000
    link/ether 44:44:44:44:44:44 brd ff:ff:ff:ff:ff:ff

I have tried to find out if it is possible to configure the kvm network using virsh net-edit default

but I have not found any reference that allows to make use of a specific egress ip address of the main host without having to use a new virtual interface and redirect packets with iptables, since this causes a great latency in the connectivity.

Is there a more "native" way to tell kvm to use the specific IP?

The ip addresses and mac addresses shown are for reference only. I make use of ufw to manage the connections.

Score:1
pt flag

To assign IP-B to the virtual machine:

  • Create a bridge interface.

  • Move eno3 to the bridge.

    Note that this will probably result in loss of network connectivity, so you'll want to make sure you are performing these changes over some sort of out-of-band connection (like a serial console or graphic console).

    Exactly how to perform these steps in a persistent fashion varies from distribution to distribution; whatever you're using I'm sure you can find appropriate documentation.

  • Assign IP-A to the bridge device.

  • Attach your virtual machine to the bridge.

    If you're creating your vm using virt-install, that might look like:

    virt-install --network bridge=br0 ...
    

    If you're creating the domain XML manually, that means an <interface> stanza like this:

    <interface type="bridge">
      <source bridge="br0"/>
      <model type="virtio"/>
    </interface>
    
  • Assign eth0 (or whatever the interface is called) inside the virtual machine IP-B

This gives you:

enter image description here

Both your host and your virtual machine are connected to the same network.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.