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
.
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
.
-
2The corresponding one-liner:
arp -n | grep `route -n | awk '/UG/{print $2}'` | awk '{print $3}'
– FalkoJan 9, 2016 at 14:57
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. Mar 3, 2019 at 8:47
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/0
returns something like:
default via 192.168.0.1 dev eth1 proto static metric 100
- 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 of192.168.0.1
with192.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
-
2A bit more explanation would help the rest of us understand your script! Mar 22, 2017 at 12:26
-
1For the last line there's a Bash feature that expands a variable to upper case:
echo ${info[5]^^}
Mar 22, 2017 at 12:54 -
1@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– GriefMar 22, 2017 at 13:12
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?– WilfApr 4, 2015 at 16:52
-
2If you ping your router (say
ping 192.168.0.1
) you should then have its MAC address on the arp cache...– RmanoApr 4, 2015 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. Apr 4, 2015 at 17:29
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]'