Source: iMore
If you're setting up a new webserver or another device that's going to be connected to the internet and you want to put it through its paces before it's live, or if you're having trouble with spyware and adware networks, there's an invisible file on your Mac that can help. It's called the Hosts file, and this is how to use it.
- So I've set up virtual hosts for my Mac and I do all my development locally on my computer. I have a vhost folder in my Sites folder, and I have a list of folders, such as: recipes.cam database.cam sandbox.cam codeigniter.cam and in my /etc/hosts file I have an entry for each one.
- How to Edit the Mac Hosts File. There are actually two ways to edit the Mac Hosts file. You can use Terminal or a standard text editor. The method you use is up to you. Using the Terminal. The simplest way to edit the Hosts file is by using the built-in UNIX terminal in OS X or macOS.
- The hosts file is a system file, you cannot modify or configure it as a simple user. You need administrator level permission to make the changes, therefore, run Notepad as an administrator. Do this if you are working with Windows Vista or higher, but simply open the Notepad if you have Windows XP or anything before that.
- Aug 30, 2018 How to Edit Hosts File on Mac. Whether you want to control network traffic or access to websites or thwart spyware, you will need to edit Hosts file on Mac. Thanks to Terminal application, configuring the host file is a bit easier on macOS. Read on to know how it works!
Oct 04, 2017 Allow the vhosts configuration from the Apache configuration file httpd.conf. Open the httpd.conf. Sudo nano /etc/apache2/httpd.conf. Search for ‘vhosts‘ and uncomment the include line # Virtual hosts Include /private/etc/apache2/extra/httpd-vhosts.conf Edit the vhosts.conf file. Open this file to add in the virtual host.
The Domain Name System
When you type in the domain name of a web site you want to visit there's a lot happening behind the scenes. Every web site, every service, almost every device connected to the Internet has a unique numeric address that tells all the other devices where it is — its TCP/IP address. The Domain Name System (DNS) translates those numeric addresses into something a bit more recognizable and memorable to humans, like, 'www.imore.com' for example.
![Configuring My Hosts File For Https Macos Configuring My Hosts File For Https Macos](/uploads/1/2/6/3/126385418/724351722.png)
The first time you type in a web address, your Mac pings a DNS server — typically one automatically configured for you by your Internet Service Provider — to find out the TCP/IP address of the server you're trying to connect to. Your Mac builds up a hidden cache file to remember those details later on when you visit the same site again.
The Hosts file
The Domain Name System and its associated cache is your Mac's standard way of knowing how to get to where it's going on the Internet, but there's another file that can be very useful. It's called the Hosts file, and it can be used to override the default DNS information.
There are some practical reasons why you'd want to use the Hosts file instead of just letting DNS do its thing. Let's say you're testing a development server you're about to deploy, and you'd like to use its domain name instead of the machine's specific IP address. Before the system's online and accessible to anyone using DNS, you can use the Host file instead: Enter the machine's IP address and when you use that domain name, your Mac will go to that device instead.
You can also use the Hosts file to block or work around spyware and ad networks by 'zeroing out' their IP addresses - putting in 0.0.0.0 then the name of the domain you'd like to block.
How to edit the Hosts file
You'll need to know the IP address of the device you'd like to send your Mac to, or the domain names you're trying to keep your Mac away from. The best way to work with the Host file is by using the Terminal application found in your Mac's Utilities folder. You can search for Terminal using Spotlight or by selecting it in Finder.
- Open a Finder window.
- Select Applications from the sidebar.Source: iMore
- Double-click on Utilities.
- Double-click Terminal.Source: iMore
In the Terminal window, you will need to enter a command to open the Nano text editor. You will need your administrator password, as well.
- type sudo nano /etc/hosts and then hit return.
- Enter your administrator password and then hit return.Source: iMore
You're now in the Nano text editor. You should see something that looks like this:
If you want to add a new device or domain, move the cursor using the arrow keys and position it after the text in the window. Then, begin typing.
If you're mapping a particular IP address on your local network to a domain, you can type the IP address, hit tab, then type the domain name.
Conversely, if you'd like to make sure a web URL doesn't go to its intended site — if you're trying to keep your Mac away from certain sites, use '127.0.0.1.' That'll map it back to your Mac. Even if your Mac is assigned a different IP address by its router, 127.0.0.1 defaults to the local machine thanks to the default settings in that hose file.
Once you're done, hold down the control and O keys to save the file, then control and X to exit.
One last step
Back at the command line, type sudo killall -HUP mDNSResponder then type return. That will flush your Mac's DNS cache, so it doesn't get confused by any changes you've made to the Hosts file.
Don't forget you've modified the Hosts file, because at some point you may need to undo the changes you've made in order to keep your Mac working right.
How to reset your Mac's Hosts file
When you're ready to undo your changes and restore where your Mac's Hosts file routes to, you can do so by replacing the current host file with a new set of terms. You don't need Terminal for this reset.
- Make sure Finder is selected on your Mac.
- Click on Go in the Mac menu bar.
- Click on Go to Folder.
- Enter /private/etc/hosts into the search field.
- Click Go.
- Drag the Hosts file from the Finder window onto your desktop.
- Open the Hosts file from your desktop. It should open to TextEdit by default.
- Delete the content of the Hosts file.
- Paste the following into the same file:
- Click Save.
- Drag this same Hosts file from your desktop back into the same folder. If you don't have the folder open in finder anymore, follow steps one through five to reopen it.
- Click Replace when asked whether you want to replace the file.
- Enter your Administrative password when prompted (that's the password used by the main user to unlock their Mac).
- Restart your Mac.
Have any questions? Let me know.
Updated February 2020: Updated for macOS Catalina.
macOS Catalina
Main
We may earn a commission for purchases using our links. Learn more.
Up and upAnalyst says iPhone 12 will cost more even without charger and headphones
Jeff Pu, an analyst at Chinese research firm GF Securities, predicts that Apple will raise the price of the iPhone 12 by at least $50.
Prerequisites
Make sure that the following prerequisites are met:
- You have a stable Internet connection.To operate with Docker you need the busybox image be available on your machine. Ensure that you have a stable Internet connection, so that PyCharm can download and run
busybox:latest
. Once you have successfully configured Docker, you can go offline. - Docker is installed. You can install Docker on the various platforms, but here we'll use the Windows installation.Note that you might want to repeat this tutorial on different platforms; then use Docker installations for macOS and Linux (Ubuntu, other distributions-related instructions are available as well).
- Before you start working with Docker, make sure that the Docker plugin is enabled. The plugin is bundled with PyCharm and is activated by default. If the plugin is not activated, enable it on the Plugins page of the Settings/Preferences dialog Ctrl+Alt+S as described in Manage plugins.If you are using Docker for Windows, enable the Expose daemon on tcp://localhost:2375 without TLS option in the General section of your Docker settings.
Preparing an example
We could have actually repeated the same example as was used for Docker, but for Docker Compose it makes no sense - too simple...
To show a realistic example of a Docker Compose application, we'll use a Django application with a PostgreSQL database running in a separate container. Get the project from GitHub, and open it in PyCharm (File | Open).
![Hosts file windows 10 Hosts file windows 10](/uploads/1/2/6/3/126385418/608236938.jpg)
For this Django application, we should create two containers: one for a database, and one for the application itself. We'll use the Docker Compose to link the two containers together.
Adding files for Docker and Docker Compose
In the Project tool window, right-click the project root and choose New | FileAlt+Insert, enter the filename (here Dockerfile) and enter the following code:
FROM python:3.6.7WORKDIR /app# By copying over requirements first, we make sure that Docker will cache# our installed requirements rather than reinstall them on every buildCOPY requirements.txt /app/requirements.txtRUN pip install -r requirements.txt# Now copy in our code, and run itCOPY . /appEXPOSE 8000CMD ['python', 'manage.py', 'runserver', '0.0.0.0:8000']
Next, repeat the same steps for the docker-compose.yml file and enter the following code:
version: '2'services: web: build: . ports: - '8000:8000' volumes: - .:/app links: - db db: image: 'postgres:9.6' ports: - '5432:5432' environment: POSTGRES_PASSWORD: hunter2
Let's look at the docker-compose.yml file. This file defines 2 services:
web
and db
, and links them together. Configuring Docker
Now that we've prepared our example, let's configure Docker.
To do that, open Settings dialog (Ctrl+Alt+S or click on the main toolbar) and click the Docker page under the Build, Execution, Deployment node. Click to create a Docker server.
Accept the suggested default values:
For macOS, select Docker for Mac to connect to the Docker daemon.
The Path mappings settings are not available on Linux. So, if you want to map some directories on a virtual machine to some path on your local Linux machine, you will have to do it manually.
Next, apply changes.
Configuring Docker Compose as a remote interpreter
Let's now define a remote interpreter based on Docker-Compose.
Ensure that you have downloaded and installed Python on your computer.
Open the Add Python Interpreter dialog by either way:
- When you're in the Editor, the most convenient way is to use the Python Interpreter widget in the . Click the widget and select Add Interpreter ...
- If you are in the Settings/Preferences dialog Ctrl+Alt+S, select Project <project name> | Project Interpreter. Click the icon and select Add.
In the dialog that opens, select the Docker Compose option, from the drop-down lists select the Docker server, Docker Compose service (here
web
), configuration file (here docker-compose.yml
)and image name (here python
).Why we've chosen
web
? This choice is explained by the fact, that after configuring a Docker-Compose-based interpreter, we'll be able to create regular run configurations that will alter the behavior of the container we selected. Therefore, if we want to debug the code in a container, that's the one we should select here. All other containers in the compose file will always be started together with this one, but you won't be able to affect their behavior from PyCharm - they'll always behave as if you started them with the command docker-compose up
from the command line.Next, wait while PyCharm starts your Docker-Compose configuration to scan and index:
Using the Docker tool window
Since we've configured Docker, the Services tool window button appears at the bottom of PyCharm's main window. Click this button and see your container running:
Configuring database credentials
Modify the
DATABASES
section of the settings.py file in your Django project to add database configuration details:DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'postgres', 'USER': 'postgres', 'PASSWORD': 'hunter2', 'HOST': 'db' }}
Running your application under Docker-Compose
First, as we are executing a Django application, we must run a migration.
To do that, choose Tools | Run 'manage.py' task and enter
migrate
:(See Run tasks of manage.py for details.)
Next, create an ordinary Django server run/debug configuration. To do that, from the main menu choose Run | Edit Configurations...; in the dialog that opens click and select Django Server:
The only thing you should pay attention to, is that Host field must be set to 0.0.0.0 - to make sure that we listen to requests coming from outside the Docker container.
Launch this configuration (Run | Run 'RunDjangoApp'):
To see output in your web browser, go to http://localhost:8000 (in the address bar, change 0.0.0.0 to localhost):
If you are using the Docker Machine, use the machine's IP address instead.
Summary
Mvps Hosts
Let's summarize what has been done with the help of PyCharm:
Hosts File Ads
- We downloaded a Django application from GitHub and opened it.
- We added specific Docker Compose files to our project.
- We configured a remote interpreter based on Docker Compose.
- We ran our Django application in the Docker Compose container.