What do you need a localhost for? Sometimes it can be useful to communicate with yourself. This is known as a loopback in network technology: instead of feeding a request to the network – e.g. the internet – you instead keep it within your system, subsequently stimulating a network connection. We will explain how the connection to 127.0.0.1 works, and what you can use localhost for.Localhost (127.0.0.1)
The hosts file is an extremely useful albeit small text file that stores host names with associated IP addresses. This determines which nodes are accessed in a network. The hosts file is an elementary tool of a network protocol and converts host names into numeric IP addresses. The network can only work with these addresses. The hosts file is created in all common operating systems for PC, Mac, and mobile devices, and can be changed by system administrators. Each time you visit a website, your device first checks the hosts file for a corresponding entry or rule for the website.
Hosting with IONOS — Fast, Flexible, Secure
Discover fast and secure hosting for any project.
We've got your code covered, whether you're on the backend, frontend, or the frontlines.
What is the hosts file?
First of all, the hosts file is a relic of the past. In the early days of the internet, the file was used to link domain names to certain IP addresses i.e. to 'resolve' them. For example, the hosts file made sure that the computer knew what IP address was meant by www.ionos.com. Therefore, the hosts file used to contain a long list of these hosts and their special addresses. The influence of the term 'host' is still noticeable today when you speak of 'hosting a website'.
If you open your own hosts file, you’ll most likely not find any entries. For the resolution of web addresses, the domain name system (DNS) is used. However, the hosts file is still practical for local (LAN) and virtual (VLAN) networks. For surfing the internet, the file is useful for blocking malicious websites, among other things.
Structure and function of the hosts file
The hosts file is a very simple text file that can be opened and edited in Windows with the pre-installed editor, for example. Firstly, you see the Windows description of the file. Below this you can define IP addresses and corresponding host names. The defined IP address is in a common line with the corresponding host name. It is important that the two pieces of information are separated by at least one space. You can also add comments by prefixing them with '#'. Blank lines in the text file are ignored.
You can also edit the hosts file with a tool. For Windows, there is the free software HostsMan.
The data can then be read out from the network. This principle applies to all Windows versions as well as to Linux, macOS, iOS, Android, and other operating systems.
The functionality of the hosts file is simple, but quite powerful:
- The hosts file enables the unique assignment of IP addresses to hosts, even if they have the same name.
- On the other hand, the hosts file enables you to assign a unique name to hosts.
- You can also lock addresses using the hosts file by simply redirecting them to a non-existent IP address (address 0.0.0.0, then the web address you want to lock). If you try to access the page, an error message appears because the host is not found. You can use this method to clock advertising servers and dangerous internet sites, among other things.
- For a better and safer surfing experience, there are list templates that you can copy into your hosts file e.g. 'How to make the internet not suck' and MVPS. Both documents are regularly updated and are full to the brim with addresses thought to be questionable or dangerous.
Don’t make the hosts file too long. Otherwise it can lead to increased loading times on weak systems.
How to change the hosts file in Windows XP, 7, 8, and 10
- Find the entry 'Editor' in the start menu and right-click on it. Select 'Run as an administrator'.
- In the editor, go to the menu item 'Open' which you’ll find under 'File'.
- Open the 'hosts' file in Windows Explorer using the path C:\Windows\ System32\drivers\etc\hosts.
- You can now edit the hosts file.
How to change the hosts file on your Mac
- Select the tab 'Go to' in the upper start bar and click on 'Go to folder'.
- In the new dialog box, enter the following file path /private/etc/hosts. Then click on 'Open'.
- The folder containing the 'hosts' file will then open. Double-click on it to open the file’s text editing.
If you don’t have write permission for the file, simply follow these steps:
- Click the magnifying glass in the upper right-hand corner of the start bar
- Type 'Terminal' into the search bar and click on the tool in the search results
- Enter the command sudo nano /etc/hosts and confirm by pressing [Enter]
- If your Mac account is password protected, you will be prompted to re-enter it
- You can then edit the hosts file
How to change the hosts file in Linux/Ubuntu
- Depending on which version of the operating system you have as well as your personal choice, you will have one or more of the following default terminal programs installed: Unity, KDE, Xfce, LXDE, GNOME Shell, MATE.
- Open the corresponding editor (usually via 'Applications' > 'Accessories' > 'Terminal').
- Enter the following command: sudo nano /etc/hosts. The sudo prefix gives you the necessary root rights. The hosts file is a system file and is especially protected in Ubuntu.
- You can then edit the hosts file with your text editor or terminal.
Risk factor for hosts files
Unfortunately, the hosts file is also a popular target for malware, which infiltrates the system and can change the file by itself. This can lead to users being directed to dangerous sites as well as certain addresses being blocked e.g. the servers of anti-virus program providers, meaning that their applications can no longer update. However, with a good firewall and an active virus scanner, you can usually prevent unauthorized access to the file. It can also do no harm to check the file for unwanted entries from time to time.
If you define the hosts file as read-only, you will be protecting it from unauthorized access.
File paths: how to find the hosts file
|Operating system||Version(s)||File path|
|Windows||95, 98, ME||95, 98, ME|
|NT, 2000, XP, 2003, Vista,2008, 7, 2012, 8, 10||C:\Windows\ System32\drivers\etc\hosts|
|Windows Phone/Mobile||Registry on HKEY_LOCAL_MACHINE\Comm\Tcpip\Hosts|
|macOS||9 and previous||Folder 'Settings' or 'System'|
|Mac OS X 10.0–10.1.5||The file must be created with the NetInfo tool (/Applications/Utilities/NetInfo Manager)|
|Mac OS X 10.2 and more current||/private/etc/hosts|
|iOS||iOS 2.0 and more current||/system/etc/hosts|