18

Is there a way to define host aliases (like in /etc/hosts) on a per user basis, i.e. in a file /home/user/.hosts ?

My primary objective is to easily share host aliases via rsync between the machines in my small lan. Since there is no machine that will be reliably always connected, a DNS server is not an option (right?).

Thank you.

mrucci
  • 368

6 Answers6

8

A better solution would be to install Avahi and libnss-mdns, and then use HOSTNAME.local addresses.

These names would not be per-user, but would solve your stated objective of having common names for all machines across a LAN.

Teddy
  • 5,424
4

See https://github.com/figiel/hosts for a way to do exactly what you ask.

That is a small library, loaded via LD_PRELOAD and which overrides gethostbyname() and several related functions that programs use for DNS resolution. It works well for me. The only caveat that I found is that Chrome web browser does not use native DNS resolution, but instead implements its own DNS client. To solve that problem you need to disable Chrome's DNS client.

2

The Linux glibc implementation can't do that.

But you could extend it with a nss library which could. This would be easy to write as you could rip off the relevant part of libc which is in the nss_files directory in

http://ftp.gnu.org/gnu/glibc/glibc-2.5.tar.bz2

For instance

This would, however, be a rather weird nonstandard thing to do.

MarkR
  • 2,928
1

As the other answers on this page describe, there are ways to achieve similar functionality, but:

No, there is no "per-user" file like /etc/hosts or any other way to override the default name resolution as a non-privileged user in GNU/Linux.

0

If you had two machines that would be up most of the time you could stand up BIND on each of them and make them the DNS servers..or get one of these (http://www.marvell.com/products/embedded_processors/developer/kirkwood/sheevaplug.jsp).

Use BIND on it to serve up your always on DNS.

-1

It would seem much easier to write a script which works out which hosts are up, and then sets the hostname for the following rsync command. Something like this

#!/usr/bin/sh

HOST="none"
TESTFILE=/tmp/testfile.$$
touch $TESTFILE
for i in hosta hostb hostc olivia
do
    scp $TESTFILE $i:/tmp
    if [ $? -eq 0 ]
    then
        HOST=$i
    fi
done
rm $TESTFILE

if [ $HOST != "none" ]
then
    echo $HOST is up
    rsync -av --rsh=ssh files $HOST:/dest
else
    echo "No host found"
    exit 1
fi

This is untested, and has assumptions, so you'll have to modify it to suit.

gorilla
  • 1,217