With a bash script, can I read the mac address of my eth0 and print it to a file?
4 Answers
ifconfig will output information about your interfaces, including the MAC address:
$ ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:11:22:33:44:55
inet addr:10.0.0.1 Bcast:10.0.0.255 Mask:255.0.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:289748093 errors:0 dropped:0 overruns:0 frame:0
TX packets:232688719 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:3264330708 (3.0 GiB) TX bytes:4137701627 (3.8 GiB)
Interrupt:17
The HWaddr is what you want, so you can use awk to filter it:
$ ifconfig eth0 | awk '/HWaddr/ {print $NF}'
00:11:22:33:44:55
Redirect that into a file:
$ ifconfig eth0 | awk '/HWaddr/ {print $NF}' > filename
- 91,316
- 38
- 238
- 232
-
2You can also use th `ip` command: `/sbin/ip link show eth0`. – Keith Apr 18 '11 at 20:39
-
@Keith I don't have that command, I'm not sure which package it's in; I think `ifconfig` is fairly standard – Michael Mrozek Apr 18 '11 at 20:49
-
2@Michael On Linux anyway, it is the [preferred tool](http://lartc.org/lartc.html#LARTC.IPROUTE2.WHY). In fact you must use to configure some advanced features. But ifconfig lives on... – Keith Apr 18 '11 at 20:55
-
3The international approach is `LC_ALL=C ifconfig eth0 | awk '/HWaddr/ {print $NF}'`, because the output might be localized, not matching 'HWaddr'. LC_ALL=C uses the international standard. – user unknown Apr 19 '11 at 00:21
-
2@user Ah, cool. As far as I know it'll always be the first line, so you could just get away with `awk '{print $NF; exit}'` too – Michael Mrozek Apr 19 '11 at 00:31
-
1Yes, if you're the AWK-type. I'm the sed-type, but would here prefer an `| head -n 1` then. – user unknown Apr 19 '11 at 03:53
-
I know this is Unix & Linux community but this also works on QNX 6.5. – IsaacS Dec 31 '14 at 05:23
Here's a modern Linux method:
ip -o link show dev eth0 | grep -Po 'ether \K[^ ]*'
It's modern in that ifconfig has long been deprecated in favour of ip from the iproute2 package, and that grep has the -P option for perl regular expressions for the zero-width positive look-behind assertion.
grep -o is nice for text extraction. sed is traditionally used for that but I find the perl-style zero-width assertions clearer than a sed substitution command.
You don't actually need the -o (oneline) option to ip, but I prefer to use it when extracting network information since I find it cleaner having one record per line. If you're doing more complicated matches or extractions (usually with awk), -o is essential for a clean script, so for the sake of consistency and a common pattern, I always use it.
Edit: Updating 10 years later: ip now has the -j flag for JSON output, and when combined with jq, it provides a more robust and readable command pipeline:
ip -j link show dev eth0 | jq -r '.[0].address'
The -r flag to jq makes it output a raw string instead of a quoted (JSON) string.
- 38,261
- 8
- 74
- 62
-
1@michelemarcon: This does not use perl. It uses recent GNU grep and grep has an option for using perl-style regular expressions. – camh Apr 19 '11 at 13:18
-
#! /bin/sh
/sbin/ifconfig eth0 | perl -ne 'print "$1\n" if /HWaddr\s+(\S+)/' >file
There are other tools that could cut the MAC address out of ifconfig's output, of course. I just like Perl.
- 26,740
- 12
- 88
- 84
Using ip -br link show eth0 will print something like this:
$ ip -br link show eth0
eth0 UP 85:e2:62:9c:b2:02 <BROADCAST,MULTICAST,UP,LOWER_UP>
You only need the 3rd column, so:
$ ip -br link show eth0 | awk '{ print $3 }'
85:e2:62:9c:b2:02
$ ip -br link show eth0 | awk '{ print $3 }' > file
- 2,030
- 14
- 32