[Make it clear, to say it! ] Service Management

Contents:
(a) understand the concept of service
(b) to view server status, startup, shutdown, reboot
(c) set the service at startup
(d) how the service is defined
(five) to run level: Learn suffix target service


(A) understand the concept of services
(1.1) First we look at Windows system services, such as DHCP Client service system, in which the "state" refers to the current state, and the current state we can select "start, stop, pause, resume, restart "; even if we set the current status of" stop ", but after a system restart is still will be" turned on "state, which is due to the" startup type ", we set a "automatic" startup type, and therefore will not affect the current startup type state of the system, it will only affect the state of the system when the next reboot the system.
[Make it clear, to say it!  ] Service Management


(二)服务器状态查看、启动、关闭、重启
(2.1)而在Linux系统中,我们可以使用systemctl命令查询系统中所有的服务信息(图1-2),在Linux系统中类似于Windows系统中的服务都是以后缀为service的服务(图1-3)。
# systemctl list-unit-files---查询系统中所有的服务
# systemctl list-unit-files --type service---查询所有类型为service的服务
[Make it clear, to say it!  ] Service Management
[Make it clear, to say it!  ] Service Management
(2.2)我们首先查询系统中是否有ftp、sshd服务,可以使用list-unit-files参数确定服务的具体名称。然后可以查询sshd服务是否是正常运行的状态,接着可以对服务进行一系列停止、启动、重启的操作,但是当前的操作都只是对系统的当前状态进行的,如果系统重启后,当前的设置都会默认到系统原始的状态。
# systemctl list-unit-files --type service |grep ftp---查询系统中是否有ftp服务
# systemctl list-unit-files --type service | grep ssh---查询系统中是否有ssh服务
# systemctl is-active sshd---查询sshd服务是否正常运行
# systemctl stop sshd.service---关闭sshd服务运行
# systemctl start sshd---将sshd服务启动起来
# systemctl restart sshd.service---将sshd服务重启一下
[Make it clear, to say it!  ] Service Management


(三)设置服务开机自动启动
(3.1)如果我们现在希望系统的sshd服务在开机后能够自动启动,我们可以使用如下的方式进行设置。
# systemctl is-enabled sshd.service---查询sshd服务是否设置了开机自动启动
# systemctl disable sshd.service---设置sshd服务开机不会自动启动
# systemctl enable sshd.service---设置sshd服务开机能够自动启动
[Make it clear, to say it!  ] Service Management
(3.2)如果我们想将一个服务设置为“禁用”的模式,则可以使用mask命令设置服务是否为禁用的状态。
# systemctl mask sshd---将sshd服务设置为“禁用”模式
# systemctl is-active sshd.service---查询当前sshd服务的状态
# systemctl stop sshd---将sshd服务停止
# systemctl start sshd---此时发现无法正常的启动sshd服务了
# systemctl unmask sshd.service---解除sshd服务的“禁用”模式
[Make it clear, to say it!  ] Service Management
(3.3)如果我们想查看服务的状态时,可以使用status参数查看服务的状态信息。
# systemctl status sshd---查询系统中sshd服务的状态
# systemctl status sshd -l---查询系统中sshd服务的状态,以及更详细的信息
[Make it clear, to say it!  ] Service Management


