在 Ubuntu 中,“缓存”可以指多种不同类型的缓存,比如 APT 软件包缓存、系统内存缓存(PageCache)、DNS 缓存、或者应用层缓存(如 Redis)。下面按常见场景分别说明如何“开启”或“启用”。
Ubuntu 的 apt 默认就会缓存下载的 .deb 包。
缓存位置:
/var/cache/apt/archives/
查看缓存是否生效:
ls /var/cache/apt/archives/
手动清理缓存:
sudo apt clean
sudo apt autoclean
✅ 一般不需要手动开启,安装软件时自动生效。
Linux 内核会自动使用空闲内存做文件缓存。
查看缓存占用:
free -h
或
cat /proc/meminfo | grep Cache
✅ 内核自动管理,无需开启。
Ubuntu 默认 没有 DNS 缓存,需要安装服务。
Ubuntu 18.04+ 默认已带,但可能未启用缓存。
sudo nano /etc/systemd/resolved.conf
修改为:
[Resolve]
DNS=8.8.8.8
Cache=yes
重启服务:
sudo systemctl restart systemd-resolved
验证:
resolvectl status
sudo apt install dnsmasq
sudo systemctl enable dnsmasq
sudo systemctl start dnsmasq
如果你指的是“开启一个缓存服务”:
sudo apt install redis-server
sudo systemctl enable redis
sudo systemctl start redis
测试:
redis-cli ping
# 返回 PONG 表示成功
如果你是指网站缓存:
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m;
如果你能补充说明:
我可以给你更精确的配置示例。