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.

| improve this question | | | | |
  • 2
    Oh wow. I was about to post this same question, but I see you have. It makes sense to me, but strangly it doesn't exist on Linux. In my case I have a jailed form of Linux with Android OS on my Samsung Galaxy Tab device, and because of my web development I often need to edit /etc/hosts. In a jailed Linux, you can't edit /etc/hosts. So, a ~/.hosts would be really cool to have, definitely! – ServerChecker Dec 26 '10 at 3:51
  • did you find any solution in the end? – quamis Oct 26 '11 at 6:37
  • Unfortunately not. Maybe it is not possible because it's a security threat. – mrucci Oct 26 '11 at 7:18
  • 1
    For SSH, you can use aliases as a partial workaround. Put alias ssh="ssh " in your .bashrc (note the trailing whitespace at the end of the alias) along with alias myhost="me@real-name-of-my-host.com" and then you can say ssh myhost and it will work. – Noah Sussman Jul 11 '12 at 20:40
  • A better approach for workaround would be to put an entry in your ~/.ssh/config where you add the lines: Host my-host and Hostname 10.0.0.xxx <-- IP of the machine. The nice thing is it also allows you to specify username, port, which key to use etc – Ciprian Tomoiagă Aug 7 '18 at 7:17

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.

| improve this answer | | | | |
  • Is there a way to specify the HOSTNAMES.local in a local file (per-user) rather than in /etc/avahi/hosts ? – mrucci Nov 30 '09 at 6:13
  • I think Teddy means that each machine would have a name which Avahi would discover. Should the machine-to-name mapping really vary on a per-user basis? – pjc50 Nov 30 '09 at 11:10
  • no, it should not vary on a per-user basis. I just need a method for sharing host aliases (in a way that allows me to do ping hostnameAlias) without root privileges. Avahi almost do the right job, because I can use the machine $HOSTNAME as an alias. Just for curiosity: where this mapping is done? – mrucci Dec 1 '09 at 8:26

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.

| improve this answer | | | | |

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


For instance

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

| improve this answer | | | | |

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.

| improve this answer | | | | |

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.

| improve this answer | | | | |

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


for i in hosta hostb hostc olivia
    scp $TESTFILE $i:/tmp
    if [ $? -eq 0 ]

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

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

| improve this answer | | | | |
  • Thank you for the answer but i was trying to solve another problem: how to store and share the list of host names (hosta hostb hostc olivia) in a user home folder, such that I can write rsync src olivia:/dst – mrucci Nov 29 '09 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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