30

I have follow this guide (Virtualization With KVM On Ubuntu 11.10) to setup my KVM (Virtual Machines Software) on my Ubuntu 11.10 Server. However, I didn't setup my VM's IP address when creating the VM, instead of using:

vmbuilder kvm ubuntu --suite=oneiric --flavour=virtual --arch=amd64 --mirror=http://de.archive.ubuntu.com/ubuntu -o --libvirt=qemu:///system --ip=192.168.0.101 --gw=192.168.0.1 --part=vmbuilder.partition --templates=mytemplates --user=administrator --name=Administrator --pass=howtoforge --addpkg=vim-nox --addpkg=unattended-upgrades --addpkg=acpid --firstboot=/var/lib/libvirt/images/vm1/boot.sh --mem=256 --hostname=vm1 --bridge=br0

I used: (I deleted "--ip=192.168.0.101 --gw=192.168.0.1" from the command line)

vmbuilder kvm ubuntu --suite=oneiric --flavour=virtual --arch=amd64 --mirror=http://de.archive.ubuntu.com/ubuntu -o --libvirt=qemu:///system --part=vmbuilder.partition --templates=mytemplates --user=administrator --name=Administrator --pass=howtoforge --addpkg=vim-nox --addpkg=unattended-upgrades --addpkg=acpid --firstboot=/var/lib/libvirt/images/vm1/boot.sh --mem=256 --hostname=vm1 --bridge=br0

I have set up the network bridge as the guide instructed and the new VM's interface is connected to the network bridge.

I assume the KVM will assign my VM via DHCP but I don't have information on my new VM's IP address, where can I find the VM's IP address and SSH to the new VM? Thanks.

[Notes: I have managed to login the VM without knowing the IP address of the VM. Using "Xming + SSH with X Graphic Forwarding" But there is no DHCP ip address assigned to my VM, Besides the above question, I have another question here: How to enable the DCHP on my VM so when I use Xming to login via "virt viewer" I can at least see my IP address is there.]

  • I have figured out the problem and solved it. In the /etc/network/interfaces I deleted "network" and "broadcast" information and the DHCP worked on my guest VMs. Now I can ssh into my Guest VM. I guess my "network XXX.XXX.XXX.XXX" or "broadcast XXX.XXX.XXX.XXX" is wrong. the guide is still working well for this kind of setup, just be careful when set up your own network environment. The problem is solved!!! – Xianlin Mar 9 '12 at 5:25
  • 1
    libvirt has two commands now: domifaddr and net-dhcp-leases – Nehal J Wani Mar 18 '15 at 0:04
  • please refer this link, you will find a answer: – user169994 May 12 '16 at 3:38

14 Answers 14

20

You can run arp -n to see what IP your virtual machine pick up. In that way, you don't have to login guest vm and type ifconfig.

The blog below has more details and includes a perl script which automates finding the address of a virtual machine.

Tip: Find the IP address of a virtual machine

  • So simple. Great.... – Indika K Nov 10 '16 at 4:22
  • 1
    Go to xyz webpage for answers are no good if link goes down, please post "full answers" – FreeSoftwareServers Feb 24 '18 at 8:11
30

Try this:

virsh net-list
virsh net-dhcp-leases <net-name>   <--- net-name from above command

You can also use following form if you know the MAC address:

virsh net-dhcp-leases <net-name> --mac <mac-address>

The MAC address can be found from dumpxml command.  See Is there a way to determine which virtual interface belongs to a virtual machine in a kvm host?

  • 5
    This is the correct answer. Works without any hacking. Thanks a lot. – chmac Jun 24 '16 at 13:46
  • 1
    Should be "virsh net-list" ... no space between "net" and "list". Otherwise, this works perfectly. – Dave Hein Aug 14 '16 at 13:57
  • @DaveHein Thanks for correcting. Updated the answer. – Rahul Aug 14 '16 at 17:50
5

list for vms:

virsh list

get vm MAC from name

virsh domiflist debian8

do the scan of your vm subnet with grep (MAC must be capitalized)

nmap -sP 10.0.0.0/24 | grep 52:54:00:FD:1F:92 -B 3 

result

Nmap scan report for 10.0.0.173
Host is up (0.0012s latency).
MAC Address: 52:54:00:FD:1F:92 (QEMU Virtual NIC)
4

If you have console access to the VM then just run ifconfig -a on the guest. While you are there, check to make sure that the guest does have network connectivity and that sshd is running.

If you have not console access, then chances are the IP address was assigned by DHCP. Look for another machine that is also getting a DHCP assignment, maybe the host server, and then add 1 to the address and try it. In fact, try the next 5 or 6 addresses. If that doesn't work, then you either have a large active network and will need to try every IP address in the subnet, or there is a lower level network problem like no route to host or two hosts with the same MAC address. Or maybe you just didn't get sshd running.

