Can I map a IP address and a port with /etc/hosts?

Can I map a IP address and a port with /etc/hosts

No.

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 @JakeGould 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

Example

server {
  listen       testdev.com:80;
  server_name  testdev.com;
  location / {
    proxy_pass http://127.0.0.1:8000;
  }
}


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