Skip to content

Instantly share code, notes, and snippets.

@MelonSmasher
Last active February 3, 2016 01:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MelonSmasher/1fdcbb4d8e2f13b231a5 to your computer and use it in GitHub Desktop.
Save MelonSmasher/1fdcbb4d8e2f13b231a5 to your computer and use it in GitHub Desktop.
Edit Hosts File On OS X

Add Entry To Hosts File

  • To edit to your hosts file start by opening your terminal.
  • Then run the following command:
sudo nano /etc/hosts
  • When you are prompted for your password enter it.
  • Nano is a commandline text editor and it will open up your hosts file.
  • Add your hosts entries to the file, it should look something like this:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##


127.0.0.1	localhost
255.255.255.255	broadcasthost
::1             localhost
fe80::1%lo0	localhost

10.10.10.10     new-host-1.domain.tld   new-host-1
192.168.1.50    new-host-2.domain.tld   new-host-2
  • Save the file by pressing Ctrl + o and then exit nano by pressing Ctrl + x.
  • Clear your DNS cache by entering the following command into your terminal:
dscacheutil -flushcache
  • Finally ping each new host and verify that it resolves to the IP address that you defined in your hosts file:
ping new-host-1.domain.tld

Remove Entry From Hosts File

  • To edit to your hosts file start by opening your terminal.
  • Then run the following command:
sudo nano /etc/hosts
  • When you are prompted for your password enter it.
  • Nano is a commandline text editor and it will open up your hosts file.
  • Place a # before the entries that you wish to remove. The # symbol turns those lines into comments, rather than host entries. You should end up with something that looks like this:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##


127.0.0.1	localhost
255.255.255.255	broadcasthost
::1             localhost
fe80::1%lo0	localhost

#10.10.10.10     new-host-1.domain.tld   new-host-1
#192.168.1.50    new-host-2.domain.tld   new-host-2
  • Save the file by pressing Ctrl + o and then exit nano by pressing Ctrl + x.
  • Clear your DNS cache by entering the following command into your terminal:
dscacheutil -flushcache
  • Finally ping each custom host and verify that it does not resolve to the IP address that you had defined in your hosts file:
ping new-host-1.domain.tld
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment