如何检查你的 Linux 系统是否使用 systemd
初始化 systemd
![](/data/attachment/album/202401/17/080808gtpgc9rvxxifjx7m.jpg)



> 
> 不知道在使用哪个初始化系统？以下是方法。
> 
> 
> 


每个主流 Linux 发行版（包括 Ubuntu、Fedora、openSUSE 和 Arch）默认都使用 systemd。


但是有 [许多非 systemd 发行版](https://itsfoss.com/systemd-free-distros/)，例如使用轻量级 runit 来获得更好性能的 Void Linux 或主要使用 sysvinit 的 Devuan。


当你尝试遵循某些教程或文档并且其中包含特定于 systemd 或某些其他初始化服务的命令时，就会出现问题。


这时，你必须检查你的 Linux 系统使用的是 systemd 还是其他系统。


一种方法是检查 PID 为 1 的进程（毕竟，初始化系统是 Linux 系统上运行的第一个进程）。



```
ps 1

```

但它的输出可能会产生误导，因为它经常显示 `/sbin/init`，这只是实际的初始化进程的软链接。


如果你 [跟随该符号链接](https://linuxhandbook.com/follow-symbolic-link/)，就可以获取初始化系统信息。有两种方法：


* 使用 `stat` 命令
* 使用 `readlink` 命令


那么让我们从第一个开始。



> 
> ? 这些方法在 6 个初始化系统中进行了测试：Systemd、OpenRC、SysVinit、Busybox、runit 和 s6。
> 
> 
> 


### 方法 1：使用 stat 命令检查 systemd 是否正在使用


以下是如何使用 `stat` 命令来了解你正在使用的初始化系统：



```
stat /sbin/init

```

如果你使用的是 systemd 支持的发行版，那么它将显示以下输出：


![](/data/attachment/album/202401/17/080833uv5z353fwfokhz4v.png)


但是，如果你使用 systemd 之外的其他任何东西，它将显示初始化系统的名称，但是如果你使用的是 SysVinit，它只会显示 `init` 而不是 `sysvinit`：


![SysVinit only displays "init" instead of sysvinit](/data/attachment/album/202401/17/080833unmh33sxnb3y636n.png)


### 方法 2：使用 readlink 命令检查初始化系统


与之前的方法不同，当你使用 `readlink` 命令时，它只会打印初始化系统的名称。


因此，如果你想知道你是否正在使用 Systemd，只需使用以下命令：



```
readlink /sbin/init

```

![](/data/attachment/album/202401/17/080833b2c0plzgpzngu0qk.png)


如果你使用 [OpenRC 初始化访问](https://wiki.gentoo.org/wiki/OpenRC/openrc-init)，那么它将显示以下输出：


![](/data/attachment/album/202401/17/080834cfzx4xyyzn0y88yr.png)


但如果你使用 SysVinit，那么它会显示以下输出：


![](/data/attachment/album/202401/17/080834fzw7vxarwx9wdwr7.png)


### 我为你写的小“脚本”


另一种方法是检查 `/run/systemd/system` 目录是否存在。


好吧，最简单的找出方法是在终端中 [使用 if-else bash 命令](https://itsfoss.com/bash-if-else/)，它将检查你是否正在运行由 systemd 驱动的发行版：



```
if [ -d /run/systemd/system ]; then echo "System is running systemd"; else echo "System is not running systemd"; fi

```

![](/data/attachment/album/202401/17/080835v96e38q4jr4rf8x9.png)


### 有关 systemd 的更多信息


当你知道你正在使用 systemd 支持的发行版，你就可以 [使用 systemctl 命令管理服务](https://linuxhandbook.com/systemctl-commands/)。


当然你也可以从头开始 [创建一个 systemd 服务](https://linuxhandbook.com/create-systemd-services/)。


我希望本指南对你有所帮助。


*（题图：DA/02d49f79-0ec4-4e49-ae72-e3d46877dfa6）*




---


via: <https://itsfoss.com/check-if-systemd/>


作者：[Sagar Sharma](https://itsfoss.com/author/sagar/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

Every mainstream Linux distro including Ubuntu, Fedora, openSUSE, and Arch uses systemd by default.

But there are [many non-systemd distros](https://itsfoss.com/systemd-free-distros/) like Void Linux that uses lightweight runit for better performance or Devuan that uses sysvinit on principal basis.

The problem comes when you are trying to follow some tutorial or documentation and it has commands specific to systemd or some other init service.

And this is where you have to check if your Linux system uses systemd or something else.

One way would to be check the process with PID 1 (after all, an init system is the first process to run on a Linux system).

`ps 1`


But its output could be misleading as often it shows `/sbin/init`

which is just a soft link to actual init process.

If you [follow that symbolic link](https://linuxhandbook.com/follow-symbolic-link/), you can get the init system information. There are two ways of doing that:

- Using the stat command
- Using the readlink command

So let's start with the first one.

## Method 1: Check if systemd is in use with the stat command

Here's how you can use the stat command to find out what init system you are using:

`stat /sbin/init`


If you are using a systemd-powered distro, then it will show you the following output:

![Use the stat command to find if you are using systemd or not](https://itsfoss.com/content/images/2023/12/Use-the-stat-command-to-find-if-you-are-using-systemd-or-not.png)

But if you are using anything else than systemd, it will show the init name unless you are using SysVnit which will only show you `init`

instead of `sysvnit`

:

![Check if you are using SysVnit in Linux or not](https://itsfoss.com/content/images/2023/12/Check-if-you-are-using-SysVnit-in-Linux-or-not.png)

## Method 2: Check the init system using the readlink command

Unlike the previous method, when you use the readlink command, it will only print the name of the init system.

So if you want to know if you are using Systemd or not, simply use the following command:

`readlink /sbin/init`


![Use the readlink command to find if you are using systemd or some other init system in Linux](https://itsfoss.com/content/images/2023/12/Use-the-readlink-command-to-find-if-you-are-using-systemd-or-not-something-else.png)

If you are using the [OpenRC init service](https://wiki.gentoo.org/wiki/OpenRC/openrc-init), then it will show the following output:

![how to check if you are using openRC init system](https://itsfoss.com/content/images/2023/12/find-if-you-are-using-openrc-as-init-system.png)

But if you are using SysVnit, then, it will show you the following output:

![How to find out if you are using SysVnit as init system in Linux](https://itsfoss.com/content/images/2023/12/How-to-find-out-if-you-are-using-SysVnit-as-init-system-in-Linux.png)

## Tiny 'script' I wrote for you

Another way is to check if the `/run/systemd/system`

directory exists or not.

Well, the easiest way to find out is to [use an if-else bash command](https://itsfoss.com/bash-if-else/) in your terminal which will check if you are running a systemd-powered distro or not:

```
if [ -d /run/systemd/system ]; then echo "System is running systemd"; else echo "System is not running systemd"; fi
```


![A simple if else statement to know if you are using systemd distro or not](https://itsfoss.com/content/images/2023/12/A-simple-if-else-statement-to-know-if-you-are-using-systemd-distro-or-not.png)

## More on systemd

Once you know you are using the systemd-powered distro, here's how you can [manage services using the systemctl command](https://linuxhandbook.com/systemctl-commands/):

[Using systemctl Command [15 Examples]The systemctl command is a must know for Linux systems with systemd init system. Here are some practical examples.](https://linuxhandbook.com/systemctl-commands/)![](https://linuxhandbook.com/content/images/2023/11/systemctl-command-examples.png)


![](https://linuxhandbook.com/content/images/2023/11/systemctl-command-examples.png)

Want to [create a systemd service](https://linuxhandbook.com/create-systemd-services/) from scratch? You can do that too:

[How to create a systemd service in LinuxLearn the steps for creating systemd services in Linux with the practical example demonstrated in this tutorial.](https://linuxhandbook.com/create-systemd-services/)![](https://linuxhandbook.com/content/images/2022/09/create-systemd-service-in-linux.png)


![](https://linuxhandbook.com/content/images/2022/09/create-systemd-service-in-linux.png)

I hope you will find this guide helpful.