0.基础知识准备。需要先了解系统的启动基本流程:
POST ---> Boot Sequence ---> Bootloader ---> kernel + iniramfs (initrd) --->rootfs ---> /sbin/init (centos5:SysV init / centos6:Upstart / centos7:Systemd)
1.初步了解Systemd。
yum install -y psmisc安装软件包。pstree查看进程树。
[root@lab1 ~]# yum install -y psmisc
[root@lab1 ~]# pstree
systemd─┬─NetworkManager─┬─dhclient
│ └─2*[{NetworkManager}]
├─VGAuthService
├─agetty
├─auditd───{auditd}
├─chronyd
├─crond
├─dbus-daemon───{dbus-daemon}
├─firewalld───{firewalld}
├─irqbalance
├─lvmetad
├─master─┬─pickup
│ └─qmgr
├─polkitd───5*[{polkitd}]
├─rsyslogd───2*[{rsyslogd}]
├─sshd───sshd───bash───pstree
├─systemd-journal
├─systemd-logind
├─systemd-udevd
├─tuned───4*[{tuned}]
└─vmtoolsd───{vmtoolsd}
2.关于控制服务状态。
rpm -qi httpd查看常用的服务httpd的安装情况(没安装的自行安装,这里不列明步骤了)。systemctl start httpd.service启动服务。systemctl status httpd查看服务状态。systemctl stop httpd关闭服务。systemctl status httpd再次查看服务状态。
[root@lab1 ~]# rpm -qi httpd
Name : httpd
Version : 2.4.6
Release : 88.el7.centos
Architecture: x86_64
Install Date: Fri 04 Jan 2019 11:35:44 PM EST
Group : System Environment/Daemons
Size : 9817309
License : ASL 2.0
Signature : RSA/SHA256, Mon 12 Nov 2018 09:28:53 AM EST, Key ID 24c6a8a7f4a80eb5
Source RPM : httpd-2.4.6-88.el7.centos.src.rpm
Build Date : Sun 04 Nov 2018 08:48:57 PM EST
Build Host : x86-01.bsys.centos.org
Relocations : (not relocatable)
Packager : CentOS BuildSystem <http://bugs.centos.org>
Vendor : CentOS
URL : http://httpd.apache.org/
Summary : Apache HTTP Server
Description :
The Apache HTTP Server is a powerful, efficient, and extensible
web server.
[root@lab1 ~]# systemctl start httpd.service
[root@lab1 ~]# 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 2019-01-06 01:59:33 EST; 13s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 2281 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─2281 /usr/sbin/httpd -DFOREGROUND
├─2282 /usr/sbin/httpd -DFOREGROUND
├─2283 /usr/sbin/httpd -DFOREGROUND
├─2284 /usr/sbin/httpd -DFOREGROUND
├─2285 /usr/sbin/httpd -DFOREGROUND
└─2286 /usr/sbin/httpd -DFOREGROUND
Jan 06 01:59:32 lab1.example.com systemd[1]: Starting The Apache HTTP Server...
Jan 06 01:59:33 lab1.example.com systemd[1]: Started The Apache HTTP Server.
[root@lab1 ~]# systemctl stop httpd
[root@lab1 ~]# 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)
Jan 06 01:59:32 lab1.example.com systemd[1]: Starting The Apache HTTP Server...
Jan 06 01:59:33 lab1.example.com systemd[1]: Started The Apache HTTP Server.
Jan 06 02:01:18 lab1.example.com systemd[1]: Stopping The Apache HTTP Server...
Jan 06 02:01:19 lab1.example.com systemd[1]: Stopped The Apache HTTP Server.
3.关于当前服务状态。
systemctl is-active httpd检查服务状态。systemctl start httpd启动服务。systemctl is-active httpd再次检查服务状态。systemctl list-units --type service列出所有活动的服务。systemctl list-units --type service --all列出所有服务的当前状态(包括不活动的服务)。
[root@lab1 ~]# systemctl is-active httpd
unknown
[root@lab1 ~]# systemctl start httpd
[root@lab1 ~]# systemctl is-active httpd
active
[root@lab1 ~]# systemctl list-units --type service | head
UNIT LOAD ACTIVE SUB DESCRIPTION
auditd.service loaded active running Security Auditing Service
chronyd.service loaded active running NTP client/server
crond.service loaded active running Command Scheduler
dbus.service loaded active running D-Bus System Message Bus
firewalld.service loaded active running firewalld - dynamic firewall daemon
[email protected] loaded active running Getty on tty1
httpd.service loaded active running The Apache HTTP Server
irqbalance.service loaded active running irqbalance daemon
kdump.service loaded active exited Crash recovery kernel arming
[root@lab1 ~]# systemctl list-units --type service --all | grep inactive | head
brandbot.service loaded inactive dead Flexible Branding Service
cpupower.service loaded inactive dead Configure CPU power related settings
● display-manager.service not-found inactive dead display-manager.service
dm-event.service loaded inactive dead Device-mapper event daemon
dracut-shutdown.service loaded inactive dead Restore /run/initramfs
ebtables.service loaded inactive dead Ethernet Bridge Filtering tables
emergency.service loaded inactive dead Emergency Shell
● exim.service not-found inactive dead exim.service
● ip6tables.service not-found inactive dead ip6tables.service
● ipset.service not-found inactive dead ipset.service
4.关于开机启动状态。
systemctl status httpd查看当前状态(开机不启动)。systemctl enable httpd设置为开机启动。systemctl status httpd再次查看当前状态(开机启动)。systemctl list-unit-files --type service查看所有服务的开机启动状态。systemctl is-enabled httpd查看是否处于开机启动状态。
[root@lab1 ~]# systemctl status httpd | grep loaded
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
[root@lab1 ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@lab1 ~]# systemctl status httpd | grep loaded
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
[root@lab1 ~]# systemctl list-unit-files --type service | head
UNIT FILE STATE
arp-ethers.service disabled
auditd.service enabled
[email protected] enabled
blk-availability.service disabled
brandbot.service static
[email protected] static
chrony-wait.service disabled
chronyd.service enabled
console-getty.service disabled
[root@lab1 ~]# systemctl is-enabled httpd
enabled
5.关于运行级别
systemctl list-units --type target查看目前处于活动状态的运行级别。systemctl list-units --type target --all查看所有的运行级别。
[root@lab1 ~]# systemctl list-units --type target
UNIT LOAD ACTIVE SUB DESCRIPTION
basic.target loaded active active Basic System
bluetooth.target loaded active active Bluetooth
cryptsetup.target loaded active active Encrypted Volumes
getty.target loaded active active Login Prompts
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
paths.target loaded active active Paths
remote-fs.target loaded active active Remote File Systems
slices.target loaded active active Slices
sockets.target loaded active active Sockets
sound.target loaded active active Sound Card
swap.target loaded active active Swap
sysinit.target loaded active active System Initialization
timers.target loaded active active Timers
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.
18 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
[root@lab1 ~]# systemctl list-units --type target --all
UNIT LOAD ACTIVE SUB DESCRIPTION
basic.target loaded active active Basic System
bluetooth.target loaded active active Bluetooth
cryptsetup.target loaded active active Encrypted Volumes
emergency.target loaded inactive dead Emergency Mode
final.target loaded inactive dead Final Step
getty.target loaded active active Login Prompts
graphical.target loaded inactive dead 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
nss-lookup.target loaded inactive dead Host and Network Name Lookups
nss-user-lookup.target loaded inactive dead User and Group Name Lookups
paths.target loaded active active Paths
remote-fs-pre.target loaded inactive dead Remote File Systems (Pre)
remote-fs.target loaded active active Remote File Systems
rescue.target loaded inactive dead Rescue Mode
shutdown.target loaded inactive dead Shutdown
slices.target loaded active active Slices
sockets.target loaded active active Sockets
sound.target loaded active active Sound Card
swap.target loaded active active Swap
sysinit.target loaded active active System Initialization
● syslog.target not-found inactive dead syslog.target
time-sync.target loaded inactive dead System Time Synchronized
timers.target loaded active active Timers
umount.target loaded inactive dead Unmount All Filesystems
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.
[root@lab1 ~]# systemctl get-default
multi-user.target
6.关于依赖关系。
systemctl list-dependencies httpd可以查看某一服务其依赖的其他服务。
[root@lab1 ~]# systemctl list-dependencies httpd
httpd.service
● ├─-.mount
● ├─system.slice
● └─basic.target
● ├─microcode.service
● ├─rhel-autorelabel-mark.service
● ├─rhel-autorelabel.service
● ├─rhel-configure.service
● ├─rhel-dmesg.service
● ├─rhel-loadmodules.service
● ├─[email protected]
● ├─paths.target
● ├─slices.target
● │ ├─-.slice
● │ └─system.slice
● ├─sockets.target
● │ ├─dbus.socket
● │ ├─dm-event.socket
● │ ├─systemd-initctl.socket
● │ ├─systemd-journald.socket
● │ ├─systemd-shutdownd.socket
● │ ├─systemd-udevd-control.socket
● │ └─systemd-udevd-kernel.socket
● ├─sysinit.target
● │ ├─dev-hugepages.mount
● │ ├─dev-mqueue.mount
● │ ├─kmod-static-nodes.service
● │ ├─lvm2-lvmetad.socket
● │ ├─lvm2-lvmpolld.socket
● │ ├─lvm2-monitor.service
● │ ├─plymouth-read-write.service
● │ ├─plymouth-start.service
● │ ├─proc-sys-fs-binfmt_misc.automount
● │ ├─sys-fs-fuse-connections.mount
● │ ├─sys-kernel-config.mount
● │ ├─sys-kernel-debug.mount
7.关于手动编辑文件
cd /usr/lib/systemd/system转到指定目录下。cat httpd.service | grep ^# -v | grep ^$ -v查看某一服务其配置文件的特定格式(照此格式可以手动编辑服务的配置文件)。
[root@lab1 ~]# cd /usr/lib/systemd/system
[root@lab1 system]# cat httpd.service | grep ^# -v | grep ^$ -v
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target