User Specific Host Aliases

Available in 中文

Sometimes we need to claim useful host aliases, for example, when linking HTB domains to target machines, we can edit /etc/hosts like this:

10.10.11.27     itrc.ssg.htb signserv.ssg.htb

This is however very invasive due to the required privilege. We want a simpler and cleaner solution allowing each user to define their own set of host aliases without polluting the global name resolution.

I found a handy project libnss_homehosts, which allows you to put user-level hosts at ${XDG_CONFIG_HOME}/hosts or $HOME/.hosts.

1
paru -S aur/libnss-homehosts-git
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
--- ./a/etc/nsswitch.conf	2024-08-23 15:54:09.793769003 +0800
+++ ./b/etc/nsswitch.conf	2024-08-23 15:55:20.103690856 +0800
@@ -8,7 +8,7 @@

 publickey: files

-hosts: mymachines resolve [!UNAVAIL=return] files myhostname dns
+hosts: homehosts mymachines resolve [!UNAVAIL=return] files myhostname dns
 networks: files

 protocols: files