(四)服务是如何定义的
(4.1)现在我们希望自己能够定义一个服务memload,我们首先将memload-7.0-1.r29766.x86_64.rpm软件下载到vms002主机上(图4-1),接着我们将memload软件包安装起来,此时我们发现在/usr/local/bin/目录下存在一个memload文件(图4-2)。
[Make it clear, to say it!  ] Service Management
[Make it clear, to say it!  ] Service Management
(4.2)接着我们在vms002主机上创建一个memload.service的文件,同时在文件中指明,如果开启服务,会指定消耗的内存空间,如果停止服务的时候,会执行杀死memload进程的操作(图4-3)。然后我们将memload.service配置文件考本到/usr/lib/systemd/system/目录中,此时我们在系统中便会发现已经有memload.service服务生成了(图4-4)。此时我们将memload服务运行起来后发现自定义的服务启动后,系统的used部分的内存使用量确实增加了1G左右(图4-5)。自定义服务,定制服务
# systemctl start memload---将自定义的服务memload启动
# ps aux | grep -v grep | grep memload---我们查询系统是否有memload服务在运行
[Make it clear, to say it!  ] Service Management
[Make it clear, to say it!  ] Service Management
[Make it clear, to say it!  ] Service Management
(4.3)然后我们将memload服务停止后,发现此时系统的used使用空间又恢复到了最开始没有没消耗1G内存的状态了。
# systemctl stop memload.service---将memload服务停止运行
# ps aux | grep -v grep | grep memload---我们查询系统是否有memload服务在运行
# free -m---查询系统的内存空间使用情况
注意:经验总结# ps axj | more---可以查询后台所有守护进程的情况daemon,后台守护进程,后台进程
[Make it clear, to say it!  ] Service Management
(4.4)当我们启动一个服务的时候,则在后台会启动一个命令,这个命令我们称之为守护进程daemon,服务就是一个文件来管理daemon。如果有时候我们想要查看某个daemon的配置文件的位置,可以使用status参数(图4-7),此时我们便可以看到sshd.service配置文件中关于ExecStart和ExecReload的配置信息(图4-8)。
# systemctl status sshd---查询sshd服务的状态
[Make it clear, to say it!  ] Service Management
[Make it clear, to say it!  ] Service Management
(4.5)如果我们需要查询一个服务配置文件的路径时,可以使用which命令即可,便可以查询到服务执行的命令的可执行文件的位置了。
# which sshd---查询出sshd服务具体执行的命令的路径
# cat /usr/lib/systemd/system/sshd.service
[Make it clear, to say it!  ] Service Management


