Kiến Thức Linux

How to Edit Hosts File on Mac

How to edit hosts file on Mac.

Introduction

Hosts is a text file used for network configuration in macOS and other Unix-like operating systems. Administrators edit the hosts file to control domain and hostname resolution on the system manually.

This article will show you how to edit the hosts file on Mac.

How to edit hosts file on Mac.How to edit hosts file on Mac.

Prerequisites

  • Administrative access to the system.
  • Access to the Terminal.

What Is Mac Hosts File?

The hosts file is a local implementation of the Domain Name System (DNS) that instructs the OS on how to resolve host queries. It lets users manually map IP addresses to any local and public host or domain name.

When a program attempts to reach a host, macOS checks the hosts file before querying a DNS server. If the file contains the relevant mapping, the system skips the DNS check and resolves the host.

Why Edit Mac Hosts File?

Editing the hosts file is frequently performed in network configuration and application development. The most common reasons to edit the file include:

  • Testing apps locally. The hosts file allows users to map domain names to local IP addresses, which is useful when testing an application before going live.
  • Creating server aliases. Custom, easy-to-remember names can simplify access to servers in a development environment.
  • Load balancer testing. Developers can test the load-balancing capabilities of their app by mapping a single domain to multiple IP addresses and monitoring how the app handles traffic requests.
  • Overriding public DNS entries. When testing DNS configuration changes, the hosts file provides a way to temporarily override relevant DNS entries without affecting the system.
  • Blocking access to websites. If you map a public domain name to the localhost IP address, the website becomes inaccessible, which helps with limiting which websites can be visited.

How to Edit Mac Hosts File

The simplest way to edit the hosts file is by using the Mac Terminal app. Follow the steps below to locate, open, and edit the hosts file in macOS.

Step 1: Open Terminal

Open Terminal by searching for it in Spotlight Search (Command + Space), or follow the steps below to run it from Finder:

1. Open Finder by selecting its icon in the dock.

The location of the Finder icon in macOS.The location of the Finder icon in macOS.

2. Select Applications in the menu on the left side of the window.

3. Expand the Utilities folder.

4. Select Terminal from the list of applications.

Finding the terminal app in macOS.Finding the terminal app in macOS.

Step 2: Open Hosts File

Open the hosts file using a Terminal text editor, such as Nano:

sudo nano /private/etc/hosts

The file already contains some default mappings.

The look of the hosts file in the nano editor.The look of the hosts file in the nano editor.

Step 3: Edit Hosts File

Edit hosts by adding your mappings to the bottom of the file. Use the following syntax:

[ip-address] [hostname-or-domain-name]

Note: You can insert any number of empty spaces between the IP address and the domain.

The example below tells the system to go to the IP address 192.168.0.12 every time there is a request to visit the test.local domain.

Editing the hosts file in macOS.Editing the hosts file in macOS.

Save the file and exit. The configuration changes are applied automatically, and there is no need to restart the system.

Note: You cannot map a port number in the hosts file. To resolve to a specific port, create a reverse proxy server.

Conclusion

This article introduced the hosts file on Mac and provided steps for editing it. After reading the tutorial, you should know how to configure domain name resolution on your system manually.

Continue setting up your Mac by reading How to Set Environment Variables in macOS.

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !