当前位置:网站首页>The use of systemctl in centos7

The use of systemctl in centos7

2020-11-10 15:20:33 xxjbs001

CentOS 7.x Start ,CentOS Start using systemd Service instead of daemon, The original management system startup and management system services related commands are all by systemctl Order in place of .

1、 The original service Command and systemctl Command comparison

daemon command systemctl command explain
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

Besides, there are two systemctl The parameter does not match service Command parameters correspond to

  • status: Parameter to view the running status of the service
  • reload: Reload service , Load the updated configuration file ( Not all services support this parameter , such as network.service)

Application, for example, :

# Start network service 
systemctl start network.service

# Stop network services 
systemctl stop network.service

# Restart network service 
systemctl restart network.service

# Check network service status 
systemctl status network.serivce

2、 The original chkconfig Command and systemctl Command comparison

2.1、 Set boot up / Do not start

daemon command systemctl command explain
chkconfig [ service ] on systemctl enable [unit type] Set up service startup
chkconfig [ service ] off systemctl disable [unit type] Device services prohibit boot up

Application, for example, :

# stop it cup Power management services 
systemctl stop cups.service

# prohibit cups Service startup 
systemctl disable cups.service

# see cups Service status 
systemctl status cups.service

# To reset cups Service startup 
systemctl enable cups.service

2.2、 View all the services on the system

Command format :

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

Parameters, :

command - list-units: basis unit List all activated unit. add –all Will list the ones that didn't start unit; - list-unit-files: basis /usr/lib/systemd/system/ Start up file in , List the startup files

–type=TYPE - by unit type, There are mainly service, socket, target

Application, for example, :

systemctl command explain
systemctl List all system services
systemctl list-units List all starts unit
systemctl list-unit-files List all startup files
systemctl list-units –type=service –all List all service Type of unit
systemctl list-units –type=service –all grep cpu List cpu Power management mechanism Services
systemctl list-units –type=target –all List all target

3、systemctl Special usage

systemctl command explain
systemctl is-active [unit type] See if the service is running
systemctl is-enable [unit type] Check if the service is set to boot
systemctl mask [unit type] Log off the specified service
systemctl unmask [unit type] Cancel logoff of specified service

Application, for example, :

# Check whether the network service is started 
systemctl is-active network.service

# Check if the network service is set to boot 
systemctl is-enable network.service

# stop it cups service 
systemctl stop cups.service

# Cancellation cups service 
systemctl mask cups.service

# see cups Service status 
systemctl status cups.service

# Cancel cancellation cups service 
systemctl unmask cups.service

4、init Command and systemctl Command comparison

init command systemctl command explain
init 0 systemctl poweroff System shutdown
init 6 systemctl reboot Restart

Other commands related to switching on / off :

systemctl command explain
systemctl suspend Go to sleep mode
systemctl hibernate Enter sleep mode
systemctl rescue Force into rescue mode
systemctl emergency Force into emergency mode

5、 Set system operation level

5.1、 Run level corresponding table

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

Besides, it's also a getty.target Used to set tty The number of .

5.2、 Set run level

Command format :

systemctl [command] [unit.target]

Parameters, :

command:
  • get-default : Get current target
  • set-default : Set specified target Is the default run level
  • isolate : Switch to the specified run level
  • unit.target : by 5.1 The runlevels listed in the table
systemctl command explain
systemctl get-default Get the current run level
systemctl set-default multi-user.target Set the default run level to mulit-user
systemctl isolate multi-user.target Without rebooting , Switch to run level mulit-user Next
systemctl isolate graphical.target Without rebooting , Switch to the graphical interface

6、 Use systemctl Analyze the dependencies before each service

Command format :

systemctl list-dependencies [unit] [–reverse]

–reverse It's used to check which unit Used this unit

Application, for example, :

# Get the current run level of target
[root@www ~]# systemctl get-default
multi-user.target

# View current run level target(mult-user) What services have been launched 
[root@www ~]# systemctl list-dependencies
default.target
├─abrt-ccpp.service
├─abrt-oops.service
├─vsftpd.service
├─basic.target
│ ├─alsa-restore.service
│ ├─alsa-state.service
.....( Middle ellipsis ).....
│ ├─sockets.target
│ │ ├─avahi-daemon.socket
│ │ ├─dbus.socket
.....( Middle ellipsis ).....
│ ├─sysinit.target
│ │ ├─dev-hugepages.mount
│ │ ├─dev-mqueue.mount
.....( Middle ellipsis ).....
│ └─timers.target
│   └─systemd-tmpfiles-clean.timer
├─getty.target
│ └─getty@tty1.service
└─remote-fs.target

# See what target Reference to the current run level target
[root@www ~]# systemctl list-dependencies --reverse
default.target
└─graphical.target

7、 Shut down network services

In the use of systemctl There's something special about shutting down network services It needs to be shut down at the same time unit.servce and unit.socket

Use systemctl Check the open 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

You can see that the system is turned on at the same time  sshd.service  and  sshd.socket , If only closed  sshd.service  that  sshd.socket Still monitoring the Internet , There is a request to connect on the network sshd It will start when  sshd.service . So if you want to shut down completely sshd service , It needs to be stopped at the same time  sshd.service  and  sshd.socket .

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

because centos 7.x Not installed by default net-tools, So I can't use netstat To see the products developed by the host . Need to pass through yum Install to get the kit :

yum -y install net-tools

See if it's off 22 port

netstat -lnp |grep sshd

8、 Turn off firewall firewall

Centos 7.x It's cancelled iptables, use firewall Instead, . To turn off the firewall and disable boot service, use the following command :

systemctl stop firewalld.service
systemctl disable firewalld.service

版权声明
本文为[xxjbs001]所创,转载请带上原文链接,感谢