Using the console is the easiest way to solve this problem.

  • I actually managed to connect to my VM via Xming Server + "virt-viewr" command. I found there is no IP assigned to the guest while I am in the VM. How to make DHCP works? Any XML file I need to change? – Xianlin Mar 2 '12 at 6:21
  • Hi Michael, do you think you can help me if I give you access to my server? I tried DHCP and Static IP but the guest VMs are not getting any IP, my host server still can access the internet though. thanks. – Xianlin Mar 5 '12 at 3:21
4

I guess this is an old question, but the current versions of virsh make this a lot easier if you're using a nat or bridged private network. I have a virtual machine named steak on a (routed) private network (AKA "NAT"). It's just two commands to find what IP is was assigned by the built-in mechanism:

$ sudo virsh list
 Id    Name                           State
----------------------------------------------------
 21    steak                          running

$ sudo virsh domifaddr steak
 Name       MAC address          Protocol     Address
-------------------------------------------------------------------------------
 vnet0      76:0c:28:ab:0e:ee    ipv4         10.14.1.1/24

I also have another VM (eggs) which is bridged to the regular network (connected to a bridge device on the hypervisor host). Libvirt doesn't assign it an address; it gets an address from my network's DHCP server, which also updates dynamic DNS in my case. That's one likely possibility when there's no output from domifadd for this VM. So, you basically have to find the address like it's any other machine - finding it in the arp table is probably the easiest -- which means ip neighbour now, as arp is deprecated and no longer present on some distributions. Luckily for those of us who don't spell things with extrae vouwels, you can also use shorter versions, like ip neigh and ip n (or ip neighbor). ;) Below, I use domiflist to find the MAC address (note that it's connected to br0 in the "source" column) and then find that in the arp table.

$ sudo virsh domifaddr eggs
 Name       MAC address          Protocol     Address
-------------------------------------------------------------------------------

$ sudo virsh domiflist eggs
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      bridge     br0        virtio      52:54:00:2c:ac:ee

$ ip neigh | grep -i 52:54:00:2c:ac:ee
192.168.0.226 dev br0 lladdr 52:54:00:2c:ac:ee REACHABLE
$ host eggs
eggs.home.domain.com has address 192.168.0.226
3

To see the IP address of your VM/s just run:

$ arp -n

If arp isn't installed on your system just install the net-tools package.

2

It's possible to connect through builtin VNC with virt-manager, and you do tty login, find the ip address with ifconfig eth0. Normally eth0, unless you've specified otherwise.

2

sudo nmap -sn 192.168.5.0/24 | awk '/Nmap scan report/{printf $5;printf " ";getline;getline;print $3;}' | fgrep -if <(virsh list --name | grep . | while read n; do virsh domiflist $n; done | grep --only-matching ..:..:..:..:..:..)

Scan subnet. Parse the output with awk to get lines like <IP> <MAC>. Then grep in them using a list of the MACs of the VMs.

The list of the MACs is obtained by listing all of the VMs (also strip empty lines), then doing virsh domiflist for each of them, and then grepping for a pattern that looks like a MAC.

Sources:

https://serverfault.com/a/669862/284568

0

If you have QEMU(VM emulator) go to "i" button and check the network card associated with "Br0" network. Take a note of the mac address of the NIC. Now Login to your VM -> Open Terminal -> type: "ifconfig" command in the terminal -> take a note of IP address associated with the the Mac address that you have noted earlier.

You can login to your VM using Putty or any ssh client using the IP you have noted in the last step.

0

I have my KVM guests on br0 interface so I think its a bit different, but here is my two scripts I made.

Note to use ARP you first need to have the MAC in your Arp Table. Therefor its best to use fping to do a quick ping of entire network (takes like 2 seconds). This makes sure your Arp cache is up to date.

apt-get install fping
yum install fping

Find a single guest IP via :

cat << 'EOF' > ~/findip.sh
#!/bin/bash
#FreeSoftwareServers.com

echo "Finding All Active IP's in Network via single Ping"
sleep 2s

fping -a -g 192.168.1.0/24
#nmap -sn 192.168.1.0/24

echo "Please Enter the Exact Name of the VM Guest:"

read guestname

arp -na | awk -v mac=$(virsh domiflist $guestname | awk '$2=="bridge"{print $NF}') '$0 ~ " at " mac {gsub("[()]", "", $2); print $2}'
EOF
chmod +x ~/findip.sh
sudo sh -c 'echo "alias findguestip=~/findip.sh" >> ~/.bashrc'
source ~/.bashrc
findguestip

Find All Guest IP's via:

cat << 'EOF' > ~/findallips.sh
#!/bin/bash
#FreeSoftwareServers.com

echo "Finding All Active IP's in Network via single Ping"
sleep 2s

fping -a -g 192.168.1.0/24
#nmap -sn 192.168.1.0/24

domainlog=/tmp/domain.log

virsh list --all | grep running |  cut -c 8- >> "$domainlog"

sed -i 's/running*//g' "$domainlog"

readarray domain < "$domainlog"

for i in "${domain[@]}"
do
ip="$(arp -na | awk -v mac=$(virsh domiflist $i | awk '$2=="bridge"{print $NF}') '$0 ~ " at " mac {gsub("[()]", "", $2); print $2}')"
echo "Hostname : $i IP : $ip"
done
rm "$domainlog"
EOF
chmod +x ~/findallips.sh
sudo sh -c 'echo "alias findallips=~/findallips.sh" >> ~/.bashrc'
source ~/.bashrc
findallips
  • I tackled this problem in the end with arp-scan. example: sudo arp-scan --interface=br0 --localnet --bandwidth=8192000 --numeric --retry=1 that was for when I wanted to actively search. I used addrwatch to sit in the background and accumulate a mac/ip address mapping for me. – Duke Dougal Feb 26 '18 at 20:38
0

I wrote a get-vm-ip script (available at https://github.com/earlruby/create-vm/blob/master/get-vm-ip) which uses this to get the IP:

HOSTNAME=[your vm name]
MAC=$(virsh domiflist $HOSTNAME | awk '{ print $5 }' | tail -2 | head -1)
arp -a | grep $MAC | awk '{ print $2 }' | sed 's/[()]//g'
0
  1. Get your domain list by virsh list --all

    virsh # list --all
    
    2     webserver_01                   running
    
  2. Get your domain interface list by domain id or name using virsh domiflist yourDomainId, and you will see the MAC ADDRESS of the domain's interface.

    virsh # domiflist 2
    
    bridge     br0        virtio      52:54:00:42:be:96
    
  3. Using nmap scan your LAN by nmap -sn 192.168.1.0/24|grep -i '52:54:00:42:be:96' -B 2, you will get the IP ADDRESS.

    [root@kvm-master ~]# nmap -sn 192.168.1.0/24|grep -i '52:54:00:42:be:96' -B2 
    
    Nmap scan report for bogon (192.168.1.210)
    Host is up (0.00013s latency).
    MAC Address: 52:54:00:42:BE:96 (QEMU Virtual NIC)
    
0

A nice solution which use the qemu-guest agent.

a)Configure your vm to use qemu-guest agent On host: Add with virt-manager a "qemu-guest-agent" channel Is really easy.In my case machine is called "Debian",and xml after modify will be similar to this.

 <channel type='unix'>
      <source mode='bind' path='/var/lib/libvirt/qemu/channel/target/domain-3-debian/org.qemu.guest_agent.0'/>
      <target type='virtio' name='org.qemu.guest_agent.0' state='connected'/>
      <alias name='channel1'/>
      <address type='virtio-serial' controller='0' bus='0' port='2'/>
    </channel>

b)On guest install qemu-guest-agent and ensure is started

