Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux: adding hosts programmatically?

Tags:

linux

dns

Is there a way to programmatically add hosts to the local name resolver under Linux?

I would rather avoid fiddling with /etc/hosts dynamically...

Example: add the name foo and bind it to the local port 127.1.2.3

Use Case: I have an application installed locally accessible through a web browser. I'd like the application to be accessible through a local URI.

like image 973
jldupont Avatar asked Jun 28 '26 01:06

jldupont


1 Answers

add the name foo and bind it to the local port 127.0.0.1:9999

What is it that you want? You can add foo 127.0.0.1 to hosts or do the equivalent in your nameserver, but a connection to foo on port 1234 will always go to 127.0.0.1:1234 -- it's not possible to redirect that to port 9999 based on name, which is lost by the time connect is called.

On Linux you can add IPs to the loopback device (i.e. ip addr add 127.1.2.3 dev lo), and then use iptables to change all connections destined for 127.1.2.3:1234 to instead go to 127.0.0.1:9999, but I can't tell from your question if that the observable behavior you want.

like image 173
ephemient Avatar answered Jun 29 '26 16:06

ephemient



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!