🏡博客主页: Passerby_Wang的博客_CSDN博客-系统运维,云计算,Linux基础领域博主
🌐所属专栏:『Linux基础』
🌌上期文章: Linux基础 - 系统安全(SELinux与Firewalld)
📰如觉得博主文章写的不错或对你有所帮助的话,还望大家多多支持呀! 关注、点赞、收藏、评论。
目录
systemd即为system daemon,是linux下的一种init软件,由Lennart Poettering带头开发,systemd这一名字源于Unix中的一个惯例:在Unix中常以"d"作为系统守护进程(英语:daemon,亦称后台进程)的后缀标识。除此以外,systemd亦是借代英文术语D体系,而这一术语即是用于描述一个人具有快速地适应环境并解决困难的能力。systemd已纳入众多Linux发行版的软件源中。
Systemd是一个更高效的系统和服务管理器,是内核引导之后加载的第一个初始化进程(PID=1),负责掌控整个Linux的运行/服务资源组合,在开机时服务并行启动,各系统服务间的精确依赖。
配置目录:/etc/systemd/system
脚本目录:/run/systemd/system
服务目录:/lib/systemd/system
Systemd 可以管理的系统资源。不同的资源统称为 unit(单元),unit有13种。
| 序号 | 资源名称 | 含义 |
| 1 | service | 系统服务 |
| 2 | socket | 用于标识进程间通信的socket |
| 3 | busname | 用于设定与此服务通信所使用的D-Bus名称 |
| 4 | target | 用于模拟实现“运行级别” |
| 5 | snapshot | 管理系统快照 |
| 6 | device | 硬件设备 |
| 7 | mount | 文件系统挂载点 |
| 8 | automount | 自动挂载点 |
| 9 | swap | 用于标识swap设备(交换空间) |
| 10 | timer | 定时器 |
| 11 | path | 文件或路径 |
| 12 | slice | 进程组 |
| 13 | scope | 不是由systemd启动的外部进程 |
systemctl start unit #启动服务
systemctl stop unit #停止服务
systemctl restart unit #重启服务
systemctl enable unit #开机自启服务
systemctl enable --now unit #设置开机自启并立即开启服务
systemctl is-enable unit #查看服务是否开机自启
systemctl disable unit #禁止开机自启服务
systemctl kill unit #杀死unit的所有进程
systemctl reload unit #重新加载unit的配置文件
systemctl daemon-reload #重新加载所有修改过的配置文件
[root@wangwu ~]# systemctl start httpd
- [root@wangwu ~]# systemctl status httpd
-
- ● httpd.service - The Apache HTTP Server
-
- Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
-
- Active: active (running) since Sun 2022-10-23 21:08:08 CST; 1min 2s ago
-
- Docs: man:httpd(8)
-
- man:apachectl(8)
-
- Main PID: 110860 (httpd)
-
- Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
-
- Tasks: 6
-
- CGroup: /system.slice/httpd.service
-
- ├─110860 /usr/sbin/httpd -DFOREGROUND
-
- ├─110861 /usr/sbin/httpd -DFOREGROUND
-
- ├─110862 /usr/sbin/httpd -DFOREGROUND
-
- ├─110863 /usr/sbin/httpd -DFOREGROUND
-
- ├─110864 /usr/sbin/httpd -DFOREGROUND
-
- └─110865 /usr/sbin/httpd -DFOREGROUND
-
-
-
- Oct 23 21:08:08 wangwu systemd[1]: Starting The Apache HTTP Server...
-
- Oct 23 21:08:08 wangwu httpd[110860]: AH00558: httpd: Could not reliably d...ge
-
- Oct 23 21:08:08 wangwu systemd[1]: Started The Apache HTTP Server.
注:
Loaded行:配置文件的位置,是否设为开机启动
Active行:表示正在运行
Main PID行:主进程ID
Status行:由应用本身提供的软件当前状态
CGroup块:应用的所有子进程
日志块:应用的日志
[root@wangwu ~]# systemctl stop httpd
- [root@wangwu ~]# systemctl status httpd
-
- ● httpd.service - The Apache HTTP Server
-
- Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
-
- Active: inactive (dead)
-
- Docs: man:httpd(8)
-
- man:apachectl(8)
-
-
-
- Sep 29 17:45:02 wangwu systemd[1]: Unit httpd.service cannot be reloaded b...e.
-
- Oct 23 21:08:08 wangwu systemd[1]: Starting The Apache HTTP Server...
-
- Oct 23 21:08:08 wangwu httpd[110860]: AH00558: httpd: Could not reliably d...ge
-
- Oct 23 21:08:08 wangwu systemd[1]: Started The Apache HTTP Server.
-
- Oct 23 21:24:02 wangwu httpd[116627]: AH00558: httpd: Could not reliably d...ge
-
- Oct 23 21:24:02 wangwu systemd[1]: Reloaded The Apache HTTP Server.
-
- Oct 23 21:24:09 wangwu systemd[1]: Stopping The Apache HTTP Server...
-
- Oct 23 21:24:10 wangwu systemd[1]: Stopped The Apache HTTP Server.
[root@wangwu ~]# systemctl restart httpd
- [root@wangwu ~]# systemctl enable httpd
-
- Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
-
-
- [root@wangwu ~]# systemctl enable --now httpd
-
-
- [root@wangwu ~]# systemctl is-enabled httpd
-
- enabled
- [root@wangwu ~]# systemctl disable httpd
-
- Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
-
- [root@wangwu ~]# systemctl is-enabled httpd
-
- disabled
- [root@wangwu ~]# systemctl kill httpd
-
-
- [root@wangwu ~]# systemctl reload httpd
-
-
- [root@wangwu ~]# systemctl daemon-reload
systemctl reboot #重启系统
systemctl poweroff #退出系统并关闭电源
systemctl halt #CPU停止工作
systemctl suspend #挂起系统
systemctl hibernate #系统休眠
systemctl hybrid-sleep #系统休眠并挂起:
systemctl -t service #查看活动的系统服务
systemctl -t service --all #列出所有系统服务(包括不活跃的)
[root@wangwu ~]# systemctl -t service
UNIT LOAD ACTIVE SUB DESCRIPTION
abrt-ccpp.service loaded active exited Install ABRT coredump hook
注:
UNIT #单元
LOA #是否存在
ACTIVE #是否激活
SUB #状态
DESCRIPTION #描述
- [root@wangwu ~]# systemctl -t service --all
-
- UNIT LOAD ACTIVE SUB DESCRIPTION
-
- ● apparmor.service not-found inactive dead apparmor.service
系统的运行级别,分为七个级别,每种运行级别代表特定的操作模式,分别用数字0-6表示。
| RHEL5、6 | RHEL7 | ||
| 级别 | 含义 | 对应级别 | 含义 |
| 0 | 关机 | poweroff.target | 关机 |
| 1 | 单用户模式 | rescure.target | 救援模式 |
| 2 | 不完全的命令行模式,不包含NFS | multi-user.target | 非图形界面的多用户方式 |
| 3 | 完全命令行模式,标准字符界面 | multi-user.target | 非图形界面的多用户方式 |
| 4 | 系统保留 | multi-user.target | 非图形界面的多用户方式 |
| 5 | 图形模式 | graphical.target | 图形界面的多用户方式 |
| 6 | 重启 | reboot.target | 重启 |
systemctl -t target #查看可用运行级别
systemctl isolate multi-user.target #切换到文本模式
systemctl isolate graphical.target #切换到图形模式
- [root@wangwu ~]# systemctl -t target
-
- UNIT LOAD ACTIVE SUB DESCRIPTION
-
- basic.target loaded active active Basic System
-
- cryptsetup.target loaded active active Local Encrypted Volumes
-
- getty-pre.target loaded active active Login Prompts (Pre)
-
- getty.target loaded active active Login Prompts
-
- graphical.target loaded active active Graphical Interface
-
- local-fs-pre.target loaded active active Local File Systems (Pre)
-
- local-fs.target loaded active active Local File Systems
-
- multi-user.target loaded active active Multi-User System
-
- network-online.target loaded active active Network is Online
-
- network-pre.target loaded active active Network (Pre)
-
- network.target loaded active active Network
-
- nfs-client.target loaded active active NFS client services
-
- nss-user-lookup.target loaded active active User and Group Name Lookups
-
- paths.target loaded active active Paths
-
- remote-fs-pre.target loaded active active Remote File Systems (Pre)
-
- rpc_pipefs.target loaded active active rpc_pipefs.target
-
- slices.target loaded active active Slices
-
- sockets.target loaded active active Sockets
-
- swap.target loaded active active Swap
-
- sysinit.target loaded active active System Initialization
-
- timers.target loaded active active Timers
- [root@wangwu ~]# systemctl isolate multi-user.target
-
- [root@wangwu ~]# systemctl isolate graphical.target
命令
systemctl get-default
实例
- [root@wangwu ~]# systemctl get-default #查看默认级别
-
- graphical.target #图形模式
常用命令
systemctl set-default multi-user.target
systemctl set-default graphica.target
实例
- [root@wangwu ~]# systemctl set-default multi-user.target #设置默认级别为文本模式
-
- Removed symlink /etc/systemd/system/default.target.
-
- Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
-
- [root@wangwu ~]# systemctl get-default
-
- multi-user.target #文本模式
- [root@wangwu ~]# systemctl set-default graphical.target #设置默认级别为图形模式
-
- Removed symlink /etc/systemd/system/default.target.
-
- Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.
-
- [root@wangwu ~]# systemctl get-default
-
- graphical.target #图形模式