Linux 域名访问速度优化实用指南
一 定位瓶颈与快速测量
cat /etc/resolv.conf。time nslookup example.com;如需观察完整递归过程,用 dig +trace example.com。二 本地 DNS 缓存优先
sudo apt-get install nscd 或 sudo yum install nscd -y;/etc/nscd.conf 中设置 enable-cache hosts yes,并合理调整 positive-time-to-live hosts(如 3600 秒)与 negative-time-to-live hosts(如 20 秒);sudo systemctl restart nscd。sudo apt install dnsmasq -y 或 sudo yum install dnsmasq -y;/etc/dnsmasq.conf 中设置 listen-address=127.0.0.1,上游 server=8.8.8.8、server=1.1.1.1,缓存大小 cache-size=1000、最小 TTL cache-min-ttl=3600;/etc/resolv.conf 添加 nameserver 127.0.0.1。systemctl status systemd-resolved;sudo systemd-resolved --flush-caches;/etc/systemd/resolved.conf 的 [Resolve] 段,设置 DNS=8.8.8.8 1.1.1.1,重启服务。三 上游 DNS 与解析策略优化
/etc/resolv.conf 或相应网络配置中同时设置多个 nameserver(如 8.8.8.8、8.8.4.4、1.1.1.1、9.9.9.9),提升可用性与命中率。nmcli 或接口配置文件设置 DNS1/DNS2;/etc/systemd/resolved.conf 而非直接改 /etc/resolv.conf。四 针对自建服务的优化
五 应急与白名单加速
sudo vi /etc/hosts93.184.216.34 example.comsudo systemctl restart NetworkManager 或重连网络。