Even when every network device is disconnected, your computer can still communicate over a kind of network. Known as a “loopback,” a Unix machine can send and receive network communications from itself and to itself over a virtual network device. Your computer can send messages from itself to itself, permitting network-style communications without the need for a functioning network.
What is 127.0.0.1?
127.0.0.1 is the most commonly-used loopback IP address. It’s part of the reserved block of more than sixteen million IP addresses like 192.168.l.l that are used specifically for loopback functionality. A loopback allows your computer to communicate with itself while using networking connection protocols.
In broad abstraction, a loopback is a virtual networking device that creates a network connection with only one endpoint, meaning that it begins and ends at the same device: your computer. In fact, the loopback device even shows up in ipconfig with the name lo, as seen above. The loopback addresses are primarily used for troubleshooting (it’s similar to looking in a mirror) or to access local resources over a network interface.
What is localhost?
“localhost” describes a communication port that connects to the originating server. It allows a network connection to “loop back” on itself, permitting you to emulate network connections when no such network is present or available. In practice, localhost is treated as synonymous with 127.0.0.1 for usage and discussion. However, it’s important to remember that they are not strictly identical.
Before making a DNS request to translate the text string typed by the user into a navigable IP address, the operating system checks the HOSTS file for any aliases or redirect rules. On a system configured to standard defaults, “localhost” in a URI will resolve to 127.0.0.1 under IPv4 or ::1 for IPv6. There are far more loopback addresses than just those two, however. The block of IP addresses reserved for loopback addresses stretches from 127.0.0.0 to 127.255.255.255.
In most all cases localhost will resolve to 127.0.0.1, thanks to a redirect rule in the HOSTS file, as mentioned above. But in some cases, localhost may be mapped to a different IP address. So, localhost could be pointed to any IP address in that block, and it would function identically.
What’s the Difference Between localhost and 127.0.0.1?
On most machines, localhost and 127.0.0.1 are functionally identical. But localhost is a label for the IP address and not the address itself. Localhost could be pointed at different IP addresses. In fact, it could be pointed at any IP address, even one outside the reserved address block. The HOSTS file doesn’t care and won’t stop you. However, it will break significant functionality on your system and crash any app that relies on a localhost connection.
The 127 block of addresses was chosen for the loopback address block because it was the last block of Class A addresses, which run from the binary address value 00000001 to 01111111. In IPv6, the loopback address is the first address, 0:0:0:0:0:0:0:1, most frequently expressed in its truncated form as ::1.
If you’re coming from Windows to a Unix system, you might have noticed that loopback is practically synonymous with localhost. You can use your HOSTS file to make “loopback” redirect to 127.0.0.1, but that’s more of a text replacement than anything else.
Set up localhost on macOS
You’ll need to enable apachectl in Terminal to launch the requisite service for running localhost.
1. Open Terminal.app in /Application/Utilities or Spotlight.
2. Run the following command to start apachctl. You’ll need to enter your password.
sudo apachectl start
3. Type ‘localhost’ to confirm that it works.
Once you have localhost running, you can configure various services to run on the server, including PHP, Perl, STMP, and more. To run this service at boot, you’ll need to create a launch agent for it so that launchd can grab on to the service and launch it at each boot.
You might also be interested in the following posts: