Skip to content

14. 常见问题与排错

14.1 "Permission denied" 权限不够

bash
# 检查文件权限
ls -la /path/to/file

# 检查当前用户
whoami
id

# 解决方案:修改权限或使用 sudo
chmod +x script.sh
sudo chown $(whoami):$(whoami) file.txt
sudo command

14.2 "No space left on device" 磁盘满了

bash
# 1. 检查磁盘空间
df -h

# 2. 找到占用最大的目录
du -sh /* 2>/dev/null | sort -rh | head -10
du -sh /var/* | sort -rh | head -10

# 3. 清理常见垃圾
sudo apt autoremove           # 清理无用包
sudo apt clean                # 清理下载缓存
sudo journalctl --vacuum-size=100M  # 清理日志
sudo systemd-tmpfiles --clean # 按系统策略清理临时文件
sudo find /tmp -mindepth 1 -mtime +7 -delete  # 只清理 7 天未修改的临时文件

# 4. 注意:可能是 inode 耗尽(而非空间)
df -i                         # 检查 inode 使用率
bash
$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        50G   48G     0 100% /

$ du -sh /var/* | sort -rh | head -5
15G	/var/log
12G	/var/cache
8G	/var/lib
2G	/var/www
50M	/var/backups

14.3 "Connection refused" 连接被拒绝

bash
# 1. 检查服务是否运行
systemctl status nginx

# 2. 检查端口是否监听
ss -tulnp | grep :80

# 3. 检查防火墙
sudo ufw status
sudo iptables -L -n

# 4. 检查服务配置
sudo nginx -t              # Nginx 配置检查
sudo apache2ctl configtest  # Apache 配置检查

# 5. 查看错误日志
tail -20 /var/log/nginx/error.log

14.4 忘记 root 密码

bash
# 在 GRUB 引导界面:
# 1. 选择 Advanced options → Recovery mode
# 2. 选择 root (Drop to root shell)
# 3. 重新挂载根文件系统为可写
mount -o remount,rw /

# 4. 修改密码
passwd root

# 5. 重启
reboot

14.5 系统启动后黑屏 / 无法进入图形界面

bash
# 切换到 TTY(Ctrl+Alt+F3)

# 重新安装显卡驱动
sudo apt install --reinstall ubuntu-desktop
sudo systemctl restart gdm3

# 检查 Xorg 日志
cat /var/log/Xorg.0.log | grep EE

# 重新配置显示管理器
sudo dpkg-reconfigure gdm3

14.6 常用排错思路

💡 提示: 💡 排错万能五步法:看日志(journalctl -xetail -f /var/log/syslog),查状态(systemctl status serviceps aux | grep),验配置(配置文件语法、端口、路径),测网络(pingss -tulnpcurl localhost),查权限(ls -laidgetfacl)。