Hosts file with port?

19,961

Solution 1

I managed to find the iptables rule to fix my issue:

iptables -A OUTPUT -d 127.0.0.1/32 -p tcp -j DNAT --to-destination 127.0.0.1:4433

Solution 2

Add a line into your /etc/hosts:

127.0.0.1      website.com

then configure your program (whatever it is) to listen on 4433 at 127.0.0.1.

And you're done.


Example:

echo hi | nc -lt 127.0.0.1 4433

now open website.com:4433 and you should see a "hi".

to fix the problem you mentioned in the comments you can use socat:

socat tcp-listen:4433,reuseaddr,fork tcp:localhost:xx

with the above command, socat will listen on 4433 and whenever a request came in in will forward it to the xx, so change the xx with the real port where your program is listening to.

Share:
19,961

Related videos on Youtube

NerdOfLinux
Author by

NerdOfLinux

Updated on September 18, 2022

Comments

  • NerdOfLinux
    NerdOfLinux almost 2 years

    I know you can't put a port in /etc/hosts, so how can I achieve the equivalent of:

    127.0.0.1     https://website.com:4433
    

    For WordPress, I need CURL to go to 127.0.0.1 on port 4433. As it's wordpress and not a script I made, I can't change that.

  • NerdOfLinux
    NerdOfLinux almost 7 years
    I can't tell the program to listen on 4433
  • Rinzwind
    Rinzwind almost 7 years
    why can't you tell curl to listen to port 4433?
  • NerdOfLinux
    NerdOfLinux almost 7 years
    It's part of wordpress. I have a problem with WP-cron and I need it to redirect my domain name to localhost.
  • Ravexina
    Ravexina almost 7 years
    That's another question however I'll update my answer ;)
  • NerdOfLinux
    NerdOfLinux almost 7 years
    Is there an iptables rule to redirect localhost requests to port 443 to port 4433?
  • Run
    Run almost 5 years
    This doesn't show any 'website.com' information.