识别自动启动的系统进程
系统启动的服务器进程由systemd
系统和服务器管理器进行管理
systemctl
可以缩写或者省略单元名称、进度数条目和单元说明
- 服务单元具有
.service
扩展名,代表系统服务 - 套接字单元具有
.socket
扩展名,代表进程之间通信 路径单元具有
.path
扩展名,用于将服务的激活推迟到特定文件系统更改发生之后服务状态
[student@localhost 桌面]$ systemctl status sshd
sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled)
Active: active (running) since 日 2017-07-30 22:38:24 CST; 2h 11min ago
Process: 6723 ExecStartPre=/usr/sbin/sshd-keygen (code=exited, status=0/SUCCESS)
Main PID: 6724 (sshd)
CGroup: /system.slice/sshd.service
└─6724 /usr/sbin/sshd -D
关键字 | 描述 |
---|---|
loaded | 单元配置文件已处理 |
active(tunning) | 正通过一个或多个持续进程运行 |
active(exited) | 已成功完成一次性配置 |
active(waiting) | 运行中,但是在等待事件 |
inactive | 不在运行 |
enable | 将在系统启动时启动 |
disabled | 不会iu在系统启动时启动 |
staic | 无法启用,但是可以在某一启用单元自动启动 |
使用systemctl列出单元文件
- 查询所有单元的状态,以验证系统启动
[student@localhost 桌面]$ systemctl
UNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys...t_misc.automount loaded active waiting Arbitrary Executable File Fo
sys-devi...-sda-sda1.device loaded active plugged VMware_Virtual_S
...
- 仅查询服务单元的状态
[student@localhost 桌面]$ systemctl --type=service
UNIT LOAD ACTIVE SUB DESCRIPTION
abrt-ccpp.service loaded active exited Install ABRT coredump hook
abrt-oops.service loaded active running ABRT kernel log watcher
abrt-xorg.service loaded active running ABRT Xorg log watcher
...
- 查询处于失败或者维护的任何单元
[student@localhost 桌面]$ systemctl status rngd.service
rngd.service - Hardware RNG Entropy Gatherer Daemon
Loaded: loaded (/usr/lib/systemd/system/rngd.service; enabled)
Active: active (running) since 日 2017-07-30 21:01:49 CST; 4h 14min ago
Main PID: 911 (rngd)
CGroup: /system.slice/rngd.service
└─911 /sbin/rngd -f
- 显示特定单元的活动和启用
[student@localhost 桌面]$ systemctl is-active sshd
active
[student@localhost 桌面]$ systemctl is-enabled sshd
enabled
- 列出所有已加载单元的活动状态
[student@localhost 桌面]$ systemctl list-units --type=service
UNIT LOAD ACTIVE SUB DESCRIPTION
abrt-ccpp.service loaded active exited Install ABRT coredump hook
abrt-oops.service loaded active running ABRT kernel log watcher
abrt-xorg.service loaded active running ABRT Xorg log watcher
- 查看所有单元已启用和已禁用设置
[student@localhost 桌面]$ systemctl list-unit-files --type=service
UNIT FILE STATE
abrt-ccpp.service enabled
abrt-oops.service enabled
abrt-pstoreoops.service disabled
abrt-vmcore.service enabled
abrt-xorg.service enabled
abrtd.service enabled
accounts-daemon.service enabled
alsa-restore.service static
...
- 仅查看失败的服务
[student@localhost 桌面]$ systemctl --failed --type=service
UNIT LOAD ACTIVE SUB DESCRIPTION
kdump.service loaded failed failed Crash recovery kernel arming
rhnsd.service loaded failed failed LSB: Starts the Spacewalk Daemon
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.
2 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
控制系统服务
1.验证进程正在运行
[student@localhost 桌面]$ ps -up 6724
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 6724 0.0 0.0 82956 104 ? Ss 7月30 0:00 /usr/sbin/sshd
[student@localhost 桌面]$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.5 53912 5672 ? Ss 7月30 0:04 /usr/lib/syste
root 2 0.0 0.0 0 0 ? S 7月30 0:00 [kthreadd]
...
2.停止服务(注意权限问题)
[root@localhost ~]# systemctl stop sshd.service
3.启动服务(进程ID已经改变)
[root@localhost ~]# systemctl start sshd.service
4.重启服务
[root@localhost ~]# systemctl restart sshd.service
5.重新加载配置文件(进程ID不变)
[root@localhost ~]# systemctl reload sshd.service
5.屏蔽服务
系统中装了相互冲突的服务
屏蔽将在配置目录中创建连接,使得启动服务时什么都不会发生
[root@localhost ~]# systemctl mask sshd.service
ln -s '/dev/null' '/etc/systemd/system/sshd.service'
[root@localhost ~]# systemctl unmask sshd.service
rm '/etc/systemd/system/sshd.service'
使系统守护进程在系统启动时启动或停止
1.禁用服务,禁用服务并不会iu停止服务。
[root@localhost ~]# systemctl disable sshd.service
rm '/etc/systemd/system/multi-user.target.wants/sshd.service'
2.启用服务
[root@localhost ~]# systemctl enable sshd.service
ln -s '/usr/lib/systemd/system/sshd.service' '/etc/systemd/system/multi-user.target.wants/sshd.service'
[root@localhost ~]# systemctl is-enabled sshd.service
enabled
systemctl命令摘要
任务 | 命令 |
---|---|
查看服务详细信息 | systemctl status httpd.service |
停止某服务 | systemctl stop httpd.service |
启动某服务 | systemctl start httpd.service |
重启某服务 | systemctl restart httpd.service |
重新加载服务配置 | systemctl reload httpd.service |
屏蔽服务 | systemctl musk httpd.service |
解除屏蔽 | systemctl unmask http.service |
使某服务自动启动 | systemctl enable httpd.service |
使某服务不自动启动 | systemctl disable httpd.service |
检查服务状态 | systemctl is-active httpd.service (仅显示是否 Active) |
显示所有已启动的服务 | systemctl list-units –type=service |