4

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) .
4

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:

  • hostname -d returns the name of the DNS domain, e.g. : department.business.com. Also, hostname and hostname -f are functionally the same in OS X. hostname -i returns a network address (IP), e.g. 127.0.0.1. Your suggestion doesn't quite do what I'd like. – marshki Oct 23 '16 at 15:41
  • Using hostname -f with sed is an acceptable solution, as it parses out the DNS domain. Using ifconfig, though, returns the IP address, not the host IP. – marshki Oct 26 '16 at 0:06
  • I could, I suppose parse out the info by looking at lo. – marshki Oct 26 '16 at 0:15
  • It's not a complicated script. – Thomas Dickey Oct 26 '16 at 0:20
  • Agreed. I was just looking for Mac OS X equivalents that didn't involve regular expression, if possible. – marshki Oct 26 '16 at 0:27
0
ipconfig getifaddr $(route get 8.8.8.8 | awk '/interface: / {print $2; }')
-1

Use 'dig'. Search for commands similar to whois

dig hostname => both hostname + ipaddy

dig ip.address => not as complete

  • These commands seem to assume that you already know the hostname and/or IP address, while the question is asking for ways to get this information.  If I’m misunderstanding what you’re suggesting, please clarify.  Please do not respond in comments; edit your answer to make it clearer and more complete. – G-Man Says 'Reinstate Monica' May 23 '18 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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