117

This is in regard to linux, but if anyone knows of a general *nix method that would be good.

I booted a system yesterday with an ethernet cable plugged in. "NetworkManager" is not installed, so once it started I went to look for the name of the ethernet interface with ifconfig to start a DHCP client manually, but it did not show anything other than lo.

The NIC was listed via lspci, and the appropriate kernel driver was loaded. The system normally uses wifi, and I could remember the interface name for that was wlan0. When I tried ifconfig wlan0 up, wlan0 appeared. But the only ethernet interface names I could remember were eth[N] and em[N] -- neither of which worked.

This document refers to "predictable interface names" but does not do a good job of explaining what they might be in simple terms. It does refer to a piece of source code which implies the name in this case might be deduced from the the PCI bus and slot numbers, which seems like an unnecessarily complicated hassle.

Other searching around led me to believe that this might be determined by systemd in conjunction with udev, but there are almost 100 files in /usr/lib/udev/rules.d and spending an hour trying to determine where (and if) there's a systemd config file for this also seems ridiculous.

It would also be nice to know for certain that they are available, not just how they might be named if they are, so I can rule out hardware problems, etc. Isn't there a simple way to find the names of available network interfaces on linux?

goldilocks
  • 87,661
  • 30
  • 204
  • 262
  • In the question you state that you used ip link but then you accept an answer that suggests ip link show which does the exactly same thing. Why? – Pavel Šimerda Apr 23 '14 at 16:07
  • The manual pages for iproute2 are quite good nowadays, I guess you're using and older version of the package. – Pavel Šimerda Apr 25 '14 at 17:12

6 Answers6

112

The simplest method I know to list all of your interfaces is

ifconfig -a

EDIT

If you're on a system where that has been made obsolete, you can use

