where will gethostbyaddr() get the hostname from - resolution

In case of gethostbyname(), it returns ip addr by doing name resolution using DNS and /etc/hosts or Netbios.
How will gethostbyaddr() work? how will this function get hostname from address ?

It depends on how host resolution is configured in the box.
It could get from /etc/hostnames or so
or from DNS or so...

Related

DNS resolution failed: server IP address could not be found

/etc/hosts file has an entry in my development server 10.xx.xx.162 server-dev.xxx-intra.net. ping to server-dev.xxx-intra.net is successful.But when I am trying to access from the browser - https://server-dev.xxx-intra.net, it throws server-dev.xxx-intra.net's server IP address could not be found. I understand that it is not able to resolve that name to IP but shouldn't /etc/hosts entry have taken care of this? Please let me know if I am missing something.

Does a dynamic DNS hide your ip adress?

I'm setting up a self hosted server on my own network.
I don't want my ip address to be easily discovered as it's also my home ip address.
I was wondering if setting a dynamic DNS such as Duck DNS would allow to hide it to most users.
If no is there a solution to hide it ?
Post scriptum : The server is running on debian.
No. Setting up DNS would not hide the IP address. You can check with quite easily by using the host utility on Linux for instance:
host stackoverflow.com
stackoverflow.com has address 151.101.129.69
stackoverflow.com has address 151.101.193.69
stackoverflow.com has address 151.101.65.69
stackoverflow.com has address 151.101.1.69
So even if you setup your own hostname, everybody will know the IP anyway. Because in order to connect to your hostname, software will look up the IP and connect to that. If you want to hide your IP, you need to use a for instance a VPN.
Get a cheap VPS with enough bandwidth and use it as proxy, then disclose the proxy IP istead of the origin one.

Allowing hostname access in pg_hba.conf, won't work unless I also add the resolved ip address?

I want to allow postgres access from a hostname rather than an IP. I added access from the hostname to my pg_hba.conf, but when looking at the error log it appears that DNS resolves this hostname to an IP, connections from this IP are not allowed unless I explicitly allow access. This defeats the whole purpose of using the hostname, as hostnames for my services will NEVER change, where as the ip addresses can change daily.
What is the solution to this problem? Maybe my conf is just incorrect?
error:
test#test FATAL: no pg_hba.conf entry for host "10.81.128.90", user "test", database "test", SSL on[1]:
test#test DETAIL: Client IP address resolved to "cannablrv2-locationserver-1.kontena.local", forward lookup not checked.
shell script that adds access to pg_hba.conf
# Restrict subnet to docker private network
echo "host all all 172.17.0.0/16 md5" >> /etc/postgresql/9.5/main/pg_hba.conf
# Allow access from locationserver
echo "host all all cannablrv2-locationserver.test.kontena.local md5" >> /etc/postgresql/9.5/main/pg_hba.conf
# And allow access from DockerToolbox / Boottodocker on OSX
echo "host all all 192.168.0.0/16 md5" >> /etc/postgresql/9.5/main/pg_hba.conf
# Listen on all ip addresses
echo "listen_addresses = '*'" >> /etc/postgresql/9.5/main/postgresql.conf
echo "port = 5432" >> /etc/postgresql/9.5/main/postgresql.conf
You see that the client IP address resolves to a different name than the one you entered in pg_hba.conf, which is why the connection fails.
Did you read the documentation? It explains in detail how host names are handled.
You might get away with using .kontena.local to match name sufixes.
This answer assumes that you are using a DNS server for hostname resolution. According to https://www.postgresql.org/docs/current/auth-pg-hba-conf.html, if hostname is provided, then a reverse DNS look up will be performed with that IP. In your case, the reverse DNS look up of IP 10.81.128.90 is resolving to cannablrv2-locationserver-1.kontena.local instead of cannablrv2-locationserver.test.kontena.local which you have provided in your pg_hba.conf. Also, both reverse and forward DNS look up must give the expected results.

Can /etc/hosts config reverse resolution?

As we all known, we can add 'ip host' item in /etc/hosts to mock a DNS's name resolution, now comes the question, can I use /etc/hosts to do inverse resolution, form ip to hostname? Or is there any other handy way to do this? Thanks!
Maybe. It will depend on the tool you use to do the lookup and the configuration of resolving on your computer.
For example gethostbyaddr() will check /etc/hosts if "files" is in the hosts section of your /etc/nsswitch.conf
Note however that not all tools will do a local resolve, such as the "host" command for example, so it depends entirely on how you are attempting to do the lookup.
Yes. It does that automatically if the application uses Name Service Switch libraries (most applications do), and if /etc/nsswitch.conf is configured to resolve IPs from /etc/hosts with a line such as this:
hosts: files dns
You can test the reverse name resolution with either of the options below:
getent hosts 127.0.0.1
or
resolveip 127.0.0.1
No. That can only be done on a DNS server.
Yes you can. If you use dnsmasq, you can interfere in a number of ways to get a forward lookup going to 127.0.0.1 and the reverse lookup from 127.0.0.1 going to your host. For example, if your hostname is host1.mydomain.com with a real IP address of 192.168.1.12, then you can get 127.0.0.1 to resolve to it by doing the following in the dnsmasq configuration file:
host-record=host1.mydomain.com,127.0.0.1
The forward interference can be done in many ways, here is one:
alias=192.168.1.12,127.0.0.1
Obviously you need to set up the rest of dnsmasq properly to forward to your real DNS server ... but that is simple enough

Linux hostname resolution on a machine with multiple running interfaces

I have 4 running eth ports on my Linux machine (eth0 through eth3). So if i give the command host <hostname>, which eth port's ipaddress will be returned? how does linux decide which eth port's ip address to be returned?
Thanks,
LinuxPenseur
host does not look at your local IPs, it looks up the hostname based on the rules specified in /etc/nsswitch.conf
In nsswitch.conf you will typically have this entry:
hosts: files dns
This means that the resolver will first check /etc/hosts, if it cannot find anything there it'll check /etc/resolv.conf and ask the specified DNS servers for the IP, optionally appending the "search" and "domain" suffixes specified in resolv.conf
See http://www.faqs.org/docs/linux_network/x-087-2-resolv.library.html
What's your "primary" interface? That one is used.
http://ubuntuforums.org/archive/index.php/t-1120370.html

Resources