search
HomeSystem TutorialLINUXLinux Troubleshooting: 5 Common Problems & How to Fix Them

Linux Troubleshooting: 5 Common Problems & How to Fix Them

Linux系统以其强大和可靠性著称,但即使是经验丰富的用户也会遇到意想不到的问题。无论是意外删除的文件、忘记的root密码,还是系统运行缓慢,高效的故障排除技巧是成为Linux专家的关键。

本指南将介绍一些常见的Linux问题解决场景以及逐步解决方案,这些问题在系统管理员、开发人员和日常Linux用户中普遍存在。

场景一:意外删除重要文件

您意外地使用rm命令删除了一个重要文件,现在需要恢复它。与Windows和macOS不同,Linux没有内置的“回收站”来存储从终端删除的文件。

恢复选项取决于使用的文件系统。

EXT3/EXT4文件系统

使用extundelete,这是一个开源实用程序,用于从Linux的ext3和ext4文件系统中恢复已删除的文件。

<code>sudo apt install extundelete  # Debian-based  
sudo yum install extundelete  # RHEL-based  </code>

在尝试恢复之前,卸载分区以防止进一步写入可能覆盖已删除的数据:

<code>sudo umount /dev/sdX</code>

接下来,运行以下命令恢复已删除的文件,并确保将/dev/sdX替换为实际删除文件的分区。

<code>sudo extundelete /dev/sdX --restore-all</code>

XFS、Btrfs或NTFS文件系统

如果您的系统使用XFS、Btrfs或NTFS,则testdisk工具是更好的选择。

<code>sudo apt install testdisk  # Debian-based  
sudo yum install testdisk  # RHEL-based  </code>

运行testdisk并按照交互式提示恢复丢失的文件。

<code>sudo testdisk</code>

预防措施:

  • 使用trash-cli:不要使用rm,使用trash-cli将文件发送到可恢复的回收站。
<code>sudo apt install trash-cli  
trash-put myfile.txt  </code>
  • 定期备份:设置rsyncTimeshift来自动备份重要文件。

场景二:恢复忘记的root密码

您忘记了root密码,无法执行管理任务,这意味着您无法安装软件、更改系统设置或访问关键文件。

您可以通过启动到恢复模式或修改GRUB引导加载程序来重置root密码。

使用恢复模式(Ubuntu/Debian)

首先,重新启动系统,并在启动过程中按住Shift键以访问GRUB菜单,然后选择“高级选项”→“恢复模式”,并选择“进入root shell提示符”。

在这里,将root文件系统重新挂载为可写并重置root密码。

<code>mount -o remount,rw /
passwd root</code>

重新启动系统。

<code>reboot</code>

使用rd.break(RHEL/CentOS/Fedora)

首先,重新启动系统,在GRUB菜单中按e键,找到以linux开头的行,并在末尾添加rd.break

接下来,挂载root文件系统并重置root密码。

<code>mount -o remount,rw /sysroot  
chroot /sysroot  
passwd root  </code>

最后,退出并重新启动。

<code>exit  
reboot  </code>

预防措施:

  • 创建一个无密码的sudo用户,以避免被锁定在root访问之外。
  • 使用SSH密钥而不是密码进行身份验证。

场景三:安装了软件包,但无法运行

您安装了一个软件包,但是当您尝试运行它时,它显示“command not found”,这通常发生在二进制文件不在系统的PATH中、软件包安装不正确或缺少依赖项时。

解决方案是,首先您需要验证软件包是否已安装。

<code>dpkg -l | grep package-name  # Debian-based  
rpm -qa | grep package-name  # RHEL-based  </code>

如果缺少,请重新安装:

<code>sudo apt install package-name  
sudo yum install package-name  </code>

接下来,检查命令是否在您的系统PATH中。

<code>which package-name  
echo $PATH</code>

如果二进制文件位于非标准位置,请将其添加到PATH:

<code>export PATH=$PATH:/usr/local/bin  </code>

预防措施:

  • 安装新软件包后,重新启动终端或运行hash -r
  • 使用Snap或Flatpak等软件包管理器,它们可以更好地处理依赖项。

场景四:磁盘空间不足

您的系统显示“No space left on device”错误,阻止软件更新、日志记录和正常操作。

以下是如何回收磁盘空间并保持系统平稳运行的方法。

步骤1:检查磁盘使用情况

解决方案是,首先您需要使用df命令检查系统上每个分区使用了多少空间。

