在/etc/hosts文件中添加一个记录,发现ping 主机名后解析不生效,即ping出来的结果和hosts中写的IP是不一样的。
通过strace查看ping命令执行过程:
strace -f -F -o /tmp/ping.txt ping bi.v.addev.com
可以看到ping的整个过程是先连本地的nscd缓存,清理nscd缓存(nscd -i hosts)后发现hosts绑定的IP立即生效。
查看nscd配置文件: /etc/nscd.conf 了解到nscd会缓存三种服务passwd group hosts,所以它会记录三个库,分别对应源/etc/passwd, /etc/hosts 和 /etc/resolv.conf每个库保存两份缓存,一份是找到记录的,一份是没有找到记录的。每一种缓存都保存有生存时间(TTL)。其作用就是在本当中增加cache ,加快如DNS的解析等的速度。
enable-cache hosts yes
会开启本地hosts缓存
# logfile /var/log/nscd.log
# threads 4
# max-threads 32
server-user nscd
# stat-user somebody
debug-level 0
# reload-count 5
paranoia no
# restart-interval 3600
enable-cache passwd yes
positive-time-to-live passwd 600
negative-time-to-live passwd 20
suggested-size passwd 211
check-files passwd yes
persistent passwd yes
shared passwd yes
max-db-size passwd 33554432
auto-propagate passwd yes
enable-cache group yes
positive-time-to-live group 3600
negative-time-to-live group 60
suggested-size group 211
check-files group yes
persistent group yes
shared group yes
max-db-size group 33554432
auto-propagate group yes
enable-cache hosts yes
positive-time-to-live hosts 3600
negative-time-to-live hosts 20
suggested-size hosts 211
check-files hosts yes
persistent hosts yes
shared hosts yes
max-db-size hosts 33554432
enable-cache services yes
positive-time-to-live services 28800
negative-time-to-live services 20
suggested-size services 211
check-files services yes
persistent services yes
shared services yes
max-db-size services 33554432
说明:
server-user user
如果设置了该选项,nscd将作为该用户运行,而不是作为root。如果每个用户都使用一个单独的缓存(-S参数),将忽略该选项。
enable-cache service <yes|no>
启用或禁用制定的 服务 缓存。
positive-time-to-live service value
设置 service 在指定缓存中正的项目(成功的请求)的TTL(存活时间)。 Value 以秒为单位。较大的值将增加缓存命中率从而减低平均响应时间,但是将增加缓存的一致性问题。
negative-time-to-live service value
设置 service 在指定缓存中负的项目(失败的请求)的TTL(存活时间)。 Value 以秒为单位。如果存在由不在系统数据库中的uid(用户ID)(例如在以root身份解包linux 内核源代码时)所拥有的文件将明显改善性能;应该维持较小的值以降低缓存一致性问题。
suggested-size service value
这是内部散列表的大小, value 应该保持一个素数以达到优化效果。
check-files service <yes|no>
启用或禁用检查属于指定 服务 的文件的改变。这些文件是 /etc/passwd, /etc/group, 以及 /etc/hosts。
清除缓存
nscd -i passwd
nscd -i group
nscd -i hosts
转载于:https://blog.51cto.com/liuqunying/1669889