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 Answers 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:

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

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. Your suggestion doesn't quite do what I'd like.
    – marshki
    Oct 23, 2016 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, 2016 at 0:06
  • I could, I suppose parse out the info by looking at lo.
    – marshki
    Oct 26, 2016 at 0:15
  • It's not a complicated script. Oct 26, 2016 at 0:20
  • Agreed. I was just looking for Mac OS X equivalents that didn't involve regular expression, if possible.
    – marshki
    Oct 26, 2016 at 0:27

I am using

scutil --dns | grep 'search domain' | head -n 1 | grep -o '[a-zA-Z0-9.-]\+$'

which seems to work under some circumstances better but YMMV. I make this an alias in my profile (for mac only) to dnsdomainname which works on Linux (in place of hostname -d).

ipconfig getifaddr $(route get | awk '/interface: / {print $2; }')

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. May 23, 2018 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.