Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 01-25-2007, 07:47 AM   #1
Big Dog XII
LQ Newbie
Registered: May 2004
Location: /UK/ENG/
Distribution: Gentoo
Posts: 7

Rep: Reputation: 0
How to get an IP Address from command line?

Hi there,

Here is a fun tidbit for a Thursday.

Is there a command that will return me just the IP Address for a given interface. Something akin to....

#getip eth0

And yes I know I could parse out the results from ifconfig, but if there is another way, please let me know.
Old 01-25-2007, 08:02 AM   #2
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Why don't you create a simple script and put it on your PATH?
ifconfig $1 | grep "inet addr" | gawk -F: '{print $2}' | gawk '{print $1}'
or alternatively create an alias with the command above. I don't know about any command to do this, except ifconfig.

Last edited by colucix; 01-25-2007 at 08:16 AM.
Old 01-25-2007, 08:15 AM   #3
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,388
Blog Entries: 15

Rep: Reputation: 1410Reputation: 1410Reputation: 1410Reputation: 1410Reputation: 1410Reputation: 1410Reputation: 1410Reputation: 1410Reputation: 1410Reputation: 1410
You could do it by extracting it from the config file for the interface.

e.g. for RH/FC:

grep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth0 |awk -F= '{print $2}'
You dismissed ifconfig for some reason. I don't know of a command that will give you just the IP. I wonder if you're aware you can do ifconfig by interface so you only have to parse from the specific interface you're interested in:

ifconfig eth0 |grep "inet addr" |awk '{print $2}' |awk -F: '{print $2}'
So far as making a single command you could easily do it with a script:

ifconfig $IFACE |grep "inet addr" |awk '{print $2}' |awk -F: '{print $2}'
You just name the above script "getip" and put execute permission on it with chmod. You can then do your "getip eth0" using this script. A side benefit is if you don't put eth0 or another interface at command line (i.e. just type "getip") it will show the IPs for all active interfaces including lo0 (
1 members found this post helpful.
Old 01-25-2007, 08:37 AM   #4
Senior Member
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Just a suggestion:

/sbin/ifconfig $1 | grep "inet addr" | gawk -F: '{print $2}' | gawk '{print $1}'
Save as a script (, and execute with the syntax: " eth1" without the quotes. Change eth1 to whichever interface you wish to know the IP of...

edit: beaten by jlightner... damn!
Old 01-25-2007, 11:54 AM   #5
Big Dog XII
LQ Newbie
Registered: May 2004
Location: /UK/ENG/
Distribution: Gentoo
Posts: 7

Original Poster
Rep: Reputation: 0

Hi all,

Thanks for the replys. I knew about the ifconfig. Just checking to see if there was something else.

Thanks again.

Big Dog XII (woof)


commandline, networking

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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a single command to list all hardware installed (command line)? davee Linux - Hardware 6 02-28-2009 07:19 PM
ip address using command line Ammad Linux - Networking 2 04-24-2007 06:12 PM
command line address book program oneferna Linux - Software 3 02-12-2006 02:24 PM
Command to output file content line by line aznluvsmc Programming 2 09-12-2004 07:45 PM
51 characters only in the 1st Line of command line eggCover Linux - General 2 07-29-2004 01:28 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:30 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration