📜 ⬆️ ⬇️

Ubuntu, fake DNS Server or setting up a local DNS server for a web developer

Why is this necessary.
On the developer’s workstation there are NN locally raised projects. This developer does the following for each project:
  1. To work with a local copy of the project, edit the configuration of its web server (apache, lighttpd, etc.) and specify the host name for this local project in the config.
  2. Then restarts the web server.
  3. Then he goes to edit / etc / hosts. Writes there the name of the new host and the local ip.
  4. Then he finds out that this registered host overlaps the one that is suddenly needed on the Internet, or he was mistaken when he registered the name in the config of the web server or in hosts.
As a result of all this tediousness, as a rule, precious nervous energy is spent that can be spent more efficiently on something more interesting.

What I propose to do.
I suggest making a local domain (in the .mydev example) and shortening the procedure above to two steps:
  1. Register a new host in the web server config with the name of the form projectname.mydev
  2. Restart the web server
And that's all. Happily we begin to finish our project by the given name.

What are we going to do, TODO
')
1. Install the DNS server.
sudo apt-get install bind9

2. Go look at the configs themselves. The principle of this thing is that at the moment when accessing a host (this is a rough scheme), our machine goes to the DNS server issued by a provider or manually written, in accordance with the settings. It receives from the DNS itself, and the IP address of the host is sent to the data in the direction of this IP. We are not interested further. How will. It will be like this: when accessing a host, we will contact the local DNS server, which will check our rules and if the request does not fit our rules, then our local DNS goes and asks the same for the provider DNS. And it gives us actually his (provider) answer. So, we go to configs
cd /etc/bind

REM: I use mc. We will need superuser rights, so either sudo mc or sudo mcedit filename when editing each file, I will not touch on the issue with rights later.

3. Open the edited named.conf.options
Here we are interested in the forwarders section, we need to uncomment it and specify the IP address (s) of the provider's DNS server (or maybe a router, depending on how the Internet connection is organized). Save, exit.

4. Open the edited named.conf.default-zones
Here we need to add a new zone down. The new section will look like this:
zone "mydev" {
type master;
file "/etc/bind/db.mydev";
};


5. On the specified path create a file
sudo cp /etc/bind/db.local /etc/bind/db.mydev
That is, just make a copy of the description of the local zone. We will need to edit something in it. So make a copy now:

6. Open for editing db.mydev
There we need to fix just a few lines. As a result, our db.mydev will look like this:
$TTL 604800
@ IN SOA mydev. root.mydev. (
2 ; Serial
604800 ; Refresh
86400 ; Retry
2419200 ; Expire
604800 ) ; Negative Cache TTL

@ IN NS mydev.

@ IN A 127.0.0.1
* IN A 127.0.0.1
@ IN AAAA ::1

Compare with the original db.local in order to make it clear what happened. Essentially nothing special.

7. Now it remains only to restart our DNS.
sudo /etc/init.d/bind9 restart

8. Well, now we check how it works.
ping 11.mydev
ping 22.mydev

If it works, then the IP should be the one that you specified (in the example 127.0.0.1) But now nothing works. Because…

9. I forgot to tell you about the rake. NetworkManager automatically writes the DNS address of the provider to /etc/resolv.conf. If an external DNS is registered there, then our crafts will not work. Appeals to the network just go past it. To eliminate this business, you need to go to the properties of the current Internet connection, go:
“Edit connections” => Choose our connection => “IPv4 settings”
Here we indicate the IP of our local DNS (in the example 127.0.0.1), this can be done by choosing the method “DHCP, only address”. That should be enough. After that, through NetworkManager, the connection is broken and re-established. If everything is ok, then in /etc/resolv.conf we should see our desired IP, the one that was registered as DNS in NetworkManager.

10. Now check the result.

That's all. Now you can not touch / etc / hosts

Source: https://habr.com/ru/post/85067/


All Articles