hellopasswd
systemd
- systemctl list-units --all --type=service
- 几个常用的服务相关的命令
- systemctl enable crond.service #让服务开机启动
- systemctl disable crond #不让开机启动
- systemctl status crond #查看状态
- systemctl stop crond #停止服务
- systemctl start crond #启动服务
- systemctl restart crond #重启服务
- systemctl is-enabled crond #检查服务是否开机启动
[root@localhost ~]# systemctl list-unit-files
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
proc-fs-nfsd.mount static
proc-sys-fs-binfmt_misc.mount static
sys-fs-fuse-connections.mount static
sys-kernel-config.mount static
sys-kernel-debug.mount static
tmp.mount disabled
var-lib-nfs-rpc_pipefs.mount static
brandbot.path disabled
systemd-ask-password-console.path static
systemd-ask-password-plymouth.path static
systemd-ask-password-wall.path static
session-1.scope static
session-2.scope static
session-26.scope static
lines 1-18
[root@localhost ~]# systemctl list-units --all --type=service
UNIT LOAD ACTIVE SUB DESCRIPTION
auditd.service loaded active running Security Auditing Service
auth-rpcgss-module.service loaded inactive dead Kernel Module supporting RPCSEC_G
avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack
brandbot.service loaded inactive dead Flexible Branding Service
cpupower.service loaded inactive dead Configure CPU power related setti
crond.service loaded active running Command Scheduler
dbus.service loaded active running D-Bus System Message Bus
● 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
firewalld.service loaded active running firewalld - dynamic firewall daem
getty@tty1.service loaded active running Getty on tty1
gssproxy.service loaded active running GSSAPI Proxy Daemon
ip6tables.service loaded inactive dead IPv6 firewall with ip6tables
lines 1-18
不加--all
[root@localhost ~]# systemctl list-units --type=service
UNIT LOAD ACTIVE SUB DESCRIPTION
auditd.service loaded active running Security Auditing Service
avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack
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
getty@tty1.service loaded active running Getty on tty1
gssproxy.service loaded active running GSSAPI Proxy Daemon
iprdump.service loaded active running LSB: Start the ipr dump daemon
iprinit.service loaded active running LSB: Start the ipr init daemon
iprupdate.service loaded active running LSB: Start the iprupdate utility
kdump.service loaded active exited Crash recovery kernel arming
kmod-static-nodes.service loaded active exited Create list of required static device
lvm2-lvmetad.service loaded active running LVM2 metadata daemon
lvm2-monitor.service loaded active exited Monitoring of LVM2 mirrors, snapshots
lvm2-pvscan@8:17.service loaded active exited LVM2 PV scan on device 8:17
lvm2-pvscan@8:18.service loaded active exited LVM2 PV scan on device 8:18
lvm2-pvscan@8:19.service loaded active exited LVM2 PV scan on device 8:19
lines 1-18
[root@localhost ~]# systemctl enable crond.service
[root@localhost ~]# systemctl disable crond.service
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@localhost ~]# systemctl enable crond.service
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
[root@localhost ~]# systemctl status crond.service
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2017-12-05 00:02:52 CST; 4h 42min ago
Main PID: 3052 (crond)
CGroup: /system.slice/crond.service
└─3052 /usr/sbin/crond -n
Dec 05 00:02:52 localhost.localdomain crond[3052]: (CRON) INFO (RANDOM_DELAY will be scaled wi....)
Dec 05 00:02:52 localhost.localdomain systemd[1]: Started Command Scheduler.
Dec 05 00:02:52 localhost.localdomain crond[3052]: (CRON) INFO (running with inotify support)
Dec 05 00:02:52 localhost.localdomain systemd[1]: Starting Command Scheduler...
Dec 05 00:02:52 localhost.localdomain crond[3052]: (CRON) INFO (@reboot jobs will be run at co....)
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# systemctl is-enabled crond.service
enabled
disable的路径
[root@localhost ~]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service
lrwxrwxrwx 1 root root 37 Dec 5 04:44 /etc/systemd/system/multi-user.target.wants/crond.service -> /usr/lib/systemd/system/crond.service
可以看出这是个链接路径,而真正的路径/usr/lib/systemd/system/crond.service
[root@localhost ~]# systemctl disable crond.service
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@localhost ~]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service
ls: cannot access /etc/systemd/system/multi-user.target.wants/crond.service: No such file or directory
unit
- ls /usr/lib/systemd/system #系统所有unit,分为以下类型
- service 系统服务
- target 多个unit组成的组
- device 硬件设备
- mount 文件系统挂载点
- automount 自动挂载点
- path 文件或路径
- scope 不是由systemd启动的外部进程
- slice 进程组
- snapshot 不是由systemd启动的外部进程
- socket systemd快照
- swap swap文件
- timer 定时器
系统所有unit
[root@localhost ~]# ls /usr/lib/systemd/system/
arp-ethers.service quotaon.service
auditd.service rc-local.service
auth-rpcgss-module.service rdisc.service
autovt@.service reboot.target
avahi-daemon.service reboot.target.wants
avahi-daemon.socket remote-fs-pre.target
basic.target remote-fs.target
basic.target.wants rescue.service
blk-availability.service rescue.target
bluetooth.target rescue.target.wants
brandbot.path rhel-autorelabel-mark.service
brandbot.service rhel-autorelabel.service
console-getty.service rhel-configure.service
console-shell.service rhel-dmesg.service
container-getty@.service rhel-domainname.service
cpupower.service rhel-import-state.service
.
.
.
[root@localhost ~]# ls -l /usr/lib/systemd/system/runlevel*
lrwxrwxrwx. 1 root root 15 Nov 23 15:36 /usr/lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 Nov 23 15:36 /usr/lib/systemd/system/runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 Nov 23 15:36 /usr/lib/systemd/system/runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 Nov 23 15:36 /usr/lib/systemd/system/runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 Nov 23 15:36 /usr/lib/systemd/system/runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 Nov 23 15:36 /usr/lib/systemd/system/runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 Nov 23 15:36 /usr/lib/systemd/system/runlevel6.target -> reboot.target
/usr/lib/systemd/system/runlevel1.target.wants:
total 0
lrwxrwxrwx. 1 root root 39 Nov 23 15:36 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service
/usr/lib/systemd/system/runlevel2.target.wants:
total 0
lrwxrwxrwx. 1 root root 39 Nov 23 15:36 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service
/usr/lib/systemd/system/runlevel3.target.wants:
total 0
lrwxrwxrwx. 1 root root 39 Nov 23 15:36 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service
/usr/lib/systemd/system/runlevel4.target.wants:
total 0
lrwxrwxrwx. 1 root root 39 Nov 23 15:36 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service
/usr/lib/systemd/system/runlevel5.target.wants:
total 0
lrwxrwxrwx. 1 root root 39 Nov 23 15:36 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service
- unit相关的命令
- systemctl list-units #列出正在运行的unit
- systemctl list-units --all #列出所有,包括失败的或者inactive
- systemctl list-units --all --state=inactive #列出inactive的unit
- systemctl list-units --type=service #列出状态为active的service
- systemctl list is-active crond.service #查看某个服务是否为active
[root@localhost ~]# systemctl list-units
UNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Formats Fi
sys-devices-pci0000:00-0000:00:07.1-ata2-host2-target2:0:0-2:0:0:0-block-sr0.device loaded active
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda1.device loaded active
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda2.device loaded active
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda3.device loaded active
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda.device loaded active plug
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:1-0:0:1:0-block-sdb-sdb1.device loaded active
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:1-0:0:1:0-block-sdb-sdb2.device loaded active
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:1-0:0:1:0-block-sdb-sdb3.device loaded active
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:1-0:0:1:0-block-sdb.device loaded active plug
sys-devices-pci0000:00-0000:00:11.0-0000:02:01.0-net-eno16777736.device loaded active plugged P
sys-devices-pci0000:00-0000:00:11.0-0000:02:02.0-sound-card0.device loaded active plugged Audio
sys-devices-pci0000:00-0000:00:11.0-0000:02:05.0-net-ens37.device loaded active plugged PRO/100
sys-devices-platform-serial8250-tty-ttyS1.device loaded active plugged /sys/devices/platform/se
sys-devices-platform-serial8250-tty-ttyS2.device loaded active plugged /sys/devices/platform/se
sys-devices-platform-serial8250-tty-ttyS3.device loaded active plugged /sys/devices/platform/se
sys-devices-pnp0-00:08-tty-ttyS0.device loaded active plugged /sys/devices/pnp0/00:08/tty/ttyS0
lines 1-18
[root@localhost ~]# systemctl list-units --all
UNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Format
dev-block-8:17.device loaded active plugged LVM PV DEzGdZ-eZpX-qWYI-XndU-e5X
dev-block-8:18.device loaded active plugged LVM PV QI4y20-GOdg-8CBJ-PfZF-GO8
dev-block-8:19.device loaded active plugged LVM PV JeafFf-5aVO-xNf2-DUv6-17S
dev-cdrom.device loaded active plugged VMware_Virtual_IDE_CDROM_Drive C
dev-disk-by\x2did-ata\x2dVMware_Virtual_IDE_CDROM_Drive_10000000000000000001.device loaded act
dev-disk-by\x2did-dm\x2dname\x2dvg1\x2dlv1.device loaded active plugged /dev/disk/by-id/dm
dev-disk-by\x2did-dm\x2duuid\x2dLVM\x2dad3wQ3M3m1btaWae4bRhQl9iPMxBDFaSbxXagDlt0CiJLOnaqpvj6Vvsyq
dev-disk-by\x2did-lvm\x2dpv\x2duuid\x2dDEzGdZ\x2deZpX\x2dqWYI\x2dXndU\x2de5X9\x2dSEcT\x2dMsiLCc.d
dev-disk-by\x2did-lvm\x2dpv\x2duuid\x2dJeafFf\x2d5aVO\x2dxNf2\x2dDUv6\x2d17S2\x2dN8Cg\x2df0E3vX.d
dev-disk-by\x2did-lvm\x2dpv\x2duuid\x2dQI4y20\x2dGOdg\x2d8CBJ\x2dPfZF\x2dGO8l\x2dambD\x2df7Jikz.d
dev-disk-by\x2dlabel-CentOS\x5cx207\x5cx20x86_64.device loaded active plugged VMware_Virtu
dev-disk-by\x2dpath-pci\x2d0000:00:07.1\x2data\x2d2.0.device loaded active plugged VMware_
dev-disk-by\x2dpath-pci\x2d0000:00:10.0\x2dscsi\x2d0:0:0:0.device loaded active plugged VM
dev-disk-by\x2dpath-pci\x2d0000:00:10.0\x2dscsi\x2d0:0:0:0\x2dpart1.device loaded active plu
dev-disk-by\x2dpath-pci\x2d0000:00:10.0\x2dscsi\x2d0:0:0:0\x2dpart2.device loaded active plu
dev-disk-by\x2dpath-pci\x2d0000:00:10.0\x2dscsi\x2d0:0:0:0\x2dpart3.device loaded active plu
lines 1-18
[root@localhost ~]# systemctl list-units --all --state=inactive
UNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys-fs-binfmt_misc.mount loaded inactive dead Arbitrary Executable File Formats F
sys-fs-fuse-connections.mount loaded inactive dead FUSE Control File System
tmp.mount loaded inactive dead Temporary Directory
systemd-ask-password-console.path loaded inactive dead Dispatch Password Requests to Conso
auth-rpcgss-module.service loaded inactive dead Kernel Module supporting RPCSEC_GSS
brandbot.service loaded inactive dead Flexible Branding Service
cpupower.service loaded inactive dead Configure CPU power related setting
● 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 loaded inactive dead IPv6 firewall with ip6tables
iptables.service loaded inactive dead IPv4 firewall with iptables
irqbalance.service loaded inactive dead irqbalance daemon
● libvirtd.service not-found inactive dead libvirtd.service
lines 1-18
仅列出active和failed
[root@localhost ~]# systemctl list-units --type=service
UNIT LOAD ACTIVE SUB DESCRIPTION
auditd.service loaded active running Security Auditing Service
avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack
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
getty@tty1.service loaded active running Getty on tty1
gssproxy.service loaded active running GSSAPI Proxy Daemon
iprdump.service loaded active running LSB: Start the ipr dump daemon
iprinit.service loaded active running LSB: Start the ipr init daemon
iprupdate.service loaded active running LSB: Start the iprupdate utility
kdump.service loaded active exited Crash recovery kernel arming
kmod-static-nodes.service loaded active exited Create list of required static device
lvm2-lvmetad.service loaded active running LVM2 metadata daemon
lvm2-monitor.service loaded active exited Monitoring of LVM2 mirrors, snapshots
lvm2-pvscan@8:17.service loaded active exited LVM2 PV scan on device 8:17
lvm2-pvscan@8:18.service loaded active exited LVM2 PV scan on device 8:18
lvm2-pvscan@8:19.service loaded active exited LVM2 PV scan on device 8:19
lines 1-18
列出所有
[root@localhost ~]# systemctl list-units --all --type=service
UNIT LOAD ACTIVE SUB DESCRIPTION
auditd.service loaded active running Security Auditing Service
auth-rpcgss-module.service loaded inactive dead Kernel Module supporting RPCSEC_G
avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack
brandbot.service loaded inactive dead Flexible Branding Service
cpupower.service loaded inactive dead Configure CPU power related setti
crond.service loaded active running Command Scheduler
dbus.service loaded active running D-Bus System Message Bus
● 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
firewalld.service loaded active running firewalld - dynamic firewall daem
getty@tty1.service loaded active running Getty on tty1
gssproxy.service loaded active running GSSAPI Proxy Daemon
ip6tables.service loaded inactive dead IPv6 firewall with ip6tables
lines 1-18
[root@localhost ~]# systemctl is-active crond.service
active
[root@localhost ~]# systemctl is-enabled crond.service
disabled
target
- 系统为了方便管理用target来管理unit
- systemctl list-unit-files --type=target
- systemctl list-dependencies multi-user.target #查看指定target下面有哪些unit
- systemctl get-default #查看系统默认的target
- systemctl set-default multi-user.target
- 一个service属于一种类型的unit
- 多个unit组成了一个target
- 一个target里面包含了多个service
- cat /usr/lib/systemd/system/sshd.service #查看[install]部分
[root@localhost ~]# systemctl list-units --type=target
UNIT LOAD ACTIVE SUB DESCRIPTION
basic.target loaded active active Basic System
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.target loaded active active Network
nfs-client.target loaded active active NFS client services
nss-lookup.target loaded active active Host and Network Name Lookups
paths.target loaded active active Paths
remote-fs-pre.target loaded active active Remote File Systems (Pre)
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
lines 1-18
[root@localhost ~]# systemctl list-dependencies multi-user.target
multi-user.target
● ├─auditd.service
● ├─avahi-daemon.service
● ├─brandbot.path
● ├─dbus.service
● ├─iprdump.service
● ├─iprinit.service
● ├─iprupdate.service
● ├─irqbalance.service
● ├─kdump.service
● ├─network.service
● ├─NetworkManager.service
● ├─nfs-server.service
● ├─plymouth-quit-wait.service
● ├─plymouth-quit.service
● ├─postfix.service
● ├─rsyslog.service
● ├─sshd.service
lines 1-18
系统默认target
[root@localhost ~]# systemctl get-default
multi-user.target
设置默认target
[root@localhost ~]# 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@localhost ~]# ls -l /etc/systemd/system/default.target
lrwxrwxrwx 1 root root 41 Dec 5 09:07 /etc/systemd/system/default.target -> /usr/lib/systemd/system/multi-user.target
设置默认target的同时,会生成软链接
多个unit组成了一个target 一个target里面包含了多个service
查看[install]部分
[root@localhost ~]# cat /usr/lib/systemd/system/sshd.service
[Unit]
Description=OpenSSH server daemon
After=syslog.target network.target auditd.service
[Service]
EnvironmentFile=/etc/sysconfig/sshd
ExecStartPre=/usr/sbin/sshd-keygen
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
通过查看[install]部分我们可以得知属于哪个target
对比
[root@localhost ~]# cat /usr/lib/systemd/system/crond.service
[Unit]
Description=Command Scheduler
After=syslog.target auditd.service systemd-user-sessions.service time-sync.target
[Service]
EnvironmentFile=/etc/sysconfig/crond
ExecStart=/usr/sbin/crond -n $CRONDARGS
KillMode=process
[Install]
WantedBy=multi-user.target
修改于