命令比对:chkconfig、runlevel、init、shutdown、……systemd unit:我把它翻译成操作组件。
1.管理系统服务
1.1.对比命令语法
对比下老工具的用法,控制命令:
service | systemctl | 注解 |
---|---|---|
service name start | systemctl start name.service | 启动服务 |
service name status | systemctl status name.service | 察看状态 |
设置命令:
chkconfig | systemctl | 注解 |
---|---|---|
chkconfig --list name | systemctl status name.service | |
chkconfig --list |
systemctl list-unit-files --type service systemctl list-dependencies --after systemctl list-dependencies --before |
|
chkconfig name on |
systemctl enable name.service | |
chkconfig name off | systemctl disable name.service |
查看systemctl托管了那些服务
$ systemctl list-unit-files --type service
$ systemctl list-unit-files -t service
服务名称,可以不带扩展名:
# systemctl start sshd.service
# systemctl start sshd
还可以使用别名完成某些操作,查看别名如下:
[root@right sbin]# systemctl show runlevel6.target -p Names
Names=runlevel6.target reboot.target
1.2.状态信息
察看当前启动的服务:
[root@hhh ~]# systemctl list-units --type service
察看所有加载的服务:
[root@hhh ~]# systemctl list-units --type service -all
参看加载的服务,当前设定的状态:
[root@hhh ~]# systemctl list-unit-files --type service
参看服务的状态:
[root@hhh ~]# systemctl status sshd [root@hhh ~]# systemctl is-active sshd active [root@hhh ~]# systemctl is-enabled sshd enabled [root@hhh ~]# systemctl status sshd --after [root@hhh ~]# systemctl status sshd --before
1.3.启动、停止服务
管理一个服务通过操作该服务对应的target。
# 启动sshd: [root@right ~]# systemctl start sshd # 停止sshd: [root@right ~]# systemctl stop sshd # 重启sshd: [root@right ~]# systemctl restart sshd
1.4.启用、禁用服务
启用sshd:
[root@right ~]# systemctl enable sshd Created symlink from /etc/systemd/system/multi-user.target.wants/sshd.service to /usr/lib/systemd/system/sshd.service. [root@right ~]# systemctl disable sshd Removed symlink /etc/systemd/system/multi-user.target.wants/sshd.service.
查看sshd状态:
[root@right ~]# systemctl status sshd ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor preset: enabled) ......
启用时,回显信息是创建了符号链接,要是本身就是启用的,那么就不会创建。要是想重新创建该链接:
[root@right ~]# systemctl reenable sshd Removed symlink /etc/systemd/system/multi-user.target.wants/sshd.service. Created symlink from /etc/systemd/system/multi-user.target.wants/sshd.service to /usr/lib/systemd/system/sshd.service.
此外,可以屏蔽服务,以防止其手动启动或由被其他服务启动:
[root@right ~]# systemctl status sshd ● sshd.service Loaded: masked (/dev/null) ...... [root@right ~]# systemctl unmask sshd Removed symlink /etc/systemd/system/sshd.service. [root@right ~]# systemctl status sshd ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) ......
2.systemd targets
System Targets对应的运行级别:
Runlevel | Target Units | Description |
---|---|---|
0 |
runlevel0.target poweroff.target |
Shutdown the system. |
1 |
runlevel1.target rescue.target |
Setup a rescue shell. |
2 |
runlevel2.target multi-user.target |
multi-user system. |
3 |
runlevel3.target multi-user.target |
... |
4 |
runlevel4.target multi-user.target |
... |
5 |
runlevel5.target graphical.target |
... |
6 |
runlevel6.target reboot.target |
Shutdown and reboot the system |
查看当前运行级别
[root@right sbin]# systemctl get-default
graphical.target
查看当前加载的target
[root@right sbin]# systemctl list-units -type target
修改默认加载target组
[root@right sbin]# systemctl set-default runlevel3.target
修改当前加载target组
[root@right sbin]# systemctl isolate runlevel3.target
进入救援模式:
[root@right sbin]# systemctl rescue
进入紧急模式:
[root@right sbin]# systemctl emergency
3.关机…
对比过去用过的关机命令。
Old Command | New Command | Description |
---|---|---|
halt | systemctl halt | Halts the system |
poweroff | systemctl poweroff | Powers off the system |
reboot | systemctl reboot | Restarts the system |
pm-suspend | systemctl suspend | Suspends the system |
pm-hibernate | systemctl hibernate | Hibernates the system |
pm-suspend-hybrid | systemctl hybrid-sleep | Hibernates and Suspends the system |
# 关机(关闭系统、关闭电源):
# systemctl poweroff
# 重启系统:
# systemctl reboot
# 挂起系统(挂起到内存):
# systemctl suspend
# 休眠(挂起到硬盘):
# systemctl hibernate
4.Controlling on a Remote Machine
允许通过ssh协议控制机器,语法:“systemctl --host user@host command”;“--host root@r”代表选项、及其参数。
[root@right ~]# systemctl -H root@zc35 status firewalld root@zc35's password: ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) Active: inactive (dead) [root@right ~]# systemctl -H root@zc35 status sshd root@zc35's password: ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2016-12-16 09:57:32 CST; 3 weeks 2 days ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 871 CGroup: /system.slice/sshd.service
5.管理“操作组件”
操作组件对应一个操作组件文件,该文件是命令运行时的配置文件,文件名代表组件操作时系统引用的操作对象。系统管理员的组件目录位于“/etc/systemd/system/”。
目录 | 优先级 | 描述 |
---|---|---|
/usr/lib/systemd/system/ | 低 | rpm包安装时存放操作组件文件的位置 |
/run/systemd/system/ | 中 | 运行时操作组件存放位置 |
/etc/systemd/system/ | 高 | 被“systemctl enable”创建的组件文件 |
5.1理解操作组件文件的语法
例如“sshd.service”:
[root@right ~]# vi sshd.service [Unit] Description=OpenSSH server daemon Documentation=man:sshd(8) man:sshd_config(5) After=network.target sshd-keygen.service Wants=sshd-keygen.service [Service] EnvironmentFile=/etc/sysconfig/sshd ExecStart=/usr/sbin/sshd -D $OPTIONS ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target
理解上述文件选项含义:
选项 | 描述 |
---|---|
Description | 描述信息 |
Documentation | 参考文档 |
After | 指定在谁后边启动 |
Requires | 指定强依赖 |
Wants | 指定弱依赖 |
Conflicts | 与“Requires”相反 |
5.2创建操作组件
5.3修改操作组件
6.常见组件名称、及操作
- sshd
ssh - firewalld
防火墙 - httpd
apache - 启动级别
systemctl set-default multi-user systemctl get-default
systemctl .