10

Is there a Linux command that allows me to get the MAC address of my router?

  • 1
    which one? your router should have multiple mac addresses for both sides of communication (incoming / outgoing). – mchid Apr 4 '15 at 22:43
  • Should this go in Super User? – user371765 Apr 7 '15 at 1:11
4

I like one-liners:

arping -f -I $(ip route show match 0/0 | awk '{print $5, $3}')

arping shows the MAC associated with the default gateway IP address from the output of ip route show match 0/0, parsed by awk.

  • I tried this (and other answers). They give good info, but the MAC address shown was off by two digits from what I was looking for. My printer lists two routers with the same SSID (probably guest and regular), and wants me to pick by MAC address. This answer askubuntu.com/a/222553 gives me one of the two listed, with the iwconfig | grep "Access Point" command. I think the one that was off by two was the cached version of the wired connection, which I unplugged to try to make sure I was getting the wireless route. – hlongmore Mar 3 at 8:47
8

Don't use the obsoleted commands ifconfig(8), arp(8) or route(8). Use the new command that replace them and can do more, ip(8).

Use ip route list to see which default router your machine have. That should be a line wich starts with default (or 0.0.0.0) and have the IP address to the router after. If you uses IPv6, just add the -6 switch, ip -6 route list.

default via 192.168.11.1 dev eth0  proto static 

To see the MAC address of the default routers IP address, use ip neigh and look up the line with the IP address and MAC address after lladdr.

192.168.11.1 dev eth0 lladdr 1c:af:f7:XX:XX:XX REACHABLE
4

If you don't know the IP of your router, it's most likely your gateway which you can get from the route command:

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 eth0
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

Note the line with the flags UG. The address in the Gateway column of that line is what you're looking for. Then follow 2707974's suggestion with arp -n (ping the IP if it doesn't show up at first), and find the matching line:

$ arp -n
Address                  HWtype  HWaddress           Flags Mask            Iface
192.168.0.1              ether   00:11:22:33:44:55   C                     eth0
192.168.0.2              ether   66:77:88:99:aa:bb   C                     eth0

Here, your router's MAC would be 00:11:22:33:44:55.

  • 2
    The corresponding one-liner: arp -n | grep `route -n | awk '/UG/{print $2}'` | awk '{print $3}' – Falko Jan 9 '16 at 14:57
3

Here is one-liner which works in dash, bash and zsh:

ip neigh|grep "$(ip -4 route list 0/0|cut -d' ' -f3) "|cut -d' ' -f5|tr '[a-f]' '[A-F]'
  1. ip -4 route list 0/0 returns something like:

    default via 192.168.0.1 dev eth1 proto static metric 100

  2. we get IP from that line as third field with cut and grep line containing that IP and immediate space after it from the output of network neighborhood. (space is required to avoid matching of 192.168.0.1 with 192.168.0.10), the matched line would be something like:

    192.168.0.1 dev eth1 lladdr ca:fe:ba:be:be:af REACHABLE

  3. Now we getfifth field and make it uppercase:

    CA:FE:BA:BE:BE:AF

  • 1
    A bit more explanation would help the rest of us understand your script! – George Udosen Mar 22 '17 at 12:26
  • 1
    For the last line there's a Bash feature that expands a variable to upper case: echo ${info[5]^^} – David Foerster Mar 22 '17 at 12:54
  • @DavidFoerster I wanted to write a portable command but the original variant relied on array indexing specific to zsh (from one) so I've updated the answer with the variant portable between three most popular shells – Grief Mar 22 '17 at 13:12
2

Is not a complete solution, but you check arp -n.

ddd@mmm ~ $ arp -n
Address                  HWtype  HWaddress           Flags Mask            Iface
xxx.xxx.xxx.xxx          ether   00:e0:1e:b4:12:42   C                     eth0
yyy.yyy.yyy.yyy          ether   00:14:78:52:28:d2   C                     wlan0
  • Doesn't this get the MAC address of the interfaces on the computer, not the router? – Wilf Apr 4 '15 at 16:52
  • 2
    If you ping your router (say ping 192.168.0.1) you should then have its MAC address on the arp cache... – Rmano Apr 4 '15 at 16:59
  • thanks @2707974 but I don't khnow no mac address no address ip and I want have a command that give me only the ip adress or mac adress of my router because I want to use the command in a script python for that show all arp table is not useful for me. – user4650183 Apr 4 '15 at 17:29
1

This is an improved version of Grief's answer. It is possible for ip -4 route list 0/0 to return more than one line (IP), in which case the complete one liner doesn't work. So the following modified version only uses the first line that ip -4 route list 0/0 returns.

ip neigh|grep "$(ip -4 route list 0/0|head -1|cut -d' ' -f3) "|cut -d' ' -f5|tr '[a-f]' '[A-F]'

protected by Community Jul 9 '18 at 12:13

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.