Managing Local Hostnames on macOS with addhost

Tutorial

On this page

Giving each local October CMS project its own hostname, such as myproject.test, keeps their addresses, cookies and sessions apart, and lets you test a Multisite project with a domain for each site. A hostname like this points to your own computer through an entry in the /etc/hosts file. addhost is a small shell script for macOS that adds, removes and lists these entries with a single command, instead of editing the file by hand.

The script was written by Łukasz Kosma of Pear Group and published at github.com/pearpl/addhost under the MIT License. It's copied in full below, along with its license.

Back Up the Hosts File

The script changes your /etc/hosts file, so make a copy of it before you start.

sudo cp /etc/hosts /etc/hosts.backup.$(date +%s)

Install the Script

Create a bin directory in your home directory, if it doesn't exist yet, and save the script below to ~/bin/addhost.sh.

mkdir -p $HOME/bin
#
# addhost.sh - Simple hosts file manager for macOS
# Author: Łukasz 'Alien' Kosma
# Homepage: https://pear.pl
# GitHub: https://github.com/pearpl
# Version: 1.0
# License: MIT
#
# Description:
# This script allows you to add, remove, and list hostnames in the /etc/hosts file.
# It is designed to work on macOS and requires sudo privileges to modify the hosts file.
#
# Setup:
# 1. Save this script to a location like $HOME/bin/addhost.sh
#    (you can create the bin directory if it doesn't exist)
#
# 2. Make it executable:
#    chmod +x $HOME/bin/addhost.sh
#
# 3. Add this line to your ~/.bash_profile or ~/.zshrc:
#    alias addhost="$HOME/bin/addhost.sh"
#
# 4. Reload your shell configuration:
#    source ~/.bash_profile     (or source ~/.zshrc)
#
# Then you can use it like a command:
#   addhost add <hostname>    # Add a hostname to 127.0.0.1
#   addhost rm <hostname>     # Remove a hostname
#   addhost list              # List all hostnames
#   addhost help              # Show help
#   addhost ver         # Show version
#

#!/bin/bash

RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'

COMMAND="$1"
HOSTNAME="$2"
IP="127.0.0.1"
HOSTS_FILE="/etc/hosts"

list_hosts() {
    grep -vE '^#|^\s*$' "$HOSTS_FILE" | grep "$IP" | awk '{print $2}'
}

case "$COMMAND" in
    add)
        if [ -z "$HOSTNAME" ]; then
            echo "Usage: addhost add hostname"
            exit 1
        fi
        if ! grep -q "$HOSTNAME" "$HOSTS_FILE"; then
            echo "$IP $HOSTNAME" | sudo tee -a "$HOSTS_FILE" > /dev/null
            echo -e "${GREEN}Hostname $HOSTNAME added to $HOSTS_FILE${NC}"
        else
            echo -e "${RED}Hostname $HOSTNAME already exists in $HOSTS_FILE${NC}"
        fi
        ;;
    rm)
        if [ -z "$HOSTNAME" ]; then
            echo "Usage: addhost rm hostname"
            exit 1
        fi
        sudo sed -i "" "/$HOSTNAME/d" "$HOSTS_FILE"
        echo -e "${RED}Hostname $HOSTNAME removed from $HOSTS_FILE${NC}"
        ;;
    list)
        echo -e "${GREEN}Existing hostnames mapped to $IP:${NC}"
        list_hosts
        ;;
    ver)
        echo "addhost.sh version 1.0"
        exit 0
        ;;
    help|*)
        echo ""
        echo -e "  ${GREEN}Usage:${NC}"
        echo ""
        echo -e "  ${GREEN}addhost add <hostname>${NC}    Add a hostname to 127.0.0.1"
        echo -e "  ${GREEN}addhost rm <hostname>${NC}     Remove a hostname"
        echo -e "  ${GREEN}addhost list${NC}              List all hostnames"
        echo -e "  ${GREEN}addhost help${NC}              Show this help message"
        echo -e "  ${GREEN}addhost ver${NC}         Show version"
        echo ""

        ;;
esac

Make the script executable.

chmod +x $HOME/bin/addhost.sh

Add an alias for it to your shell's configuration file, which is ~/.zshrc for the Z shell that macOS uses by default, or ~/.bash_profile for Bash.

alias addhost="$HOME/bin/addhost.sh"

Then reload the configuration, or open a new terminal window.

source ~/.zshrc

Using addhost

The script points hostnames to 127.0.0.1, your own computer. Adding and removing hostnames asks for your password, as the hosts file can only be changed with sudo.

Command Description
addhost add <hostname> Adds a hostname
addhost rm <hostname> Removes a hostname
addhost list Lists the hostnames that point to 127.0.0.1
addhost help Shows the usage information
addhost ver Shows the script's version

For example, add a hostname for a project, check the list, and remove it again when the project is finished.

addhost add myproject.test
addhost list
addhost rm myproject.test

Tip: Use the .test ending for local hostnames rather than .local. On macOS, .local names are also looked up on the local network with Bonjour, which can make every page load seconds slower.

The hosts file only points a hostname to your computer. Your local web server still needs to serve the project at that hostname, for example with a virtual host in Apache or Nginx.

Things to Know

  • Remove the full hostname. Removing deletes every line in the hosts file that contains the text you enter, and a dot matches any character. For example, addhost rm host would also remove the localhost entries macOS needs, so always enter the complete hostname.
  • Adding looks for the text anywhere in the file. If a longer hostname contains the one you add, such as mysite.test when adding site.test, the script reports that it already exists and doesn't add it.
  • The script is for macOS. Removing uses the macOS form of the sed command, which doesn't work on Linux.
  • If a new hostname doesn't open, clear the DNS cache of macOS.
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

See Also

Was this article helpful?

comments powered by Disqus
Still need help?
October Forum

Community members share experiences, ask questions and exchange tips.

Discord

Talk with other October CMS developers in real time.

Custom Development

Hire the October CMS team to work on your website.

Contact Us

For bug reports and help with this website. See the roadmap or submit an idea on the Portal.