17

Possible Duplicate:
Can I create a user-specific hosts file to complement /etc/hosts?

In short: I would like to know if it is possible to get a ~/hosts file that could override the /etc/hosts file, since I don't have any privileged access.

A machine I am working on does seem to be properly configured with a correct DNS server. When I try to ping usual machine name I am working with, it fails. But when I try to ping them by IP address it works as expected.

I want to avoid changing any scripts and other musuculary memorized handcrafted commmand line ™ that I made because of a single unproperly configured machine. I contacted sys admin, but they have other fish to fry.

How can I implement that?

  • 2
    Similar to http://unix.stackexchange.com/questions/10438/can-i-create-a-user-specific-hosts-file-to-complement-etc-hosts. Based on the info, it looks like a DNS configuration problem – dartonw Dec 03 '12 at 15:25
  • Nothing standard like that, I'm afraid. But do check @dartonw's link, especially the first (accepted) answer with LD_PRELOAD. – peterph Dec 03 '12 at 16:36

1 Answers1

24

Beside the LD_PRELOAD tricks. A simple alternative if you're not using nscd is to copy libnss_files.so to some location of your own like:

mkdir -p -- ~/lib &&
cp /lib/x86_64-linux-gnu/libnss_files.so.2 ~/lib

Binary-edit the copy to replace /etc/hosts in there to something the same length like /tmp/hosts.

perl -pi -e 's:/etc/hosts:/tmp/hosts:g' ~/lib/libnss_files.so.2

Edit /tmp/hosts to add the entry you want. And use

export LD_LIBRARY_PATH=~/lib

for nss_files to look in /tmp/hosts instead of /etc/hosts.

Instead of /tmp/hosts, you could also make it /dev/fd//3, and do

exec 3< ~/hosts

For instance which would allow different commands to use different hosts files.

$ cat hosts
1.2.3.4 asdasd
$ LD_LIBRARY_PATH=~/lib getent hosts asdasd 3< ~/hosts
1.2.3.4         asdasd

If nscd is installed and running, you can bypass it by doing the same trick, but this time for libc.so.6 and replace the path to the nscd socket (something like /var/run/nscd/socket) with some nonexistent path.

  • Might want to add this answer to the previous (duplicate) question, if you haven't already. – vgoff Dec 05 '12 at 00:10
  • 3
    What a HACK! ;-) I didn't tested it. But it deserves an accepted answer. – yves Baumes Oct 28 '13 at 16:43
  • What does "replace the path to the nscd socket (something like /var/run/nscd/socket) with some nonexistent path" mean? – Labo Dec 20 '16 at 00:00
  • I tested it and while it works to define new host entries or re-define existing ones, it does not work to remove /etc/hosts entries, i.e. /etc/hosts domain blocking still works. Which is good in a way. Good hack though. :) – KIAaze Jan 10 '19 at 11:42