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.
The hosts file is for DNS resolution. DNS resolve names to IP addresses, and has nothing to do with ports I am afraid. You will need to use something else in conjunction with the hosts file to redirect the port (Mangle the TCP header by altering the destination port). With iptables: Does MAC OS use iptables / netfilter (I didn't think it did).?
- Normally your Mac doesn’t find what it’s looking for in the hosts file and quickly moves on to resolving the web address via the DNS (Domain Name System) server. With a little tinkering you can use the hosts file to make sure your Mac doesn’t resolve certain URLs via the DNS server.
- The easiest way to access Host file editing is to use a command line, which in Mac OS is called “Terminal”. You can run “Terminal” utility through the Finder, it is located in the list of standard programs of the operating system, or you can just use a search and type its name.
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/.
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.
How To Add Host Entry On Mac
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.
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 >
HostsNews blog & Updates |
Blocking Unwanted Connections with a Hosts File | ||||||||||
What it does ... You can use a modified HOSTS file to block ads, banners, 3rd party Cookies, 3rd party page counters, web bugs, and even most hijackers and possibly unwanted programs. This is accomplished by blocking the connection(s) that supplies these little gems. The Hosts file is loaded into memory (cache) at startup, so there is no need to turn on, adjust or change any settings with the exception of the DNS Client service (see below). Windows automatically looks for the existence of a HOSTS file and if found, checks the HOSTS file first for entries to the web page you just requested. The 0.0.0.0 (prefix) is considered the location of your computer, so when an entry listed in the MVPS HOSTS file is requested on a page you are viewing, your computer thinks 0.0.0.0 is the location of the file. When this file is not located it skips onto the next file and thus the ad server is blocked from loading the banner, Cookie, or some unscrupulous tracker, or javascript file. Example - the following entry 0.0.0.0 ad.doubleclick.net blocks all files supplied by that DoubleClick Server to the web page you are viewing. This also prevents the server from tracking your movements. Why? ... because in certain cases 'Ad Servers' like Doubleclick (and many others) will try silently to open a separate connection on the webpage you are viewing, record your movements then yes ... follow you to additional sites you may visit. Using a well designed HOSTS file can speed the loading of web pages by not having to wait for these ads, annoying banners, hit counters, etc. to load. This also helps to protect your Privacy and Security by blocking sites that may track your viewing habits, also known as 'click-thru tracking' or Data Miners. Simply using a HOSTS file is not a cure-all against all the dangers on the Internet, but it does provide another very effective 'Layer of Protection'. In case you're wondering ... this all happens in microseconds, which is much faster than trying to fetch a file from half way around the world. Another great feature of the HOSTS file is that it is a two-way file, meaning if some parasite does get into your system (usually bundled with other products) the culprit can not get out (call home) as long as the necessary entries exist. This is why it's important to keep your HOSTS file up to Date. Get notified of MVPS HOSTS updates. Special Note: new Windows 10 users ... the MVPS Hosts file installs just fine, no need to make any changes. Simply follow the instructions for Windows 10/8
Started providing a HOSTS file in 1998 ... and now celebrating 20 yrs. proudly still the #1 rated HOSTS file on Google ...
This download includes a simple batch file (mvps.bat) that will rename the existing HOSTS file to HOSTS.MVP then copy the included updated HOSTS file to the proper location. For more information please see the Windows version that applies to you ...
When you run the (mvps.bat) batch file - right-click and select: Run as Administrator. Once updated you should see another prompt that the task was completed. Some users may see a pop-up from certain Security programs about changes to the HOSTS file. Allow the change ... however if you see this pop-up (changes to the HOSTS file) at any other time ... investigate.
Reproduction of information on this site, in any form, is prohibited without express written permission.
Mac Edit Host File TerminalCopyright © 1998 - 2020 All rights reserved. |