systemctl用法

最近开始使用systemd,相比于以往使用service命令,或者直接修改/etc/init.d,或者chkconfig这样多种工具搭配来完成一个系统服务的配置,部署,systemd的野心极大,它要让一切都回归统一,redhat系已经将systemd作为了其默认的系统服务管理工具。

这篇文章记录了一些systemd常用的用法,大都是我自己会用到的。

  1. 启动级别配置
    由于工作需要,常常会在虚拟机中安装各类linux系统,而绝大部分时间,我们需要的只是一个文字界面,能够通过host ssh到VM中,图形界面没必要起着,白白浪费cpu和内存,通过systemctl命令配置默认进入文字界面,只需一行:

    systemctl set-default multi-user.target

    如果要切回图形界面,相应地:

    systemctl set-default graphical.target

  2. 开机启动服务

    systemctl enable ***.service

  3. 停止开机启动服务

    systemctl disable ***.service

  4. 启动/停止/重启服务

    systemctl start/stop/restart ***.service

  5. 查询服务状态

    systemctl status ***.service

  6. 在远程机器上执行命令,类似ssh user@host “”

    systemctl -H user@hostname

  7. 列出系统服务,-t后用tab键可以关联出所有支持的unit类型

    systemctl -t service

  8. 列出所有已经加载的units的状态

    systemctl -a

  9. 列出加载的units/sockets/timers

    systemctl list-units [pattern]
    systemctl list-sockets [pattern]
    systemctl list-timers [pattern]

  10. 查询某项服务是否active,以sshd.serice为例

    systemctl is-active sshd.service

  11. 直接查看某项服务的配置文件,以sshd.serice为例

    systemctl cat sshd.service

  12. 查看服务的依赖关系,以sshd.serice为例

    systemctl list-dependencies sshd.service

  13. 查看环境变量

    systemctl show-environment

systemctl 还能实习非常之多的系统管理配置功能,不在这一一列举了。
关于systemd的背景,还有它在社区引起一些有意思的争论,甚至导致了debian的分裂,可以看看
why systemd?
upstart or systemd
Posittions forming in the Debian init system discuession

    原文作者:renweihub
    原文地址: https://segmentfault.com/a/1190000002457000
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