apt -y install qemu-guest-agent

systemctl status qemu-guest-agent.service
● qemu-guest-agent.service - LSB: QEMU Guest Agent startup script
   Loaded: loaded (/etc/init.d/qemu-guest-agent; generated; vendor preset: enabled)
   Active: active (running) since Sat 2019-01-26 09:35:57 CET; 3s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1624 ExecStop=/etc/init.d/qemu-guest-agent stop (code=exited, status=0/SUCCESS)
  Process: 1630 ExecStart=/etc/init.d/qemu-guest-agent start (code=exited, status=0/SUCCESS)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/qemu-guest-agent.service
           └─1638 /usr/sbin/qemu-ga --daemonize -m virtio-serial -p /dev/virtio-ports/org.qemu.guest_agent.0

c)Finally this nice pipe will return the ip,instead of NAMEOFVMNIC put eth0,enp1s0,etc..

virsh domifaddr  --domain yourvmname --source agent|grep -w NAMEOFVMNIC|egrep -o '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}'
-1

(While you seem too advanced for such a simple answer)

Will

$ ifconfig

work?

  • also, be careful, if you have duplicate instances of the same machine, as the machine will have issues with the fake MAC address etc... there are ways to manually edit that (and I think run a command for that as well) – David Mar 2 '12 at 3:58
  • I only have one VM "vm1" on my ubuntu server host and the "ifconfig" command output shows no ip address assigned to my "eth0" network adapter. – Xianlin Mar 2 '12 at 4:59
  • yeah, you have no ip... Did you try "dhclient eth0" --might need sudo – David Mar 3 '12 at 23:21
  • I tried but still not getting dhcp ip from my host server to my guest VMs. My host server is assigned a fixed IP and it can access the internet. – Xianlin Mar 5 '12 at 3:22
  • if you want an ip not on the parent network, you want to select NAT in your vm settings instead of bridged. – David Mar 5 '12 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.