DESCRIPTION
This manual page describes the format of the /etc/hosts file. This file is a simple text file that associates IP addresses with hostnames, one line per IP address. For each host a single line should be present with the following information:
IP_address canonical_hostname [aliases. ]
The IP address can conform to either IPv4 or IPv6. Fields of the entry are separated by any number of blanks and/or tab characters. Text from a «#» character until the end of the line is a comment, and is ignored. Host names may contain only alphanumeric characters, minus signs («-«), and periods («.»). They must begin with an alphabetic character and end with an alphanumeric character. Optional aliases provide for name changes, alternate spellings, shorter hostnames, or generic hostnames (for example, localhost). If required, a host may have two separate entries in this file; one for each version of the Internet Protocol (IPv4 and IPv6).
The Berkeley Internet Name Domain (BIND) Server implements the Internet name server for UNIX systems. It augments or replaces the /etc/hosts file or hostname lookup, and frees a host from relying on /etc/hosts being up to date and complete.
In modern systems, even though the host table has been superseded by DNS, it is still widely used for:
bootstrapping Most systems have a small host table containing the name and address information for important hosts on the local network. This is useful when DNS is not running, for example during system bootup. NIS Sites that use NIS use the host table as input to the NIS host database. Even though NIS can be used with DNS, most NIS sites still use the host table with an entry for all local hosts as a backup. isolated nodes Very small sites that are isolated from the network use the host table instead of DNS. If the local information rarely changes, and the network is not connected to the Internet, DNS offers little advantage.
FILES
NOTES
Modifications to this file normally take effect immediately, except in cases where the file is cached by applications.
Historical notes
RFC 952 gave the original format for the host table, though it has since changed.
Before the advent of DNS, the host table was the only way of resolving hostnames on the fledgling Internet. Indeed, this file could be created from the official host data base maintained at the Network Information Control Center (NIC), though local changes were often required to bring it up to date regarding unofficial aliases and/or unknown hosts. The NIC no longer maintains the hosts.txt files, though looking around at the time of writing (circa 2000), there are historical hosts.txt files on the WWW. I just found three, from 92, 94, and 95.
EXAMPLES
# The following lines are desirable for IPv4 capable hosts 127.0.0.1 localhost # 127.0.1.1 is often used for the FQDN of the machine 127.0.1.1 thishost.example.org thishost 192.168.1.10 foo.example.org foo 192.168.1.13 bar.example.org bar 146.82.138.7 master.debian.org master 209.237.226.90 www.opensource.org # The following lines are desirable for IPv6 capable hosts ::1 localhost ip6-localhost ip6-loopback ff02::1 ip6-allnodes ff02::2 ip6-allrouters
SEE ALSO
Package name: core/man-pages Version: 6.04-1 Upstream: https://www.kernel.org/doc/man-pages/ Licenses: GPL, custom Manuals: /listing/core/man-pages/ Table of contents
Powered by archmanweb, using mandoc for the conversion of manual pages.
The website is available under the terms of the GPL-3.0 license, except for the contents of the manual pages, which have their own license specified in the corresponding Arch Linux package.
Can I map a IP address and a port with /etc/hosts?
So that when I hit testdev.com , it refers 127.0.0.1:8000 . I tried the above one, but it doesn’t work. Any other alternative way to achieve this?
What David Postill says is correct, but if you are really hoping to get rid of the 8000 port for local testing you might want to investigate setting up a reverse proxy via Apache or Nginx. That is the most common and accepted way to map a port-based address like 127.0.0.1:8000 to testdev.com .
4 Answers 4
Can I map a IP address and a port with /etc/hosts
The /etc/hosts file is part of your system’s domain name resolver (it will check this file, then check DNS).
The resolver’s job is to convert text domain names to an IP address, not an IP address + port.
Some applications like Minecraft support checking a DNS server’s SRV record and can use a port number from that, but again, this is dependent on the program’s behavior and can’t be done from your /etc/hosts file.
I’d like to map 127.0.0.1:8000 to testdev.com
What @Giacomo1968 said in the comments says is what you need to do.
This solution worked for me with nginx
If you are using nginx you can use Nginx as reverse proxy and do something like this below
For more information regarding the above method check this here
For window users
I found this solution from stackoverflow here
As Lawrence has already said it is not possible through /etc/hosts but you can set a reverse proxy in order to achieve it using nginx or apache . I had the same problem in the past so I made this tool to achieve this with a /etc/hosts syntax: https://github.com/cristianoliveira/ergo
The solution that worked for me using apache was first to modify my apache config like this:
Web Server Config
ServerName testdev.com ServerAdmin webmaster@localhost DocumentRoot /var/www/testdev.com/public AllowOverride All Options Indexes FollowSymLinks Require all granted ErrorLog $/error.log CustomLog $/access.log combined # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
And dont forget to restart apache after you’ve modified any config file. Notice the ServerName Directive has been set to the domain name you want to use, and the VirtualHost *:80 is set to 80. This will direct all requests from your browser that hit your domain name on port 80 to this virtual host configuration.
Now you have to modify your hosts file as below:
127.0.0.1 localhost 127.0.0.1 testdev.com
Now head to your browser and type in testdev.com this will direct you to your web app
Hosts file port linux
NAME
hosts - static table lookup for hostnames
SYNOPSIS
DESCRIPTION
This manual page describes the format of the /etc/hosts file. This file is a simple text file that associates IP addresses with hostnames, one line per IP address. For each host a single line should be present with the following information: IP_address canonical_hostname [aliases. ] Fields of the entry are separated by any number of blanks and/or tab characters. Text from a "#" character until the end of the line is a comment, and is ignored. Host names may contain only alphanumeric characters, minus signs ("-"), and periods ("."). They must begin with an alphabetic character and end with an alphanumeric character. Optional aliases provide for name changes, alternate spellings, shorter hostnames, or generic hostnames (for example, localhost). The Berkeley Internet Name Domain (BIND) Server implements the Internet name server for UNIX systems. It augments or replaces the /etc/hosts file or hostname lookup, and frees a host from relying on /etc/hosts being up to date and complete. In modern systems, even though the host table has been superseded by DNS, it is still widely used for: bootstrapping Most systems have a small host table containing the name and address information for important hosts on the local network. This is useful when DNS is not running, for example during system bootup. NIS Sites that use NIS use the host table as input to the NIS host database. Even though NIS can be used with DNS, most NIS sites still use the host table with an entry for all local hosts as a backup. isolated nodes Very small sites that are isolated from the network use the host table instead of DNS. If the local information rarely changes, and the network is not connected to the Internet, DNS offers little advantage.
FILES
NOTES
Modifications to this file normally take effect immediately, except in cases where the file is cached by applications. Historical notes RFC 952 gave the original format for the host table, though it has since changed. Before the advent of DNS, the host table was the only way of resolving hostnames on the fledgling Internet. Indeed, this file could be created from the official host data base maintained at the Network Information Control Center (NIC), though local changes were often required to bring it up to date regarding unofficial aliases and/or unknown hosts. The NIC no longer maintains the hosts.txt files, though looking around at the time of writing (circa 2000), there are historical hosts.txt files on the WWW. I just found three, from 92, 94, and 95.
EXAMPLE
127.0.0.1 localhost 192.168.1.10 foo.mydomain.org foo 192.168.1.13 bar.mydomain.org bar 146.82.138.7 master.debian.org master 209.237.226.90 www.opensource.org
SEE ALSO
hostname(1), resolver(3), resolver(5), hostname(7), named(8) Internet RFC 952
How to Edit the Hosts File in Ubuntu Linux
The hosts file which is located at /etc/hosts is a very important network configuration file. The /etc/hosts file is a static DNS file with a list of computer names and their corresponding addresses.
Adding Hosts to /etc/hosts
To edit or add a new host to the hosts file, you need to open this file in a text editor. You can use any editor that you like, but if you are working on an Ubuntu server, you need to use a command line editor like vim, nano or emacs.
The format of the /etc/hosts file is very simple. Each DNS entry should contain at least two columns. The first column has the IP address of the computer, and the second column specifies the domain name/hostname.
192.168.1.10 server1.example.com
This tells the Ubuntu system that IP address of the server1.example.com is 192.168.1.10 (simple as that).
The domain name or the hostname can be provided as a short name (e.g. server1), or as a Fully qualified domain name (FQDN) like www.example.com.
192.168.1.10 server1 192.168.1.20 server2 192.168.1.30 www.example.com
If a computer has more than one name, you can specify more than one name on a single line as shown in the following screenshot.
In the example, the 192.168.1.10 host has both the short name (server1) and fully qualified domain name (server1.localdomain.local) included in the Ubuntu hosts file.
Ubuntu utilizes the /etc/hosts file, as well as DNS services to look up IP Addresses. When communicating with a remote computer by their name (instead of IP address), the Ubuntu domain name lookup service looks first in the /etc/hosts file before consulting DNS servers.
If an entry found in this file, the Ubuntu system will not communicate with a DNS server to resolve the IP address. Therefore, you can use the /etc/hosts file is to override an address that a DNS server is giving you.
However, this also means if the name record found in this file is incorrect, then the communication will fail. When you troubleshoot such problem make sure to check the /etc/hosts file to see if there are any DNS record for the given domain name.