Get name of first configured interface

ifconfig | grep -B 1 "inet addr:" | head -1 | cut -d" " -f1
Get the line containing "inet addr:" and the line before that, get down to only the first line, and then get the first word on that line, which should be the interface.
Sample Output
eth0

-2
2010-01-04 23:02:57

These Might Interest You

  • From 'man netstat' "netstat -i | -I interface [-abdnt] [-f address_family] [-M core] [-N system] Show the state of all network interfaces or a single interface which have been auto-configured (interfaces statically configured into a system, but not located at boot time are not shown). An asterisk (``*'') after an interface name indicates that the interface is ``down''. If -a is also present, multicast addresses currently in use are shown for each Ethernet interface and for each IP interface address. Multicast addresses are shown on separate lines following the interface address with which they are associated. If -b is also present, show the number of bytes in and out. If -d is also present, show the number of dropped packets. If -t is also present, show the contents of watchdog timers."


    -2
    netstat -an | grep -i listen
    scubacuda · 2009-02-19 19:27:49 3
  • parses the output of ifconfig to show only the configured ip address (in this case from interface eth0). the regexp is quick'n'dirty im sure it can be done in a better way. --> this alias does not show your "internet ip" when you're in a nat-environment Show Sample Output


    0
    alias showip="ifconfig eth0 | grep 'inet addr:' | sed 's/.*addr\:\(.*\) Bcast\:.*/\1/'"
    dizzgo · 2009-03-25 07:50:12 7
  • Short list of all ip. Shows even the newly supported multiple ip address on the same interface (see eth1). For ipv6 use -6 Show Sample Output


    4
    ip -o -4 a s
    ioggstream · 2012-09-19 10:59:05 0
  • Shows the interface and the ip-address Show Sample Output


    1
    { if (/^[A-Za-z0-9]/) { interface=$1; next } else { if (/inet [Aa][d]*r/) { split($2,ip,":") } else { next } } print interface"\t: "ip[2] }
    gismo82 · 2010-12-14 15:38:25 0

What Others Think

and if you remove head -1, it will show all of the configured devices
ik_5 · 441 weeks and 4 days ago
I wrote this for conky where I wanted to do downspeedf of the first active interface because it's name changes, so that's why I use head -1 to get it to only one interface.
jasonwalsh · 441 weeks and 2 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: