How to Locate and Edit Hosts File on a Mac Using Terminal

Estimated read time 2 min read

The hosts file in an operating system is used to convert domain names to IP addresses. Users can enter their hosts files and go to the IP addresses they define themselves, instead of replies from the DNS server.

Mac users can find this file in the /private/etc/hosts folder. If you are a user with root privileges, you can edit this file in any text editor.

How to make changes to the hosts file?

  • Let’s open the hosts file

Let’s open Spotlight with the help of Command + Spacebar keys. Let’s open the terminal application by typing “Terminal” in the search box.

  • Let’s open the hosts file with the nano editor from the command line.

When you enter the command, it will ask for your password.

sudo nano /private/etc/hosts

Below is the opened file view.

You can save it by making any additions you want to the bottom of this file. For example, let’s say your server at home has a fixed IP address of and you want it to go to this IP address when you type instead of constantly typing this address into your web browser.

  • In this case, you should add the following line to the hosts file.

You can save it with Ctrl+O and exit the nano editor with Ctrl+X.

  • Flush DNS Cache

In order for your changes to take effect, you must clear the DNS temporary records kept on your computer. The following commands will suffice for this.

macOS Monterey, Big Sur

sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

macOS Catalina, Mojave, High Sierra, Sierra, Mountain Lion, Lion

sudo killall -HUP mDNSResponder


In this article, we have made our own IP address definition instead of the results from the DNS server by editing the hosts file. In this way, we explained that even if we do not have a public IP address, we can use our own home server in our system by entering the hostname instead of the IP address.

İbrahim Korucuoğlu

Yazar, bilişim ve teknoloji alanında derlediği faydalı içerikleri bu blogta paylaşmaktadır.