How to edit the Host file on MacOS

Pre-requisites:

Root or sudo privileges
Access to a terminal or command line

By modifying the hosts file you can easily preview your website on the local network during the migration of your website to a new server enabling you to establish a staging environment without impacting existing DNS records.

To edit your Mac's host file for testing a LAMP stack,

Press the command and spacebar key to open Terminal by searching it via the spotlight search

Next, open the hosts file with your preferred editor program.

sudo vi /etc/hosts

Edit the hosts file

Type L in vi to move to the bottom of the screen

Type $ to move to the end of the line

Type i to enter insert mode

Use the following syntax at the bottom of the file

IP_Address domain.com

Once you are done editing the hosts file, type the following to save and quit the file

:wq

To flush your Mac's DNS cache, type the following command

For macOS Monterey,

sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

The changes should take effect immediately.

Remember, you should remove the new lines from the hosts file once propagation is done after connecting your domain to web hosting.

Learn

Related articles

Getting Started with React

In this guide, we will cover the basics of setting up a local development environment, starting a simple React project, and the basics of how it works.

How to Create Bash Aliases

This tutorial demonstrates how to use the alias command to create personalized shortcuts, which can help you save time and feel less frustrated.

Using Git for PHP Development

This guide walks through the fundamentals of Git. In this tutorial, we will show you Git commands. For Linux you can use the built-in terminal.

How to Connect to MySQL with Laravel

In this guide, you will learn how to connect your Laravel application to your MySQL database.

How do you change the default SSH Port on CentOS Stream 9?

Changing the default SSH port adds an extra layer of security by reducing the risk of your password being cracked from a brute force attack.

What is Inheritance in PHP?

In this tutorial we will explain inheritance in PHP, a mechanism that allows a child class to inherit properties and behaviors from a parent class.