对于 Linux 用户来说,特别是在 Ubuntu 这样的现代发行版上,最强大且便捷的工具之一是 systemctl command。这个命令行工具通常用来与 systemd(系统和服务管理器)交互。
通过这个命令,你可以管理系统服务、查看它们的状态、启动或停止它们,以及设置它们在系统启动时是否自动运行。很方便,对吧?但如果 systemctl 命令找不到或无法识别,你就会遇到 “sudo:systemctl: command not found” error.
So, let’s talk about what the “systemctl: command not found” error means, the various reasons why you’re facing this error, and how to fix this error.
systemctl: 命令未找到是什么意思?
Simply put, the “systemctl: command not found” error means that the systemctl command-line utility is not available or accessible in the current environment. This can be due to a host of reasons, which all come back to either the environment being a non-system environment or the systemd package not being installed on the system.
通常这是因为 systemd 软件包未安装,或者你的 Linux 发行版本不使用 systemd,而是使用 init.d 等更早的服务管理系统。另一种可能是 PATH 环境变量没有包含 systemctl 所在的目录,导致 shell 找不到它。
Now that you have an idea of what could be wrong with your system, leading to the “systemctl: command not found” error, let’s look at how you can verify where your problem lies and how to fix it.
修复 systemctl: Command Not Found 错误
接下来,我们会检查你的系统是否安装了 systemd,以及是否使用 systemctl,同时确认 systemctl 的路径。确认问题后,我们会采取相应措施修复这个错误。
1. 检查您的系统是否使用 systemd
由于 Ubuntu systemctl 仅在已安装 systemd 软件包的情况下才能使用,我们首先需要运行以下命令来检查您的系统是否应该使用 systemd:
ps -p 1 -o comm=
或者,你可以用下面这条命令检查 systemd 的版本:
systemd --version
如果输出是 systemd,说明 systemd 是你的 init 系统,应该已经安装了 systemctl。但如果输出是 init 或其他进程管理器,说明你的系统使用的是另一种初始化系统,除非迁移到 systemd,否则 systemctl 无法使用。
如果你看到 Upstart 或 SysVinit 这样的旧初始化系统,想要切换到 systemd 来使用 systemctl,请按照以下步骤操作:
1. 使用以下命令安装 systemd 以获得 systemctl:
sudo apt install systemd-sysv
2. 运行以下命令重启系统:
sudo reboot
3. 检查您的系统现在是否在使用 systemd:
ps -p 1 -o comm=
输出现在应该是 systemd 了。
2. 确保 systemd 安装正确
通常情况下,如果systemd已安装且作为你的初始化系统运行,systemctl命令应该能正常工作。如果不能,说明systemd包要么没有安装,要么安装不正确。
请注意,如果您使用的是 Ubuntu 的最小版本,例如 Docker 容器或云实例,systemd 可能默认未安装。要通过安装 systemd 来获取 systemctl,请运行以下命令:
sudo apt update sudo apt install systemd
安装完成后,检查 systemctl 命令是否可用。
3. 检查 systemctl 的 PATH
systemctl not found 错误的一个常见原因是 systemctl 已安装,但不在系统的 PATH 中。要检查 systemd 二进制文件是否存在于通常的位置(如 /bin、/usr/bin 或 /usr/local/bin),可以运行以下命令:
which systemctl
如果在 PATH 环境变量列出的任何目录中找到 systemctl,你会看到类似这样的输出:
/usr/bin/systemctl
如果 systemctl 在 PATH 中的任何目录里都找不到,which 命令会返回空。
不过,即使 systemctl 在你的目录中能找到,它仍然可能无法运行。这是因为 systemctl 所在的目录可能不在 PATH 环境变量中。遇到这种情况,你需要用下面的命令来检查 systemctl 所在的目录是否已添加到系统路径中:
echo $PATH
输出应该是这样的:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
现在,你只需检查 systemctl 所在的目录(从 which 命令获得)是否包含在 echo $PATH 命令输出的目录列表中。
如果 systemctl 的目录没有出现在 echo $PATH 的输出中,你需要将其添加到 PATH 变量。运行以下命令:
For Bash shell 用户,在文本编辑器中打开 ~/.bashrc 文件:
nano ~/.bashrc
在文件末尾添加以下内容:
export PATH=$PATH:/usr/bin
在本例中,/usr/bin 是 systemctl 所在的典型目录。
现在,按 CTRL + X 保存并关闭文件,然后按 Y 确认,再按 Enter 键保存。
For Zsh users,在文本编辑器中打开 ~/.zshrc 文件:
nano ~/.zshrc
在文件末尾添加以下内容:
export PATH=$PATH:/usr/bin
在本例中,/usr/bin 是 systemctl 所在的典型目录。
现在,按 CTRL + X 保存并关闭文件,然后按 Y 确认,再按 Enter 键保存。
完成以上步骤后,使用 echo $PATH 命令验证 systemctl 目录是否已添加到 PATH 中。现在你应该能够直接运行 systemctl,不会再出现 "systemctl: command not found" 错误。
4. Reinstalling systemd
如果之前的方法都试过了,还是出现命令未找到的错误,可以尝试完全重新安装 systemd 来解决。运行以下命令:
sudo apt purge systemd sudo apt install systemd
接下来,用这个命令重启你的系统:
sudo reboot
检查 systemctl 是否正常工作。
5. 验证 systemd 配置是否正确
有时问题可能很简单,只是 systemd 安装配置不当,导致出现 "systemctl: command not found" 错误。要修复这个问题,只需运行以下命令即可:
sudo dpkg-reconfigure systemd
6. 过时的 Windows 子系统 for Linux (WSL)
如果你在 Ubuntu 下使用 WSL (Windows Subsystem for Linux) 你应该知道较早版本的 WSL,如 WSL 1,不原生支持 systemctl。无论你使用前面哪种修复方法,都无法运行 systemctl。
要使用 systemctl 的功能,必须升级到 WSL 2,它对 systemd 的支持更好。如果不确定是否在使用 WSL,可以用以下命令检查:
uname -a
如果输出信息中提及 WSL,那就是你看到 "systemctl: command not found" 错误的原因。
7. 用 sudo service 命令替代 systemctl
如果因为系统不使用 systemd 而出现 "systemctl: command not found" 错误,可以用 sudo service 命令来管理服务。此命令适用于 SysVinit 或 Upstart 等较早的 init 系统。对于依赖 init 脚本而非 systemd 的 Ubuntu 系统特别有用。
例如,要启动服务,运行:
sudo service <service_name> start
Final Thoughts
systemd 及其 systemctl 命令行工具已取代大多数较早的 init 系统,如 SysVinit 和 Upstart。虽然 systemd 因其单体设计和非 Unix 哲学不适合所有人,但 systemd 仍被普遍认为是主流选择。希望你能解决 systemctl 命令不可用的错误。无法使用 systemd 的命令行工具会严重限制其功能。
FAQ
什么是 systemctl,"systemctl: Command Not Found" 是什么意思?
systemctl 是用于管理运行 systemd 的 Linux 系统中服务的命令行工具,如 Ubuntu。"systemctl: command not found" 错误发生在工具不可用或无法访问时,通常是因为系统不使用 systemd 或未安装 systemd 包。
systemctl 在 Ubuntu 的所有版本上都可用吗?
不是。systemctl 并非在 Ubuntu 的所有版本上都可用。systemctl 是 systemd init 系统的一部分,它是大多数现代 Ubuntu 版本(15.04 及之后)的默认选项。较早版本或某些最小化安装可能使用 SysVinit 或 Upstart 等替代 init 系统。