본문 바로가기

카테고리 없음

Hosts File For Mac Os X

  • The hosts file can be edited to block certain hostnames (like ad-serving/malicious hosts), or used for web development purposes, i.e. To redirect domains to local addresses. Editing the hosts file Editing the hosts file in Mac OS X Leopard, is a pretty easy task, especially if you are familiar with the terminal. Step 1 Open the Terminal.app.
  • The hosts file can be edited to block certain hostnames (like ad-serving/malicious hosts), or used for web development purposes, i.e. To redirect domains to local addresses. Editing the hosts file. Editing the hosts file in Mac OS X – Leopard, is a pretty easy task, especially if you are familiar with the terminal. Step 1 – Open the.
  • Hosts add a preference pane to your system preferences which lets you toggle your host file entries on and off, as well as add and remove them. Free PermanentMarkers Mac OS X 10.8/10.9 Version.

The /etc/hosts file on the Mac can be hard to find. Here are some tips for finding and editing it, and what you can do with it.

Where is the /etc/hosts file in Mac OS X?

The hosts file used by Mac OS X is in /etc/. Windows users sometimes have problems finding it since there’s no %SystemRoot%system32driversetc folder in the Mac OS. UNIX users might not expect /etc/ to be hidden by the Mac OS X Finder, so they often look in /Library/Application Support/.

There are two primary ways to edit the hosts file in Mac OS X. The first is by using TextEdit since the hosts file is a plain text document. However, you can’t open the file directly since it.

Accessing /etc/hosts

/etc/ is hidden by default in the Finder. The easiest way of getting to the mac hosts file is to open /Applications/Utilities/Terminal.app. Then type:
sudo nano /etc/hosts

…you will need to type in your password. Your hosts file will open in the text editor nano.

Why ‘sudo nano’?

The hosts file, in addition to being hidden, is also protected by the system. Using sudo makes the editor nano run as a super-user, which makes editing the file possible.

Adding entries to the /etc/hosts file on Mac

In nano, on a new line type the IP Address followed by at least one space or tab and then the hostname. Examples:
127.0.0.1 mydevsite.local
192.168.1.222 ads.shadycompany.biz

…hit ctrl + o to Save, and then ctrl + x to Quit the text editor.

Background: What is /etc/hosts used for?

The hosts file is a simple way of mapping IP Addresses to hostnames. It overrides mappings provided by the Domain Name System network. The hosts file is commonly used by network administrators to provide access to LAN resources when no name resolution is available. The other common usage is to block access to specified hosts by adding host entries resolving them to — for example — 127.0.0.1.

Another use for the hosts file is for easily allowing your Mac to run multiple virtual servers, mainly useful for web developers. See here for more about using /etc/hosts for Virtual Servers: Adding an entry to /etc/hosts on OS X.

File

More Resources:
http://en.wikipedia.org/wiki/Hosts_(file).

If you find this page useful, check out my ebook. It contains an overhauled version of the hosts file info here, plus useful stuff to do with the Mac Terminal, bash, AppleScript, and Automator. Get to grips with the Mac Terminal and scripting tools now. More Info >

mountaindogmedia left the following comment on my post for installing Apache, PHP, and MySQL on Mac OS X:

Jason, have you tried a modified Include statement for virtual hosts to map a directory? So instead of /etc/apache2/extra/httpd-vhosts.conf as indicated, one would use /etc/apache2/extra/vhosts/*.conf and then just create a default.conf for the first virtual host, and then add/edit/delete vhost files as needed. I think it would be easier to manage host files and changes.

Indeed, mountaindogmedia, this is an easier way. In fact, this is the default configuration for many servers.

By default, the Apache Virtual Host configuration on Mac OS X is located in a single file: /etc/apache2/extra/httpd-vhosts.conf. You need to edit the Apache configuration to include this file and enable virtual hosts.

Over the years, I have created many virtual hosts. Each time editing httpd-vhosts.conf. To mountaindogmedia's point, this becomes difficult to manage. Furthermore, Apache configurations often get reset when upgrading Mac OS X. In the same amount of steps (two), you can adopt a more manageable configuration.

What are Virtual Hosts?

Hosts File For Mac Os X

From the Apache Virtual Host documentation:

The term Virtual Host refers to the practice of running more than one web site on a single machine.

By default, the Apache configuration on Mac OS X serves files from /Library/WebServer/Documents accessed by the name locahost. This is essentially a single site configuration. You could mimic multiple sites by creating subdirectories and access a site at localhost/somesite.

This is not ideal for several reasons. Primarily, we would rather access the site using a name like somesite.local. To do that, you need to configure virtual hosts.

A Cleaner Configuration

Before I being, I assume you already installed and configured Apache on Mac OS X.

First, open the Terminal app and switch to the root user to avoid permission issues while running these commands.

Edit the Apache configuration file:

Find the following line:

Below it, add the following line:

This configures Apache to include all files ending in .conf in the /private/etc/apache2/vhosts/ directory. Now we need to create this directory.

Create the default virtual host configuration file.

Add the following configuration:

I create this file to serve as the default virtual host. When Apache can not find a matching virtual host, it will use the first configuration. By prefixing this file with an underscore, Apache will include it first. Techincally this file is not needed as it simply repeats the configuraton already in httpd.conf. However, it provides a place to add custom configuration for the default virtual host (i.e. localhost).

Now you can create your first virtual host. The example below contains the virtual host configuration for my site. Of course, you will want to substitute jasonmccreary.me with your domain name.

Create the virtual host configuration file:

Add the following configuration:

This VirtualHost configuration allows me to access my site from http://jasonmccreary.local for local development.

Note: I use the extension local. This avoids conflicts with any real extensions and serves as a reminder I am developing in my local environment.

Note: The Require all granted configuration became available in Apache 2.4 which comes with Mac OS X Yosemite. If you are running a version of OS X before Yosemite, use the equivalent 2.2 configuration in the upgrading Apache examples.

The final step is to restart Apache:

If you run into any problems, run:

This will test your Apache configuration and display any error messages.

Mapping the .local extension

In order to access sites locally you need to edit your hosts file.

Add a line to the bottom of this file for your virtual host. It should match the value you used for the ServerName configuration. For example, my site:

I like to run the following to clear the local DNS cache:

Now you can access your site using the .local extension. For example, http://jasonmccreary.local.

A note about permissions

Hosts File For Mac Os X 10.13

You may receive 403 Forbidden when you visit your local site. This is likely a permissions issue. Simply put, the Apache user (_www) needs to have access to read, and sometimes write, to your web directory.

If you are not familiar with permissions, read more. For now though, the easiest thing to do is ensure your web directory has permissions of 755. You can change permissions with the command:

In my case, all my files were under my local ~/Documents directory. Which by default is only readable by me. So I had to change permissions from my web directory all the way up to ~/Documents to resolve the 403 Forbidden issue.

Hackintosh Download

Note: There are many ways to solve permission issues. I have provided this as the easiest solution, not the best.

In Closing

Hosts File For Mac Os

Any time you want to add a site to Apache on your Mac, simply create a virtual host configuration file for that site and map it in your hosts file.

Os X 10.3 Download

Find this interesting? Let's continue the conversation on Twitter.