<code>df -h</code>

步骤2:查找并删除大型文件

接下来,通过运行du命令来查找占用空间最大的文件,它将扫描您的系统并列出前10个最大的文件或目录。使用rm删除不必要的文件或将其移动到外部驱动器。

<code>du -ah / | sort -rh | head -10</code>

步骤3:删除不必要的日志

日志对于故障排除和监控系统活动至关重要,但它们可能会快速增长并占用大量磁盘空间。

随着时间的推移,旧的日志可能不再需要,这使得它们成为清理的首选对象。

<code>sudo journalctl --vacuum-time=2d  # 删除超过2天的日志  
sudo apt autoclean                # 删除过时的软件包文件  </code>

步骤4:删除旧内核(Ubuntu/Debian)

当您更新系统时,尤其是在基于Ubuntu或Debian的发行版上,通常会安装新版本的Linux内核。

但是,旧内核不会自动删除,随着时间的推移,这些旧内核可能会累积并占用大量磁盘空间。

删除它们是一种安全有效的方法,可以在不影响系统功能的情况下释放空间。

<code>sudo apt autoremove --purge  </code>

预防措施:

  • 设置日志轮换:使用logrotate来自动管理日志文件大小和保留期限。
  • 监控磁盘使用情况:安装ncdu等工具来跟踪磁盘使用情况并识别占用空间大的文件。
  • 定期清理:安排定期清理以删除临时文件、缓存和未使用的软件包。

场景五:服务器突然无响应

您正在管理一个Linux服务器,突然它停止响应,您尝试通过SSH连接,但连接超时或拒绝建立。您甚至可能会注意到服务器仍在运行,但它对任何命令都没有反应。

这种情况可能是由各种问题引起的,包括:

  • 由于失控进程导致的高CPU或内存使用率。
  • 磁盘I/O瓶颈,系统被读/写操作超载。
  • 内核恐慌或系统崩溃。
  • 网络故障,阻止远程访问。

要恢复控制,请按照以下故障排除步骤操作。

步骤1:本地或通过TTY访问服务器

如果SSH无法工作,请尝试直接或通过TTY会话访问服务器:

  • 在物理机器上,使用本地控制台。
  • 在虚拟机上,使用虚拟机管理程序的控制台。
  • 对于Linux系统,使用Ctrl + Alt + F2(或F3F4等)切换到另一个TTY会话。

步骤2:检查系统负载

登录后,检查系统的负载和资源使用情况,这将显示系统在1、5和15分钟内的平均负载。负载值高于CPU内核数表示需求量很大。

<code>uptime  </code>

接下来,使用tophtop实时监控进程:

<code>top  
或
htop</code>

查找消耗过多CPU或内存的进程。

步骤3:识别并终止失控进程

要识别资源消耗最多的进程,请运行:

<code>ps aux --sort=-%cpu | head  </code>

这将列出CPU消耗最多的进程,您可以在其中找到有问题的进程,并使用以下命令终止它:

<code>kill -9 PID  </code>

PID替换为有问题的应用程序的进程ID。

步骤4:检查系统日志

如果系统仍然可以响应,请检查日志中的错误:

<code>sudo tail -f /var/log/syslog  
或
sudo dmesg | tail  </code>

这些命令显示最近的系统消息和内核日志,这可以帮助识别硬件或软件问题。

步骤5:使用SysRq安全重启

如果系统完全冻结,请使用SysRq组合键安全重启:

<code>echo b > /proc/sysrq-trigger  </code>

这将触发安全重启,通过同步磁盘和卸载文件系统来确保数据完整性。

结论

故障排除是每个Linux用户的一项重要技能。无论是恢复已删除的文件、重置密码还是修复系统错误,了解正确的命令都可以节省时间和精力。

您是否有自己的故障排除技巧?请在评论中分享!让我们一起建立一个有益的Linux社区。

The above is the detailed content of Linux Troubleshooting: 5 Common Problems & How to Fix Them. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to Install and Run FreeDOS on Linux Using QEMUHow to Install and Run FreeDOS on Linux Using QEMUApr 29, 2025 am 10:36 AM

This guide shows you how to set up the free and open-source DOS-compatible operating system, FreeDOS, within a Linux environment using the QEMU emulator. This allows you to run legacy DOS software and games on modern hardware without needing a separ

