nscd
(Name Service Cache Daemon)是 Linux 系统中的一个缓存服务,用于加速名称服务(如用户、组、主机名等)的查询,减少对远程目录服务(如 LDAP、NIS)或本地配置文件(如 /etc/passwd
、/etc/hosts
)的频繁访问。
缓存常见名称服务查询:
/etc/passwd
或 LDAP/NIS)。/etc/group
或 LDAP/NIS)。/etc/hosts
或 DNS)。/etc/services
)。减少重复查询:
自动失效机制:
/etc/nscd.conf
ini
# 缓存 hosts(DNS 查询)并设置 TTL
enable-cache hosts yes
positive-time-to-live hosts 3600 # 缓存有效时间(秒)
negative-time-to-live hosts 20 # 查询失败时的缓存时间
启动/停止服务:
sudo systemctl start nscd # 启动
sudo systemctl stop nscd # 停止
sudo systemctl restart nscd # 重启
清除缓存:
sudo nscd -i passwd # 清除用户缓存
sudo nscd -i hosts # 清除 DNS/hosts 缓存
sudo nscd -i group # 清除用户组缓存
查看运行状态:
sudo systemctl status nscd
缓存不一致:
/etc/passwd
或 DNS 但未生效 → 重启 nscd
或清除缓存。性能问题:
/etc/nscd.conf
中的 positive-time-to-live
。禁用缓存:
/etc/nscd.conf
中设置 enable-cache
为 no
,或直接停止服务。调试模式:
sudo nscd -d -f /etc/nscd.conf # 前台运行并输出调试日志
nscd
的 hosts
缓存可能与 systemd-resolved
冲突,建议在 DNS 频繁变更的环境中禁用其一。如果需要更详细的配置或问题排查,可以提供具体场景进一步分析!