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 10-16-2008, 12:20 PM   #1
LQ Newbie
Registered: May 2007
Posts: 3

Rep: Reputation: 0
How do I find out what my active ethernet interface is?

I'm looking for a one line command that will return the ID of the active ethernet interface - i.e., that will return 'eth0' if I'm on my cabled interface or 'wlan0' if I'm on my wireless. I know that by searching through the output from 'ifconfig' I can figure out which one is active - I'd like to determine that by a command (so I can stick the output in a script where I need it).

I'm guessing I could create something if I were a genius at scripting, but I'm drawing a blank so far (since I'm not the aforementioned genius!)

Any ideas? Thanks!
Old 10-16-2008, 02:48 PM   #2
Senior Member
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,414
Blog Entries: 1

Rep: Reputation: 65
Tricky question since you can have more than one interface active at the same time.
For example, my laptop is plugged by wire on my office LAN and to internet by GSM modem.
At same time I can access files on internal lan and browse the internet not using the office infra-structure but the GSM network.

"ifconfig" without any arguments return all active interfaces. So just get the first line and cut out everything else but the id:
[miguel@babylon5 ~]$ /sbin/ifconfig | grep "Link encap:Ethernet" | cut -d' ' -f1
[miguel@babylon5 ~]$
If, by active you mean the interface that connects to external world, you can use the "route" command; get the default gateway line, and return the id in the last field:
[miguel@babylon5 ~]$ /sbin/route -n | grep "^" | rev | cut -d' ' -f1 | rev
[miguel@babylon5 ~]$
Old 10-16-2008, 05:19 PM   #3
LQ Newbie
Registered: May 2007
Posts: 3

Original Poster
Rep: Reputation: 0
That's fabulous - the second one looks exactly like what I had in mind. Thanks!


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
how can i shutdown the PHYSICAL interface of an ethernet interface markraem Linux - Networking 2 04-12-2007 03:29 PM
interface eth0 is not (yet?) active jorcas SUSE / openSUSE 1 11-22-2004 07:53 PM
no active interface (yet?) network... breezewax Linux - Networking 17 09-16-2004 10:13 PM
ethernet active, but no access to internet nouwen Linux - Newbie 3 06-14-2004 01:38 PM
Can't Bring Ethernet Up / Can't Active Ethernet / Guigo Linux - Newbie 8 04-03-2004 09:31 AM > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 03:24 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