Linux的systemd相关知识学习


Linux中有很多命令已经存在了N多年,渐渐有些已被一些新的命令所代替,不过由于习惯的原因,很多时候我们并不能一下子适应过来,例如:ifconfig之于ip命令。

最近在使用ArchLinux的时候用到了systemctl命令,虽然之前在 “Linux中国” 上看到了关于 systemd 的文章,不过当时没有什么感觉,认为自己一时半会是不会用到这个工具的,但世事难料,没想到这么快我就要花时间研究这个命令并在实际中使用了。

systemctl是systemd下的一个工具。网上查了下,该命令是用来替代service和chkconfig两个命令的。

参考链接:
参考解答:
  • 系统环境:4.0.7-2-ARCH
  • 所在路径:/usr/bin/systemctl
#将httpd服务设为开机自动启动(enable)

systemctl enable httpd.service

#检查httpd服务是否为开机启动(is-enabled)

systemctl is-enabled httpd.service

#禁止httpd服务开机自动启动(disable)

systemctl disable httpd.service

#查看httpd服务的运行状态(status)

systemctl status httpd.service

#启动httpd服务(start)

systemctl start httpd.service

#检查httpd服务是否处于活动状态(is-active)

systemctl is-active httpd.service

#停止httpd服务(stop)

systemctl stop httpd.service

#重新加载httpd服务(reload)

systemctl reload httpd.service

#重新启动httpd服务(restart)

systemctl restart httpd.service

#显示启动失败的服务(--failed
# systemctl --failed

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

==

1.更快的启动速度/精准分析
# systemd-analyze  #查看系统引导用时
Startup finished in 1.520s (kernel) + 709ms (userspace) = 2.229s

# systemd-analyze blame #以进程初始化所占用时间排序

# systemd-analyze critical-chain    #分析启动时的关键链

# systemd-analyze critical-chain httpd.service  #分析某个服务(httpd)的关键链
2.列出正在运行/可用/所有的units
# systemctl
# systemctl list-units      #列出所有运行中单元
# systemctl list-unit-files #列出所有可用单元

# systemctl list-unit-files --type=service  #列出所有服务(包括启用的和禁用的)
# systemctl list-unit-files --type=mount    #列出所有系统挂载点
# systemctl list-unit-files --type=socket   #列出所有可用系统套接口
3.列出失败的units
systemctl --failed
4.systemd和sysVinit

Systemd有一个完成与sysVinit的runlevels相似任务的构想。

sysVinit的runlevels大多是以数字分级的,下面是runlevels在systemd中的对应元素:

  •   0 runlevel0.target, poweroff.target
  •   1, s, single runlevel1.target, rescue.target
  •   2, 4 runlevel2.target, runlevel4.target, multi-user.target
  •   3 runlevel3.target, multi-user.target
  •   5 runlevel5.target, graphical.target
  •   6 runlevel6.target, reboot.target
  •   emergency emergency.target
5.systemd的日志

#journald是systemd独有的日志系统,替换了sysVinit中的syslog守护进程。可以用命令journalctl来读取日志

journald

#运行journalctl -b命令来查看所有引导日志

journalctl -b

#实时显示系统日志(类似tail -f)

journalctl -f

#查看特定服务的日志

journalctl /usr/bin/dhcpcd

6.电源管理

#systemctl命令也可以用来关机,重启或者休眠。要关机、重启、挂起和休眠,分别使用如下命令:

  • systemctl poweroff
  • systemctl reboot
  • systemctl suspend
  • systemctl hibernate
7.在systemd中显示系统信息的新方法

hostnamectl

8.其它
# systemctl get-default     #列出当前使用的运行等级

# systemctl show httpd  #检查某个服务的所有配置细节

# systemd-analyze critical-chain httpd.service  #分析某个服务(httpd)的关键链

# systemctl list-dependencies httpd.service #获取某个服务(httpd)的依赖性列表

=EOF=


《 “Linux的systemd相关知识学习” 》 有 13 条评论

  1. 在CentOS 7上查看/启动/停止MySQL服务:
    `
    systemctl status/start/stop mysql.service
    `

  2. 有时候会忘了服务的名称,比如OpenSSH服务的名称到底是:ssh还是sshd?
    一方面可以直接尝试;
    另一方面可以通过查看对应的「.service」文件名称来获取(在CentOS 7上默认是在「/usr/lib/systemd/system/」目录中):
    `$ sudo find / -type f -name “*.service”
    $ ls /usr/lib/systemd/system/*.service
    `

  3. 一般情况下 `systemd` 中可以通过 `systemctl` 命令进行「启动(start)/停止(stop)/重启(restart)/开机自启动(enable)」等设置,但对于类似于Nginx这样的服务来说,如果只是简单修改了一下配置文件,最好还是希望能通过重新加载配置文件的方式避免重启,但我又不清楚它是否支持 `reload` 这样的命令,这时就可以通过查看服务对应的 .service 文件来判定该服务是否支持 reload 等操作:
    `
    # find / -type f -iname “nginx.service”
    # cat /usr/lib/systemd/system/nginx.service

    ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
    ExecReload=/bin/kill -s HUP $MAINPID
    ExecStop=/bin/kill -s QUIT $MAINPID

    `

  4. Linux pid 1 和 systemd
    http://coolshell.cn/articles/17998.html
    `
    sysvinit 1983年(把整个操作系统带入可操作的状态,串行启动,通过各种启停脚本)
    UpStart 2006年(基于事件驱动的机制,把之前的完全串行的同步启动服务的方式改成了由事件驱动的异步的方式)
    systemd 2010年(按需启动,能不启动就不启动,如果要启动,能并行启动就并行启动,包括你们之间有依赖,我也并行启动)
    `

  5. 加固 systemd 服务
    https://blog.lilydjwg.me/2018/2/10/harden-a-systemd-service.212145.html
    `
    [Unit]
    Description=Graphite/Carbon
    After=network.target

    [Service]
    Type=forking
    ExecStart=/usr/bin/carbon-cache.py –config=/etc/carbon/carbon.conf start
    User=carbon

    PrivateTmp=true
    PrivateDevices=true
    ProtectSystem=full
    ProtectHome=true
    NoNewPrivileges=true
    CapabilityBoundingSet=

    ReadOnlyPaths=/
    ReadWritePaths=/run
    ReadWritePaths=/var/log/carbon
    ReadWritePaths=/var/lib/carbon

    [Install]
    WantedBy=multi-user.target
    `

  6. 清理systemd日志
    https://tlanyan.me/clear-systemd-journal-logs/
    https://linuxhandbook.com/clear-systemd-journal-logs/
    `
    journalctl提供了三种清理systemd日志的方式。

    第一种是清理指定时间之前的日志:
    # 清理7天之前的日志
    sudo journalctl –vacuum-time=7d
    # 清理2小时之前的日志
    sudo journactl –vacuum-time=2h
    # 清理10秒之前的日志
    sudo journalctl –vacuum-time=10s
    # 上述命令示例输出:
    # Vacuuming done, freed 3.7G of archived journals on disk.

    第二种是限制日志占用的空间大小:
    # 限制systemd日志占用不超过1G空间
    sudo journalctl –vacuum-size=1G
    # 限制systemd日志占用不超过100M
    sudo journalctl –vacuum-size=100M
    # 输出与第一种类似

    第三种是保留日志文件个数:
    # 保留最近的5个日志文件
    sudo journalctl –vacuum-files=5
    # 输出与第一种类似

    第四种方法:
    编辑配置 /etc/systemd/journald.conf 文件
    `

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注