How to Install KDE Plasma on Linux Mint 22How to Install KDE Plasma on Linux Mint 22Apr 29, 2025 am 10:10 AM

Linux Mint, an operating system known for its simplicity, stability and ease of use, is popular with users and is especially suitable for beginners. It uses the Cinnamon desktop environment by default, providing a simple and friendly user interface. But if you prefer a different look or need more customization options, you can install other desktop environments such as KDE Plasma. KDE Plasma is a feature-rich, highly customizable and visually excellent desktop environment that provides a modern and stylish user experience. It has a wide range of customization options, advanced window management capabilities and sophisticated aesthetics, perfect for users who want to have a better control over the desktop experience. This guide will guide you step by step to install KDE Pl on Linux Mint 22

How to Reduce High RAM & CPU Usage on LinuxHow to Reduce High RAM & CPU Usage on LinuxApr 29, 2025 am 10:05 AM

Linux system performance optimization: Reduce RAM and CPU usage Linux systems are powerful and efficient, but high RAM and CPU usage can reduce performance, slow down applications, and even cause servers, workstations, or embedded systems to crash. Therefore, optimizing resource usage is crucial for the smooth operation of the system. This guide will explore practical ways to reduce RAM and CPU usage in Linux systems, covering monitoring tools, process management, kernel tuning and system optimization technologies to help you keep your system running efficiently. Identify resource-intensive processes The first step in reducing RAM and CPU usage is to identify which processes consume the most resources. To do this, you can use the following command-line tools: a. Use top

How to Boot Into Single User Mode in AlmaLinux 8/9How to Boot Into Single User Mode in AlmaLinux 8/9Apr 29, 2025 am 09:46 AM

Single User Mode (also known as Rescue Mode) Guide for AlmaLinux 8 and 9 Single-user mode is a streamlined Linux environment that allows system administrators to perform maintenance tasks, troubleshoot problems, and recover from system failures. Single-user mode is especially useful when you need to reset your root password, fix configuration errors, fix corrupt file systems, or investigate system errors that prevent normal startup. As RHEL-based distributions, AlmaLinux 8 and 9 provide an easy way to enter single-user mode via the GRUB boot loader. This guide will explain step by step how to enter single-user mode on AlmaLinux 8 and 9. What is single use

Linux Troubleshooting: 5 Common Problems & How to Fix ThemLinux Troubleshooting: 5 Common Problems & How to Fix ThemApr 29, 2025 am 09:42 AM

Linux systems are known for their power and reliability, but even experienced users will encounter unexpected problems. Whether it is an unexpectedly deleted file, a forgotten root password, or a slow system running, efficient troubleshooting skills are the key to becoming a Linux expert. This guide will introduce common Linux problem solving scenarios and step-by-step solutions that are common among system administrators, developers, and everyday Linux users. Scene 1: Unexpected deletion of important files You accidentally deleted an important file using the rm command and now you need to restore it. Unlike Windows and macOS, Linux does not have a built-in "recycle bin" to store files deleted from the terminal. Recovery options depend on

How to Permanently Change Docker Folder Permissions on LinuxHow to Permanently Change Docker Folder Permissions on LinuxApr 29, 2025 am 09:35 AM

Docker is a powerful tool that allows you to run applications in an isolated environment called containers. However, sometimes you may need to change the permissions of the Docker folder to ensure that your application has access to the necessary files and directories. This article will guide you through the process of permanently changing Docker folder permissions on Linux systems. Understand Docker folder permissions By default, Docker stores its data, including images, containers, and volumes, in specific directories on Linux systems. The most common directory is /var/lib/docker. The permissions of these folders determine who can read, write, or execute the files in it. if

Manage Docker Like a Pro: Install Portainer CE on LinuxManage Docker Like a Pro: Install Portainer CE on LinuxApr 29, 2025 am 09:24 AM

Simplify Docker Management with Portainer CE on Linux: A Step-by-Step Guide Managing Docker containers via the command line can be daunting, especially for newcomers. Portainer CE (Community Edition) offers a free, lightweight, and intuitive solutio

How to Use Whisper AI for Live Audio Transcription on LinuxHow to Use Whisper AI for Live Audio Transcription on LinuxApr 29, 2025 am 09:18 AM

This guide details how to install and use Whisper AI for real-time speech-to-text transcription on Linux systems. Whisper AI, an OpenAI creation, offers high-accuracy transcription across multiple languages. While primarily designed for batch proces

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version