Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is it possible to define a mapping hostname to IP address in Linux without root access (ie modify /etc/hosts) nor a DNS server ?

share|improve this question
2  
Already discussed here: serverfault.com/questions/89149/per-user-hosts-file –  zmode Oct 16 '12 at 13:24
    
@zmode thanks a lot. –  UmNyobe Oct 16 '12 at 13:27
    
add comment

1 Answer 1

up vote 1 down vote accepted

It depends on what you're trying to do. In either case, you're practically guaranteed to need to write your own code, so if you are not a programmer, you might want to either start learning how to be one, or hope you have a friend who is one.

Rolling Your Own DNS Resolution Infrastructure

If you're writing your own program, you can basically bypass the system's DNS resolution entirely, and do it yourself.

You see, DNS resolution is provided as a system-wide service, but there is not (usually) any restriction that prevents you from creating your own, separate, DNS resolution architecture. The actual activity of DNS resolution is "just" IP packets going over the network. So all you have to do is use an existing library that understands the DNS protocol but allows you to customize the responses or use a hosts file in a custom directory. This approach has the advantage that no "hacks" are needed, but the disadvantage is that you have to create your own program -- whether it's a scripting language or a native programming language is not relevant... either way, you need to create new software. You can't apply this hack to existing software, especially not compiled code for which you don't have the source code.

An example of a C library that just does DNS message encoding/decoding is libdns from NMAP.

Overriding DNS Resolution In Existing Programs

If you're running programs that you don't have write access to, and are setuid root, then no -- you're out of luck, unless you want to copy that program binary to another location and remove the setuid (although some programs refuse to run if they aren't setuid root).

If the program you're running is not setuid root, then it should be possible. The broad strokes of what you'd have to do is to LD_PRELOAD a library that implements the standard C library DNS resolution functions, and do some custom processing to make the desired modifications, before turning over unrecognized hosts to the system C library implementation for "normal" resolution.

The C library functions you'll have to override with LD_PRELOAD are as follows:

Note that you might really confuse certain programs if you do this, because making arbitrary modifications to the return values of these functions can violate the POSIX.1-2001 standard. See RFC 2553.

If you want to enable this for all programs you start, you'll have to export the LD_PRELOAD environment variable into your shell or your login session so that everything starts with LD_PRELOAD. Unfortunately, this excludes programs such as Xorg, because (I believe) it is setuid root. Also, programs like vmware-vmx (the main binary for VMware's hypervisor) are setuid root, so you have the same problem there.

Lower-level Info

This hack is possible because setting the LD_PRELOAD environment variable to a shared library causes the dynamic linker on Linux to read the preloaded library's symbols before it reads them from any other library. So if you have a symbol (which is, essentially, a function signature) called foo in libc.so.6 (the C library implementation), and you also have foo in libevil.so (your library), and you set LD_PRELOAD=libevil.so (you need to include the full path though), it will load foo from your libevil.so, so that when the program you launched calls foo() in code, it will call into libevil.so, which can then decide (if it wants) to use dynamic linker functions to call into libc.so.6's implementation of foo() -- optionally.

You will have to do all of this hacking in C, most likely, due to the low level nature of the code. You can use utility libraries such as GLib to make life easier, so you don't have to invent algorithms for basic operations like string manipulation and automatically growing arrays.

References

share|improve this answer
    
I don't know if it's just me but the name of the LD_PRELOAD library seems to indicate that this is not a good option. –  UmNyobe Oct 16 '12 at 13:55
    
It's just an arbitrary name I gave it. Trying to override the DNS resolution of the C library is not a good option, period. I can't think of a good reason why you'd want to do this, and why you would have to, on a system where you lack root access, or where you can't request a root access service to do it for you, or set the permissions on the file, etc. It sounds like your problem is "not a good option". But I worked within your problem definition to help you as much as possible. –  ÃŁŁǫǛȉЖΦΤїҪ Oct 16 '12 at 14:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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