Skip to content

Add project hosts entry on MacOS

Add project hosts entry on MacOS

macOS resolves names from /etc/hosts before DNS. Use it for one-off project names, or use Devilbox auto DNS for every project under your TLD_SUFFIX.

Example project names

Assume .env uses TLD_SUFFIX=loc and you have these project folders:

Project directoryURLHostname to add
project-1http://project-1.locproject-1.loc
www.project-1http://www.project-1.locwww.project-1.loc

Use 127.0.0.1 with Docker Desktop on macOS.

Add entries

Open the hosts file:

Terminal window
sudo nano /etc/hosts

Add one line per hostname:

127.0.0.1 project-1.loc
127.0.0.1 www.project-1.loc

Save the file, then flush the resolver cache:

Terminal window
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder

Verify

Check that macOS returns loopback:

Terminal window
ping -c1 project-1.loc
ping -c1 www.project-1.loc

Expected result:

PING project-1.loc (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.050 ms

Open the site:

Terminal window
open http://project-1.loc

Use auto DNS instead

Start the DNS container and configure macOS once:

Terminal window
./dvl.sh up bind
sudo mkdir -p /etc/resolver
echo "nameserver 127.0.0.1" | sudo tee /etc/resolver/loc

Now every *.loc name resolves through Devilbox.

Remove an entry

Edit the file again, delete the matching line, and flush the cache:

Terminal window
sudo nano /etc/hosts
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder