How to get the MAC address of the 'local' system given the IP address?

Tom H tomh0665 at gmail.com
Sat Feb 1 13:46:38 UTC 2020


On Fri, Jan 31, 2020 at 12:26 PM Little Girl <littlergirl at gmail.com> wrote:
> Ken D'Ambrosio wrote:
>>
>> I humbly submit you should *not* use "hostname -i":
>> ken at strider ~ $ hostname -i
>> 127.0.1.1
>>
>> Not sure where it pulls that from, but localhost is probably not
>> what you're really wanting, so unless you understand the mechanics,
>> there's more than one way to write a script.
>
> It's case-sensitive. A lower-case i (hostname -i) returns localhost
> and an upper-case I (hostname -I) returns the LAN IP.

"-i" returns the addresses associated with a system's hostname.

"-I" returns all the addresses associated with a system.


> Here's another way to get the LAN IP by using the ip command:
>
> ip route | grep kernel | cut -f1 -d'/'
>
> I also had that command in my notes written this way, but can't
> remember why:
>
> ip route | grep kernel | cut -f1 -d' ' | cut -f1 -d'/'
>
> Both give the same result on my computer, but maybe only one will
> work on certain systems or networks.

"ip r | grep kernel | cut -f1 -d'/'" works because the network address
is the first record.

"ip r | grep kernel | cut -f1 -d' ' | cut -f1 -d'/'" is the "proper"
way because it would be extensible if the network address were the
2nd/3rd/... record (for the 2nd, you'd use "... cut -f2 -d' ' ...".




More information about the ubuntu-users mailing list