22

Is there some way to define user specific hosts - like in /etc/hosts? Maybe something like ~/.hosts?

1
  • 2
    can you tell what you are actually wanting to do?
    – Anonymous
    Jul 15, 2009 at 13:28

5 Answers 5

28

For anything ssh based (including rsync over ssh) you can add entries to your ~/.ssh/config file

e.g.

Host myhost
    Hostname myhost.example.com

Then ssh myhost will connect you to myhost.example.com

3
  • add a "User" option and It's a great recipe for heterogenous systems.
    – hayalci
    Jul 15, 2009 at 14:16
  • 1
    I use this approach with wildcards and bash completion for hostnames with the HOSTFILE environment variable. I end up with tab-completion of the 'alternate' hostnames quite nicely.
    – ericslaw
    Jul 15, 2009 at 14:29
  • You can also use this to do cool stuff with Git, if you have multiple keys or accounts you need to use. You can set up path-based conditional config in Git, and use SSH settings to specify users/keys as needed. stackoverflow.com/questions/8801729/…
    – shearn89
    Jan 22 at 9:02
5

Specific applications may have something you can use, like Nick suggested, but there is no user homedir equivelent of the /etc/hosts file.

When applications try to resolve hostnames it gets handled by NSS. You can check how NSS handles hostnames on your system by looking at /etc/nsswitch.conf

$ grep host /etc/nsswitch.conf 
hosts:          files dns

This means that hostnames will be resolved first against the file database (/etc/hosts), and failing that against the dns details specified in /etc/resolv.conf

5

I was wondering the same thing and a colleague found this solution: http://blog.tremily.us/posts/HOSTALIASES/

It basically involves setting an environment variable (HOSTALIASES) which points to the file to use for host aliases (you could use ~/.hosts for example).

2
  • 1
    The link broke :-( Feb 13, 2018 at 11:46
  • Currently, the link works (again).
    – vog
    Oct 7, 2019 at 10:53
0

Another solution could be different "chroot" environments for different users. Even different root jails for same user, depending on certain criteria.

0

I found https://github.com/bAndie91/libnss_homehosts referenced in https://bbs.archlinux.org/viewtopic.php?id=266445

That we you could have ~/.hosts file, with same syntax as /etc/hosts has.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .