Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Networking
User Name
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.


  Search this Thread
Old 02-04-2005, 07:55 AM   #1
LQ Newbie
Registered: Feb 2005
Location: Tokyo Japan
Posts: 5

Rep: Reputation: 0
Question How to get ip address associated with an interface

Title says it all.

/sbin/ifconfig does it, but I want to do it in a C (or any other compiler) programs, without /sbin/ifconfig. I just want to get IPv4 address from an interface name (eth0,eth1, etc.) in a program to inform my dns service automatically (I hope you know what I mean )


Last edited by ro0tless; 02-04-2005 at 08:09 AM.
Old 02-04-2005, 10:32 AM   #2
LQ Newbie
Registered: Feb 2005
Location: Tokyo Japan
Posts: 5

Original Poster
Rep: Reputation: 0
Never mind. I got it.
Old 02-07-2005, 12:34 AM   #3
LQ Newbie
Registered: Feb 2005
Posts: 1

Rep: Reputation: 0
ro0tless, Could you update us with your findings...

Old 02-07-2005, 02:59 AM   #4
LQ Newbie
Registered: Feb 2005
Location: Tokyo Japan
Posts: 5

Original Poster
Rep: Reputation: 0
I found it in a source file of a client of another dns service ( It's not directly associated with network, but with the "interface" as a device.

see this code....

struct ifreq ifr;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
strcpy(ifr.ifr_name, device);
if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0)

"struct ifreq" is defined in /usr/include/linux/if.h so it can hold ip address, netmask, etc, and interface name.

It's cool, isn't it? But I myself haven't made my own yet

Last edited by ro0tless; 02-07-2005 at 03:08 AM.
Old 10-03-2011, 12:18 PM   #5
John Lumby
Registered: Oct 2008
Posts: 50

Rep: Reputation: 30
ioctl to retrive interface IP address

In case anyone else reads this, ro0tless's post has an incorrect ioctl parameter -

where he says
ioctl(fd, SIOCGIFFLAGS, &ifr)
it should read

ioctl(fd, SIOCGIFADDR, &ifr)


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
Add another IP address to an Interface hcclnoodles Linux - Software 1 08-19-2004 02:50 AM
URGENT: how to get an ip address of sending interface in C dravya Linux - General 4 05-25-2004 03:32 PM
Getting IP address from an interface in C dravya Programming 2 05-25-2004 02:48 PM
multiple ip address for single eth interface blackhound Linux - Networking 2 02-25-2004 06:02 AM
How can I retrieve the IP address of an interface? kobilevi Linux - Networking 2 05-15-2001 07:31 AM > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 10:38 PM.

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
Open Source Consulting | Domain Registration