Is there a Linux command that allows me to get the MAC address of my router?
6 Answers
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.e
192.168.11.1 dev eth0 lladdr 1c:af:f7:XX:XX:XX REACHABLE
If you don't find a machine in the list, you need to connect to it, because it has been removed from the list because not used for a while. So just try:
ping -c 3 192.168.11.1
Then try the ip neigh command again. The option -c 3 will restrict ping to only send three ICMP ECHO messages and then end. If not set, it will run until you stop the program with C-c.
- 1,615
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.
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.
- 55,453
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]'
ip -4 route list 0/0returns something like:
default via 192.168.0.1 dev eth1 proto static metric 100
- we get IP from that line as third field with
cutand grep line containing that IP and immediate space after it from the output of network neighborhood. (space is required to avoid matching of192.168.0.1with192.168.0.10), the matched line would be something like:
192.168.0.1 dev eth1 lladdr ca:fe:ba:be:be:af REACHABLE
- Now we get fifth field and make it uppercase:
CA:FE:BA:BE:BE:AF
- 483
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
- 10,758
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]'
- 21