Linux: adding hosts programmatically?

Is there a way to programmatically add hosts to a local name in Linux?

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

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

Use case: I have an application accessible locally via a web browser. I want the app to be accessible via a local URI.

+3


source to share


4 answers


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

What you need? You can add foo 127.0.0.1

to hosts

or do the equivalent in your nameserver, but the connection from foo

on port 1234 will always go to 127.0.0.1:1234

- it is not possible to redirect it to port 9999 based on the name that is lost by the time it connect

is called.



On Linux you can add IP addresses to the loopback device (i.e. ip addr add 127.1.2.3 dev lo

) and then use the iptables

target 127.1.2.3:1234 to change all connections, instead go to 127.0.0.1:9999 but I can't tell by to your question, is this the observable behavior you want.

+4


source


If you only add hosts, a pretty safe way to do it

echo -e "ip.add.re.ss\thostname" >> /etc/hosts

      

Now, if you want to remove them, it starts to get hairy. I suspect that you also want to remove them.

If so, you can use Dynamic DNS, for example BIND has an nsupdate tool to update zone files:



       $ nsupdate
       > update delete oldhost.example.com A
       > update add newhost.example.com 86400 A 172.16.1.1
       > send

      

It does the following:

Any A records for oldhost.example.com are removed. And an A record for newhost.example.com with an IP address of 172.16.1.1. The newly added entry has a 1 day TTL (86400 seconds).

+1


source


The google search you want is "DDNS" for "Dynamic DNS". It is a technology for dynamically adding records to DNS servers that sounds exactly what you want. I'm sure the binding in most lunix distributions supports it, but you may need to read how to set it up.

0


source


I'll talk about a recent discovery: multicast-dns using the Avahi package. An example can be found here .

0


source







All Articles