Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

marked as duplicate by Gilles, jasonwryan, Renan, jw013, derobert Dec 4 '12 at 22:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2  
Similar to unix.stackexchange.com/questions/10438/…. Based on the info, it looks like a DNS configuration problem –  dartonw Dec 3 '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 3 '12 at 16:36
add comment

1 Answer

up vote 13 down vote accepted

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.

share|improve this answer
    
Might want to add this answer to the previous (duplicate) question, if you haven't already. –  vgoff Dec 5 '12 at 0:10
1  
What a HACK! ;-) I didn't tested it. But it deserves an accepted answer. –  yves Baumes Oct 28 '13 at 16:43
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.