ip link show
  • 3
    If I'd just looked at the man page! Be interesting to know if ip has a parallel, since supposedly ifconfig is being "obsoleted" in favour of that (IMO) more obtuse and less user friendly command. – goldilocks Apr 18 '14 at 15:05
  • @TAFKA'goldilocks' I haven't heard that. Are you sure? The commands serve different purposes. ip - show / manipulate routing, devices, policy routing and tunnels and ifconfig - configure a network interface – Elliott Frisch Apr 18 '14 at 15:07
  • 3
    First thing in my man ifconfig: "NOTE This program is obsolete! For replacement check ip..." That may be specific to linux. Hopefully they have to retain it for backward compatibility with something. – goldilocks Apr 18 '14 at 15:09
  • @TAFKA'goldilocks' I'm on 12.04 ubuntu. Things do change from time to time. Try ip link show – Elliott Frisch Apr 18 '14 at 15:10
  • :) That works too. You should edit it into the answer to illustrate the parallel with ifconfig. – goldilocks Apr 18 '14 at 15:12
  • 7
    ifconfig has been deprecated, TMK, so we should be trying to show examples using ip. See the ifconfig wikipedia article: https://en.wikipedia.org/wiki/Ifconfig. – slm Apr 18 '14 at 16:35
  • 4
    @slm Although the question is (was) linux specific, an example using both seems good as BSD users may be in the habit of looking at "linux" pages anyway -- in fact I'll take linux out of the title and edit a bit (originally this was self-answered and I did not think there was a portable method). – goldilocks Apr 18 '14 at 16:44
  • 7
    ifconfig deprecated? come on. This command has been around for like 30 years. – Michael Martinez Apr 18 '14 at 18:02
  • @MichaelMartinez - It is according to the Wikipedia page and other sources if you google it. I'm not the one deprecating it, so don't shoot the messenger. – slm Apr 18 '14 at 23:54
  • 1
    @TAFKA'goldilocks' - if it wasn't tagged linux I wouldn't have said anything but we have a lot of back catalog that shows ifconfig and we should be nudging ppl to getting more comfortable w/ ip going forward. If the intent was unix and not linux per-say then I'll remove the comments if someone who's sure wants to retag it. BTW, I think your A is better 8-) – slm Apr 18 '14 at 23:55
  • ifcconfig can (and likely will) remain deprecated for quite some time, and even if removed from the base system - I would expect it to be available from the repo (or at least a PPA). – Elliott Frisch Apr 19 '14 at 00:05
  • use ip l if you want to save some chars there. – sitilge Mar 17 '17 at 21:34
  • @MichaelMartinez Nevertheless if you had checked ifconfig(8) you'd know why it is. See the NOTES and BUGS section for starters. – Pryftan Nov 02 '19 at 12:11
  • @Pryftan I'll check that out – Michael Martinez Nov 02 '19 at 17:10
  • @MichaelMartinez I might also add that technically netstat is also considered deprecated iirc. Instead one should use ss. But then that's Linux; I've not really used BSD or any other Unix in years but I have this vague thought that BSD does have netstat! – Pryftan Nov 04 '19 at 18:49
  • For physical NIC, you can use

    find /sys/class/net/*/device

    – ticapix Feb 04 '20 at 09:38
90

The kernel lists them by name in /sys, both separately in (e.g.) the tree of PCI devices -- although finding them there if you don't know where they are to start with is not simple -- and together via symlinks in /sys/class/net. E.g.:

> ls /sys/class/net
em1 lo wlp6so

Another example:

> ls /sys/class/net
lo p6s1 wlan0

If you are not sure which is which, you could just put them all up with either:

ifconfig [name] up 

Or:

ip link set [name] up

And then look at the (fairly clear) clues listed by ifconfig (with no arguments). The systemd source code linked from the Freedesktop.org document also refers to:

Two character prefixes based on the type of interface:
 *   en -- ethernet
 *   sl -- serial line IP (slip)
 *   wl -- wlan
 *   ww -- wwan

Although in one of the cases above (p6s1) there is no prefix.

goldilocks
  • 87,661
  • 30
  • 204
  • 262
  • The prefix I get for ethernet is eth. I believe en is for wireless lan. – Kaushal Modi Jan 23 '15 at 13:36
  • 4
    @kaushalmodi eth is a linux kernel thing; systemd implements via udev Freedesktop.org's predictable interface names -- the kernel ones are not predictable in the same sense, which is the cause of all the confusion. The quote above is explicitly cut n' pasted from here. There's more to it than that, since I don't have any systemd machines using en for ethernet; the closest is em, others are completely different. You can read the Freedesk.org link in the question yourself for more explanation. – goldilocks Jan 23 '15 at 16:27
  • 4
    [Later] Sometime in the past year Fedora or systemd corrected that? My PCI ethernet is now eno1. – goldilocks Jan 08 '16 at 13:36
  • @goldilocks &c: you can override that by the kernel command line options: biosdevname=0 net.ifnames=0. I never found the systemd way 'predictable' and eth as a prefix seemed reasonable to me so I have always had that. Anyway there it is: you can have it the pseudo-random way (i.e. systemd) or the other way depending on kernel options. – Pryftan Nov 02 '19 at 12:14
13

I always do cat /proc/net/dev it's simple and easy to memorize

6

ifconfig = Solaris
ipconfig = Windows
ip = Linux

That I know, but am certainly open to additions and corrections.

sam
  • 22,765
  • 2
    Huh? ifconfig is Linux also. – fpmurphy Apr 27 '16 at 20:57
  • 5
    @fpmurphy1 Read the first paragraph (after the synopsis) of man ifconfig on a linux system. – goldilocks Apr 27 '16 at 21:21
  • @fpmurphy ifconfig(8) is also deprecated under Linux. Please see the sections NOTES and BUGS. This has been the case for years now. So although ifconfig works under Linux it's not the correct way. – Pryftan Nov 15 '19 at 18:54
  • @Pryftan. Just because a utility is depreciated, does not mean that the utility does not work for your requirements. Correct way? Sorry but Linux does not mandate correct ways. – fpmurphy Nov 26 '19 at 08:51
  • @fpmurphy For some uses however it does not work. Yes it can work but using deprecated ways is - for many cases - just silly. Why would anyone want to use the old way that for all you know could be removed in the future? It's been deprecated for many many years now! Anyway for ifconfig what's the point of using it? ip is more than sufficient. But hey if you like it so be it. I'm just saying that there are reasons it's deprecated! – Pryftan Dec 02 '19 at 12:55
  • @fpmurphy you're right, "correct" is the wrong word. "Officially supported" is a better way to express it. For a number of people, community / enterprise support is very important. If it's not important to you, then you keep doing your thing. Some distros have already stopped including the utility, and I imagine as time goes by it'll be more and more difficult to install and use the ifconfig utility (esp. once they stop building packages completely). But it's open source, so as long as the Linux API remains compatible, keep on keeping on - just expect it to take more work over time. – Chris Eberle Apr 06 '22 at 15:39
1

When there is no ifconfig, ip available in the system one way that helped me getting IP address on Ubuntu was:

hostname -i

As pointed out in the comments, this mechanism relies on DNS hostname reolution and will not provide authorative results for all available network interfaces.

Still, this command might be helpful in troubleshooting scenarios with containers (Docker, LXC), where system tools are often not available. And at least in some cases it shows container IP which might be helpful depending on your goal.

  • Probably not a great methodology as this is done via DNS resolution and on systems with hostname localhost (eg., likely most PC users), it will yield 127.0.0.1. None the less may be useful on whatever oddball system it is that doesn't have [/usr]/sbin/ip. – goldilocks Jun 30 '22 at 13:59
  • I thought that as well. But in fact in my containers I'm getting their IP in that way. I think, in some cases that command may be useful. – Sergey Shcherbakov Jun 30 '22 at 14:15
  • 1
    Makes sense -- I didn't think of that. You might want to edit details into the answer for people who's search terms include (eg) docker or lxc. – goldilocks Jun 30 '22 at 14:42
0
ip -br a

short and easy to remember thanks to a silly clothing mnemonic.

prints something like:

lo               UNKNOWN        127.0.0.1/8 ::1/128 
enp0s32f6        DOWN           
wlp0s10f3        UP             192.168.3.144/24 ge80::f074:13c4:50e1:41a8/64 
docker1          DOWN           172.17.3.1/16 
naugtur
  • 2,403