为什么 Ubuntu 18.04 使用 `/sbin/init` 而不是 `systemd`?

aka*_*ecc 2 linux ubuntu init systemd

首先,这是我的系统环境:

# cat /proc/version
Linux version 4.15.0-52-generic (buildd@lgw01-amd64-051) (gcc version 7.3.0 (Ubuntu 7.3.0-16ubuntu3)) #56-Ubuntu SMP Tue Jun 4 22:49:08 UTC 2019

# cat /etc/issue
Ubuntu 18.04.2 LTS \n \l
Run Code Online (Sandbox Code Playgroud)

参考这个Ubuntu Wiki,ubuntu从15.04开始默认使用Systemd,Systemd以PID 1作为运行/sbin/init。但是,我在 ubuntu 18.04 上发现了不同的结果:

# ps aux | awk '$2==1{print $0}'
root         1  0.0  0.8 159692  8784 ?        Ss   Oct24   0:21 /sbin/init noibrs splash

# lsof -p 1 | grep txt
systemd   1 root  txt       REG              252,1  1595792     927033 /lib/systemd/systemd
Run Code Online (Sandbox Code Playgroud)

所以,我的问题是:

  1. 为什么 Ubuntu 18.04 使用/sbin/init而不是/lib/systemd/systemd

  2. 为什么PID为1的进程却lsof -p 1 | grep txt返回?/lib/systemd/systemd/sbin/init

mjb*_*kmn 6

/sbin/init是一个符号链接/lib/systemd/systemd

看一下stat /sbin/initor的输出readlink /sbin/init

This is what they mean by systemd "running as /sbin/init". The systemd binary is linked as /sbin/init and started by that link name.

Update

To further explain the difference between the ps and lsof output: ps is showing the command that started the process, while lsof is showing which files a process has opened.

When systemd was started, it was called by /sbin/init noibrs splash, the file system resolved the link to the file /lib/systemd/systemd which was then read from disk and executed.