Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 02-05-2011, 06:54 AM   #1
tank junior
Registered: Apr 2010
Posts: 42

Rep: Reputation: 0
get unique host id of machine c/c++


I am looking for c/c++ code (no third party lib dependency) to get the physical address of ethernet card on mother board.

Most of them suggests to use the linux commands and process the output. For example "/sbin/ifconfig eth0".

The problem with above command is that when you run this on a linux variant which is running using VirtualBox on windows host, it's not returning the correct mac address. In this case the correct mac address is the one set for individual virtual machine.

"/sbin/ifconfig eth0" works fine when linux variant is installed normally.

There is another case when linux variant is installed normally and if you install virtualization apps such as VirtualBox or VmPlayer, they create a virtual adepter for their own use. In that case running "/sbin/ifconfig" returns all the adepters. (physical and virtual)


Old 02-06-2011, 01:20 AM   #2
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
Just a couple things for clarification, you're using three different terms in your post, and each has a different meaning. Your title says you are looking for "unique host id of machine," your first sentence asks for the "physical address of ethernet card," and then you say it's possible that ifconfig can return the wrong "mac address."

I assume what you want is the MAC address of the ethernet card, because that's what you spend most of the time talking about. This isn't a lecture, but be aware that the "physical address" of a device typically refers the the address in memory where a memory-mapped device can be accessed, and a "unique host id" probably causes people to think you're looking for processor serial numbers or the like.

First thing, I haven't messed with it, but if I recall, VirtualBox allows the user to specify a MAC address for any virtual network adapter added to the virtual machine. I think the MAC address is randomly selected for each NIC created (but I'm not 100% certain of that). The MAC address is under the "advanced" section of each adapters configuration if you're using the VirtualBox GUI. If you're not using the VirtualBox GUI, I imagine the MAC address is modified with modifyvm, etc.

The reason I mention all that is because, if you're running a C/C++ program on a virtual machine, I don't think it's even possible for you to retrieve the "real" or host machine's MAC address. Your C/C++ program's universe will be the guest machine--unless you do some special networking configuration.

With all that said, I'll give you two alternatives to ifconfig. The first is much, much better than the second. Though, the first may be source for what ifconfig reads.

1. Read the address from the /sys filesystem. For example, on my system:
$ cat /sys/class/net/eth0/address 
$ cat /sys/class/net/eth1/address 
2. Look at dmesg output. For example, on my system:
$ dmesg | grep eth0
[    1.854863] eth0: RTL8110s at 0xffffc9000067ec00, 00:22:fb:c3:3f:0b, XID 04000000 IRQ 21
[   13.241043] udev: renamed network interface eth0 to eth1
[   13.291075] udev: renamed network interface eth1_rename to eth0
[   16.314517] r8169: eth0: link up
[   16.314526] r8169: eth0: link up
[   26.560873] eth0: no IPv6 routers present
The MAC address of the NIC is displayed on the first line of the output. Keep in mind though, the output shows that udev rules might swap device names. In this case, eth0 got moved to eth1. Your program would need to check for such a rename, and then check if that new name was later renamed, and so on.

The only other option I could think of would be to try and read the MAC address directly from the NIC itself. The would mean accessing device registers through memory--device registers that can vary between different devices.

Last edited by Dark_Helmet; 02-06-2011 at 01:25 AM.


address, ethernet

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
file sharing between a virtual machine and the host machine okok Linux - Networking 3 06-21-2013 12:42 AM
Host Machine Issues Jahgro Linux From Scratch 3 05-20-2010 09:36 PM
[SOLVED] Mounting unique filesystem or directory by network booted host theNbomr Linux - Networking 5 04-07-2010 05:16 PM
Unique ID of machine, other than IP-Addr/MAC-Addr to prevent spoofing anupamsaxena Linux - Security 9 12-20-2009 10:58 PM
How to configure the serial port of my virtual machine with host machine. akram Linux - Newbie 4 07-31-2009 10:39 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:41 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration