The use of systemctl in CentOS7

I started to play CentOS 7 recently. I rubbed it and found that many commands have been changed. Hey, I feel sad.
Original address: https://blog.csdn.net/u012486840/article/details/53161574

Starting with CentOS 7.x, CentOS began to use systemd services to replace daemon. The original management system startup and management system services related commands are all replaced by systemctl commands.

1. Comparison between the original service command and the systemctl command

daemon command systemctl command illustrate
service [service] start systemctl start [unit type] start the service
service [service] stop systemctl stop [unit type] Out of service
service [service] restart systemctl restart [unit type] restart the service

In addition, the two systemctl parameters do not correspond to the service command parameters

  • status: parameter to view the running status of the service
  • reload: reload the service and load the updated configuration file (not all services support this parameter, such as network.service)

Application examples:

#启动网络服务
systemctl start network.service

#停止网络服务
systemctl stop network.service

#重启网络服务
systemctl restart network.service

#查看网络服务状态
systemctl status network.serivce

2. Comparison between the original chkconfig command and the systemctl command

2.1. Set startup / do not start

daemon command systemctl command illustrate
chkconfig [service] on systemctl enable [unit type] Set the service to start at startup
chkconfig [service] off systemctl disable [unit type] Device service is prohibited from booting

Application examples:

#停止cup电源管理服务
systemctl stop cups.service

#禁止cups服务开机启动
systemctl disable cups.service

#查看cups服务状态
systemctl status cups.service

#重新设置cups服务开机启动
systemctl enable cups.service

2.2 View all services on the system

Command format:

systemctl [command] [–type=TYPE] [–all]

Parameter details:

command - list-units: List all activated units by unit. Add –all to list the unstarted units; - list-unit-files: List the startup files according to the startup files in /usr/lib/systemd/system/

--type=TYPE - is the unit type, mainly service, socket, target

Application examples:

systemctl command illustrate
systemctl List all system services
systemctl list-units List all startup units
systemctl list-unit-files List all startup files
systemctl list-units –type=service –all List all units of service type
systemctl list-units –type=service –all grep cpu List the services of the cpu power management mechanism
systemctl list-units –type=target –all list all targets

3. Special usage of systemctl

systemctl command illustrate
systemctl is-active [unit type] Check if the service is running
systemctl is-enable [unit type] Check if the service is set to start on boot
systemctl mask [unit type] Log out of the specified service
systemctl unmask [unit type] Cancel the logout of the specified service

Application examples:

#查看网络服务是否启动
systemctl is-active network.service

#检查网络服务是否设置为开机启动
systemctl is-enable network.service

#停止cups服务
systemctl stop cups.service

#注销cups服务
systemctl mask cups.service

#查看cups服务状态
systemctl status cups.service

#取消注销cups服务
systemctl unmask cups.service

4. Comparison of init command and systemctl command

init command systemctl command illustrate
init 0 systemctl poweroff system shutdown
init 6 systemctl reboot Restart

Other commands related to power on and off:

systemctl command illustrate
systemctl suspend enter sleep mode
systemctl hibernate enter sleep mode
systemctl rescue Force into rescue mode
systemctl emergency Force into emergency rescue mode

5. Set the system run level

5.1. Correspondence table of run level

init level systemctl target
0 shutdown.target
1 emergency.target
2 rescure.target
3 multi-user.target
4 without
5 graphical.target
6 without

In addition, a getty.target is used to set the number of ttys.

5.2, set the run level

Command format:

Detailed explanation of systemctl [command] [unit.target]
parameters:

command:
get-default : get the current target
set-default : set the specified target as the default run level
isolate : switch to the specified run level
unit.target : be the run level listed in table 5.1

systemctl command Command description
systemctl get-default Get the current runlevel
systemctl set-default multi-user.target Set the default runlevel to mulit-user
systemctl isolate multi-user.target Switch to runlevel mulit-user without restarting
systemctl isolate graphical.target Switch to the graphical interface without restarting

6. Use systemctl to analyze the dependencies before each service

Command format:

systemctl list-dependencies [unit] [–reverse]
–reverse is used to check which unit uses this unit

Application examples:

#获得当前运行级别的target
[root@www ~]# systemctl get-default
multi-user.target

#查看当前运行级别target(mult-user)启动了哪些服务
[root@www ~]# systemctl list-dependencies
default.target
├─abrt-ccpp.service
├─abrt-oops.service
├─vsftpd.service
├─basic.target
│ ├─alsa-restore.service
│ ├─alsa-state.service
.....(中间省略).....
│ ├─sockets.target
│ │ ├─avahi-daemon.socket
│ │ ├─dbus.socket
.....(中间省略).....
│ ├─sysinit.target
│ │ ├─dev-hugepages.mount
│ │ ├─dev-mqueue.mount
.....(中间省略).....
│ └─timers.target
│   └─systemd-tmpfiles-clean.timer
├─getty.target
│ └─getty@tty1.service
└─remote-fs.target

#查看哪些target引用了当前运行级别的target
[root@www ~]# systemctl list-dependencies --reverse
default.target
└─graphical.target

7. Turn off network services

There are some special needs to close unit.servce and unit.socket at the same time when using systemctl to close the network service

Use systemctl to view the enabled sshd service

[root@www system]#  systemctl list-units --all | grep sshd
sshd-keygen.service loaded inactive dead        OpenSSH Server Key Generation
sshd.service        loaded active   running     OpenSSH server daemon
sshd.socket         loaded inactive dead        OpenSSH Server Socket

可以看到系统同时开启了 sshd.service 和 sshd.socket , 如果只闭关了 sshd.service 那么 sshd.socket还在监听网络,在网络上有要求连接 sshd 时就会启动 sshd.service 。因此如果想完全关闭sshd服务的话,需要同时停用 sshd.service 和 sshd.socket 。

systemctl stop sshd.service
systemctl stop sshd.socket
systemctl disable sshd.service sshd.socket

由于centos 7.x默认没有安装net-tools,因此无法使用netstat 来查看主机开发的商品。需要通过yum安装来获得该工具包:

yum -y install net-tools

查看是否关闭22端口

netstat -lnp |grep sshd

8、关闭防火墙firewall

Centos 7.x 中取消了iptables, 用firewall取而代之。要关闭防火墙并禁止开机启动服务使用下面的命令:

systemctl stop firewalld.service
systemctl disable firewalld.service

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324924086&siteId=291194637