Mapping Hostnames with Ports in /etc/hosts


The /etc/hosts file is a simple text file used to map hostnames to IP addresses. It is used to resolve hostnames to IP addresses, bypassing the need for a DNS server. Each line in the file represents a single mapping, with the IP address followed by one or more hostnames separated by spaces.

You can map hostnames with ports by specifying the hostname followed by the port number, separated by a colon. For example, to map the hostname "example.com" to the IP address "192.168.0.1" on port 80, you would add the following line to the /etc/hosts file −

192.168.0.1 example.com:80

This will cause the hostname "example.com:80" to resolve to the IP address "192.168.0.1". When you use this hostname in a web browser, it will be directed to the IP address on port 80, you can use similar for other ports as well.

Keep in mind that this is a local mapping, which means it only affects the device on which the hosts file is modified and it will not affect other devices on the network.

The Hosts File

The hosts file is a simple text file that maps hostnames to IP addresses. It is used to resolve hostnames to IP addresses, bypassing the need for a DNS server. The file is located on most computers at the path /etc/hosts (on Windows it is located in c:\Windows\System32\drivers\etc\hosts) and can be edited by anyone with access to the file.

Each line in the file represents a single mapping, with the IP address followed by one or more hostnames separated by spaces. For example, to map the hostname "example.com" to the IP address "192.168.0.1", you would add the following line to the hosts file −

192.168.0.1 example.com

When you type "example.com" into a web browser, the computer will first check the hosts file before making a DNS request. If it finds a match, it will use the IP address specified in the hosts file. This can be useful for testing websites on a local development machine or for blocking unwanted websites by redirecting them to a non-existent IP address.

It's important to note that the hosts file is a local file and changes made to it will only affect the computer on which it is modified. This means that it is not a global solution for things like blocking unwanted websites, and will not affect other devices on the network.

Adding a Port Number

You can add a port number to a hostname in the hosts file by specifying the hostname followed by a colon and the port number. For example, to map the hostname "example.com" to the IP address "192.168.0.1" on port 80, you would add the following line to the hosts file −

192.168.0.1 example.com:80

This will cause the hostname "example.com:80" to resolve to the IP address "192.168.0.1". When you use this hostname in a web browser, it will be directed to the IP address on port 80.

It's important to note that most applications and browsers will not automatically use the port number specified in the hosts file. You will need to include the port number in the URL when accessing the website. For example, instead of typing "example.com" into the browser, you would need to type "example.com:80".

Also, keep in mind that the hosts file is a local file and changes made to it will only affect the computer on which it is modified. This means that it is not a global solution for things like redirecting traffic to a specific port, and will not affect other devices on the network.

Using a Reverse Proxy

A reverse proxy is a server that sits in front of one or more web servers and acts as a gateway for client requests. The reverse proxy receives the incoming request from the client, forwards it to the appropriate web server, and then sends the web server's response back to the client.

One of the main benefits of using a reverse proxy is that it can provide additional functionality such as load balancing, SSL termination, and caching. It can also hide the existence and characteristics of the backend servers from the outside world, providing an additional layer of security.

When using a reverse proxy, the client sends a request to the reverse proxy's IP address and port, instead of the IP address and port of the backend web server. The reverse proxy then uses the information in the request to determine which backend server should handle the request, and forwards the request to that server. The backend server processes the request and sends the response back to the reverse proxy, which then sends the response back to the client.

Reverse proxies can be configured to route requests based on the hostname or path of the request, the client IP address, or other criteria. They can also be used to route requests to different backend servers based on the type of request (e.g., static vs dynamic content).

Popular open-source reverse proxy servers are Nginx and Apache, both of them also have the capability to add port number in the proxy rule and route the request based on the port number.

It's important to note that, setting up and configuring a reverse proxy requires knowledge of server administration and web server configuration.

Conclusion

In conclusion, the /etc/hosts file is a simple text file used to map hostnames to IP addresses, bypassing the need for a DNS server. It can be edited to include a port number by specifying the hostname followed by a colon and the port number. However, changes made to the hosts file are only local, and will only affect the computer on which it is modified.

A reverse proxy is a server that sits in front of one or more web servers and acts as a gateway for client requests. It can provide additional functionality such as load balancing, SSL termination, and caching. It can also hide the existence and characteristics of the backend servers from the outside world, providing an additional layer of security. Reverse proxies can be configured to route requests based on hostname, path, client IP address, or other criteria. They can also be used to route requests to different backend servers based on the type of request.

Updated on: 25-Jan-2023

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements