Sunday, January 30, 2022

Steps to "fix" the dynamic ip of KVM

 https://www.cyberciti.biz/faq/linux-kvm-libvirt-dnsmasq-dhcp-static-ip-address-configuration-for-guest-os/
"KVM libvirt assign static guest IP addresses using DHCP on the virtual machine"







  - in this blog-post :

I'm connecting to a running KVM via dynamic iP ;

I’m changing DHCP for default Virtual Network to grant “fixed” iPs based on KVM’s mac Hwaddress .

Eventually I'm connecting to same KVM via a “fixed” iP that won’t change after KVM restarts ;




====================================================================

- List all KVMs :

--------------------------------------------------------


root@ubuntu2004:~# virsh list --all

Id Name State

---------------------------

1 centos82a running



====================================================================

- Check mac of running KVM - "centos82a" :

--------------------------------------------------------


root@ubuntu2004:~# virsh dumpxml centos82a | grep -i '<mac'

<mac address='52:54:00:a1:16:27'/>

root@ubuntu2004:~#

root@ubuntu2004:~#



====================================================================

- Check the list of virtual networks :

--------------------------------------------------------


root@ubuntu2004:~# virsh net-list

Name State Autostart Persistent

--------------------------------------------

default active yes yes



====================================================================

We need dynamic iPs that can be assigned to virtual machines on default network ;

- Check dhcp range of default network bridge :

--------------------------------------------------------


root@ubuntu2004:~# virsh net-dumpxml default

<network>

<name>default</name>

<uuid>37775fce-316b-4e75-9bac-b18f65b5655e</uuid>

<forward mode='nat'>

<nat>

<port start='1024' end='65535'/>

</nat>

</forward>

<bridge name='virbr0' stp='on' delay='0'/>

<mac address='52:54:00:5b:c7:83'/>

<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>



====================================================================

Check iP of host OS :

--------------------------------------------------------


root@ubuntu2004:~#

root@ubuntu2004:~# ip a | grep glo

inet 172.20.124.107/20 brd 172.20.127.255 scope global dynamic eth0

inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0



====================================================================

- Check iP of guest KVM :

--------------------------------------------------------


root@ubuntu2004:~#

root@ubuntu2004:~# arp -n

Address HWtype HWaddress Flags Mask Iface

172.20.112.1 ether 00:15:5d:bf:f7:ba C eth0

192.168.122.223 ether 52:54:00:a1:16:27 C virbr0

root@ubuntu2004:~#

root@ubuntu2004:~#



====================================================================

- We can enter into KVM via dynamic iP that can change after shutdown / start :

--------------------------------------------------------


root@ubuntu2004:~# sshpass -p"toor" ssh root@192.168.122.223

Last login: Thu Jan 27 13:53:45 2022 from 192.168.122.1

[root@localhost ~]#

[root@localhost ~]# ip a | grep glo

inet 192.168.122.223/24 brd 192.168.122.255 scope global dynamic noprefixroute ens2

[root@localhost ~]#

[root@localhost ~]# logout

Connection to 192.168.122.223 closed.

root@ubuntu2004:~#

root@ubuntu2004:~#

root@ubuntu2004:~# virsh net-edit default


Select an editor. To change later, run 'select-editor'.

1. /usr/bin/vim.nox

2. /bin/nano <---- easiest

3. /usr/bin/vim.basic

4. /usr/bin/mcedit

5. /usr/bin/vim.tiny

6. /bin/ed


Choose 1-6 [2]: 3


Network default XML configuration not changed.


root@ubuntu2004:~#

root@ubuntu2004:~# select-editor


Select an editor. To change later, run 'select-editor'.

1. /usr/bin/vim.nox

2. /bin/nano <---- easiest

3. /usr/bin/vim.basic

4. /usr/bin/mcedit

5. /usr/bin/vim.tiny

6. /bin/ed


Choose 1-6 [2]: 4

root@ubuntu2004:~#

root@ubuntu2004:~#

root@ubuntu2004:~# virsh dumpxml centos82a | grep -i '<mac'

<mac address='52:54:00:a1:16:27'/>

root@ubuntu2004:~#

root@ubuntu2004:~#

root@ubuntu2004:~# virsh net-dumpxml default

<network>

<name>default</name>

<uuid>37775fce-316b-4e75-9bac-b18f65b5655e</uuid>

<forward mode='nat'>

<nat>

<port start='1024' end='65535'/>

</nat>

</forward>

<bridge name='virbr0' stp='on' delay='0'/>

<mac address='52:54:00:5b:c7:83'/>

<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@ubuntu2004:~#




====================================================================

On "default" VirtuaL Network , we add this line at <dhcp> :

<host mac='52:54:00:a1:16:27' name='centos82a' ip='192.168.122.111'/>

After change : net-destroy + net-start on "default"

--------------------------------------------------------


root@ubuntu2004:~#

root@ubuntu2004:~# virsh net-edit default

Network default XML configuration edited.


root@ubuntu2004:~#

root@ubuntu2004:~# virsh net-destroy default

Network default destroyed


root@ubuntu2004:~# virsh net-start default

Network default started


root@ubuntu2004:~#

root@ubuntu2004:~# virsh net-dumpxml default