(五)运行级别:了解后缀为target的服务
(5.1)一般我们在系统中使用tab键时可以进行命令的快速索引的,而我们如果使用最小化安装系统的时候会发现无法使用快速索引的功能,此时我们应该安装bash-completion.noarch这个软件包,然后将系统重启一下,此时我们便又可以使用tab命令快速索引的功能了。自动补全,自动补齐
# yum install bash-completion.noarch -y
[Make it clear, to say it!  ] Service Management
(5.2)在我们系统中有很多类型的服务,例如有后缀为service的服务,也有后缀为socket的服务,还有后缀为mount的服务,我们现在来关注的是后缀为target的服务。
# systemctl list-unit-files --type target---查询后缀为target的服务
[Make it clear, to say it!  ] Service Management
(5.3)所谓target的服务,实际上类似于RHEL 6所说的运行级别的概念。我们在开机后经过了BIOS加电自检-->GRUB引导程序-->内核初始化,此时会读取/etc/fstab文件中的内容,挂载各个设备,同时启动各个服务。此时在我们的系统中是存在多个选择的,例如我们有0-6一共7个服务可供选择,假设A、B、C、D代表的是系统普通服务,X代表的是图形化界面服务。例如我们现在想要运行rc5.d服务,此时系统启动起来后,我们系统中就会有ABDX服务在运行了。如果我们现在已经在rc5.d的级别运行了,此时我们希望能够切换到rc3.d的级别运行,那么系统需要做的是将当前运行的ADX服务关闭,然后将没有运行的C服务开启,此时我们系统就进入了rc3.d的运行级别了。一般系统默认的运行级别是rc5.d,它是在/etc/inittab文件的最后一行定义的。
[Make it clear, to say it!  ] Service Management
(5.4)在RHEL 6系统中,对于运行级别我们一般做如下的定义,其中rc0.d表示的是“关机”;rc1.d表示的是“单用户模式”或“救援模式”;rc2.d表示的是“命令行”模式,但是不能使用nfs;rc3.d表示的是“全模式”即所有功能都可以使用,但是只在字符界面操作;rc4.d属于未分配的模式;rc5.d表示是“全模式”,同时是图形化的操作界面;rc6.d表示的是“重启”。我们使用runlevel命令就可以查看当前系统是属于哪个运行级别,如果我们想关机可以执行init 0,如果我们想重启系统则可以执行init 6
# runlevel---查看系统的运行级别
# init 6---重启系统
[Make it clear, to say it!  ] Service Management
(5.5)在RHEL 7系统中我们所看到后缀为target的服务其实就是运行级别。其中RHEL 7系统的各个运行级别和RHEL 6系统运行级别的对比如下图所示。默认情况下我们的系统启动后的运行级别为“runlevel5.target”或“graphical.target”即图形化界面的模式,我们可以通过get-default命令查看系统的默认级别。
# systemctl get-default---查询系统的默认级别
[Make it clear, to say it!  ] Service Management
[Make it clear, to say it!  ] Service Management
(5.6)如果我们希望将当前的运行级别临时切换为多用户字符命令模式,则可以使用isolate命令(图5-7),临时切换系统的运行级别为字符界面多用户模式,此时我们发现此时系统当前的运行级别已经是3级别了(图5-8)。
# systemctl isolate multi-user.target---临时切换系统的运行级别为字符界面多用户模式
[Make it clear, to say it!  ] Service Management
[Make it clear, to say it!  ] Service Management
(5.7)现在我们在字符界面输入startx命令后便进入了图形化的界面了,此时我们发现虽然是图形化界面,但是系统的运行级别仍然是3即多用户字符界面的模式。由于我们在(5.6)的操作步骤中已经将当前系统的运行级别临时设置为3了,所以此时3的运行级别下默认是不启用X服务的,但是我们是可以单独额外的启动X服务。如果我们想要切换到图形化界面运行,则可以使用isolate命令临时设置当前的状态为graphical.target模式。
[Make it clear, to say it!  ] Service Management
[Make it clear, to say it!  ] Service Management
(5.8)注意在RHEL 6系统中如果我们是属于运行级别5图形化界面,然后我们使用Ctrl+alt+F2进入到字符界面后,此时我们在字符界面执行startx命令,是无法正常的执行成功的,执行后产生了冲突,系统会认为我现在已经是在运行级别5的,而且已经能执行一个startx的服务,不能在执行第二个同样的服务的。
(5.9)我们在vms002主机的系统中默认的运行级别是graphical.target,如果我们希望更改系统默认的运行级别,则应该使用set-default命令,此时我们便可以使得系统默认的运行级别为multi-user.target了(图5-11)。此时的设置并不会影响系统当前的状态,我们发现系统的当前状态仍然为graphical.target图形化界面,而重启系统主要可以通过图5-12的四种方法进行。
# systemctl get-default---查询系统当前的默认运行级别
# systemctl set-default multi-user.target---修改系统默认的运行级别为multi-user.target
# reboot---重启系统方法1
# init 6---重启系统方法2
# Systemctl isolate reboot.target --- reboot the system method 3
# systemctl isolate runlevel6.target --- 4 methods to restart the system
[Make it clear, to say it!  ] Service Management
[Make it clear, to say it!  ] Service Management
after (5.10) to restart the system, then we default into the character of the command line interface, and then we use isolate command set login graphical interface graphical.target (Figure 5-13), then we find that the default system run level is still set for multi-user.target, so even if the system reboot again and still is a character into the command line interface (Fig. 5-14).
# Systemctl isolate graphical.target --- temporarily switch to log on to the graphical interface
[Make it clear, to say it!  ] Service Management
[Make it clear, to say it!  ] Service Management
(5.11) we hope to see a graphical interface where the service is defined, we can use systemctl status command to query (Figure 5-15), then we can find graphical.target services in / usr / lib / systemd / system / directory configuration files and view details (Figure 5-16 and 5-17) profile.
# Systemctl status vsftpd --- query the status of the service vsftpd
# cd / usr / lib / systemd / System /
# vim graphical.target
[Make it clear, to say it!  ] Service Management
[Make it clear, to say it!  ] Service Management
[Make it clear, to say it!  ] Service Management

------ This concludes the article, thanks for reading ------

Guess you like

Origin blog.51cto.com/13613726/2461257