Mac OS X Terminal Equivalent for Host Name Info

command linehostnameosx

In Linux, using a Bash Terminal, I can do:

  • hostname -d to display the name of the DNS domain, and
  • hostname -i to display the network address(es) of the hostname .

How can I retrieve the same information–preferably using a single command (with option, if needed), and without having to elevate privileges–from a Bash terminal in Mac OS X?

For reference, here's the Bash Version I'm using in Mac OS X:

  • GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin15) .

Best Answer

For the hostname -d, use hostname -f:

hostname -f | sed -e 's/^[^.]*\.//'

For IP-addresses, use ifconfig -a (look for the inet data). Your machine may have only one network device, en0, so you could do just

ifconfig en0 |awk '/inet / {print $2; }'

If you are interested in all of the network devices, keep in mind that ifconfig -l lists the devices. This lists the devices and their correspond addresses:

#!/bin/sh
for name in $(ifconfig -l)
do
    ifconfig $name |awk -v name=$name '/inet / {printf "%s: %s\n", name, $2; }'
done

Further reading:

Related Question