<network>

<name>default</name>

<uuid>37775fce-316b-4e75-9bac-b18f65b5655e</uuid>

<forward mode='nat'>

<nat>

<port start='1024' end='65535'/>

</nat>

</forward>

<bridge name='virbr0' stp='on' delay='0'/>

<mac address='52:54:00:5b:c7:83'/>

<ip address='192.168.122.1' netmask='255.255.255.0'>

<dhcp>

<range start='192.168.122.2' end='192.168.122.254'/>

<host mac='52:54:00:a1:16:27' name='centos82a' ip='192.168.122.111'/>

</dhcp>

</ip>

</network>


root@ubuntu2004:~#

root@ubuntu2004:~# virsh dumpxml centos82a | grep -i '<mac'

<mac address='52:54:00:a1:16:27'/>

root@ubuntu2004:~#

root@ubuntu2004:~# sshpass -p"toor" ssh root@192.168.122.223

ssh: connect to host 192.168.122.223 port 22: No route to host

root@ubuntu2004:~#

root@ubuntu2004:~#

root@ubuntu2004:~# arp -n

Address HWtype HWaddress Flags Mask Iface

192.168.122.111 (incomplete) virbr0

192.168.122.223 (incomplete) virbr0

172.20.112.1 ether 00:15:5d:bf:f7:ba C eth0

root@ubuntu2004:~#

root@ubuntu2004:~#

root@ubuntu2004:~#




====================================================================

Shutdown / start KVM "centos82a" :

--------------------------------------------------------


root@ubuntu2004:~# virsh shutdown centos82a

Domain centos82a is being shutdown


root@ubuntu2004:~#


root@ubuntu2004:~#

root@ubuntu2004:~# virsh list --all

Id Name State

----------------------------

- centos82a shut off


root@ubuntu2004:~#



root@ubuntu2004:~#

root@ubuntu2004:~# virsh start centos82a

Domain centos82a started


root@ubuntu2004:~#

root@ubuntu2004:~#

root@ubuntu2004:~# virsh list --all

Id Name State

---------------------------

2 centos82a running


root@ubuntu2004:~#

root@ubuntu2004:~#

root@ubuntu2004:~#

root@ubuntu2004:~# arp -n

Address HWtype HWaddress Flags Mask Iface

192.168.122.111 (incomplete) virbr0

192.168.122.223 (incomplete) virbr0

172.20.112.1 ether 00:15:5d:bf:f7:ba C eth0

root@ubuntu2004:~#

root@ubuntu2004:~#

root@ubuntu2004:~#



root@ubuntu2004:~#

root@ubuntu2004:~# arp -n

Address HWtype HWaddress Flags Mask Iface

192.168.122.111 ether 52:54:00:a1:16:27 C virbr0

192.168.122.223 (incomplete) virbr0

172.20.112.1 ether 00:15:5d:bf:f7:ba C eth0

root@ubuntu2004:~#

root@ubuntu2004:~#





====================================================================

- Now We can enter into KVM via dynamic iP that won't change after shutdown / start :

it's fixed by mac address

the new iP "192.168.122.111" it's fixed by mac (HWadress) "52:54:00:a1:16:27"

--------------------------------------------------------


root@ubuntu2004:~#

root@ubuntu2004:~# sshpass -p"toor" ssh root@192.168.122.111

Last login: Sun Jan 30 05:45:54 2022 from 192.168.122.1

[root@centos82a ~]#

[root@centos82a ~]# cat /etc/*rele* | grep -i linux | sort -u

CentOS Linux release 8.2.2004 (Core)

Derived from Red Hat Enterprise Linux 8.2 (Source)

NAME="CentOS Linux"

PRETTY_NAME="CentOS Linux 8 (Core)"

[root@centos82a ~]#

[root@centos82a ~]# ip a | grep glo

inet 192.168.122.111/24 brd 192.168.122.255 scope global dynamic noprefixroute ens2

[root@centos82a ~]#


[root@centos82a ~]# 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: ens2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000

link/ether 52:54:00:a1:16:27 brd ff:ff:ff:ff:ff:ff

inet 192.168.122.111/24 brd 192.168.122.255 scope global dynamic noprefixroute ens2

valid_lft 2955sec preferred_lft 2955sec

inet6 fe80::f55f:7b40:d40c:3062/64 scope link noprefixroute

valid_lft forever preferred_lft forever

[root@centos82a ~]#


[root@centos82a ~]# logout

Connection to 192.168.122.111 closed.

root@ubuntu2004:~#

root@ubuntu2004:~#


root@ubuntu2004:~#

root@ubuntu2004:~# virsh net-dhcp-leases default

Expiry Time MAC address Protocol IP address Hostname Client ID or DUID

-------------------------------------------------------------------------------------------------------------

2022-01-30 17:09:45 52:54:00:a1:16:27 ipv4 192.168.122.111/24 centos82a 01:52:54:00:a1:16:27


root@ubuntu2004:~#


root@ubuntu2004:~# cat /etc/*rele* | grep -i version

VERSION="20.04.3 LTS (Focal Fossa)"

VERSION_ID="20.04"

VERSION_CODENAME=focal

root@ubuntu2004:~#




No comments:

Post a Comment