【Linux】服务管理和进程监控

目录

服务(service)管理

服务运行级别(runlevel)

systemctl get-default 查看当前运行级别

systemctl set-default +级别 设置运行级别

 chkconfig设置各个服务自启动/关闭

systemctl管理指令

防火墙打开或者关闭指定端口

 动态监控进程

 netstat查看系统网络情况


服务(service)管理

介绍:

服务(service)本质就是进程,但是是运行在后台,通常都会监听某个端口,等待其他程序的请求,比如(mysqld,sshd ,防火墙等),因此我们又称为守护进程,是Linux中非常重要的知识点。

 找那个端口,本质就会找那个端口监听的系统服务或者说是守护进程

service 管理指令

1、service 服务名 [start |stop| restart|reload |status]

2、在CenOS7.0后很多服务不在使用service,而是systemctl

3、service指令管理的服务在 /etc/init.d 查看

oot@kongchao02 ~]# ll /etc/init.d
lrwxrwxrwx. 1 root root 11 2月  28 19:44 /etc/init.d -> rc.d/init.d
[root@kongchao02 ~]# 

查看网络状态

[root@kongchao02 ~]# service network status
已配置设备:
lo ens33
当前活跃设备:
lo ens33 virbr0
[root@kongchao02 ~]# 

服务运行级别(runlevel)

Linux系统有7种运行级别(runleve)):常用的是级别3和5

运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动

运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆

运行级别2:多用户状态(没有NFS),不支持网络

运行级别3:完全的多用户状态(有NFS),无界面,登陆后进入控制台命令行模式

运行级别4:系统未使用,保留

运行级别5:X11控制台,登陆后进入图形GUI模式

运行级别6:系统正常关闭并重启,默认运行级别不能设为6,否则不能正常启动

 开机的流程说明:

 CentOS 7后运行级别说明

在 /etc/initab进行了简化

如:multi-user.target : analogous to run level 3命令行

 graphical.target:analogous to run level 5图形化

systemctl get-default 查看当前运行级别

[root@kongchao02 ~]# systemctl get-default
graphical.target
[root@kongchao02 ~]# 

 当前运行级别为图形界面

systemctl set-default +级别 设置运行级别

如设置文本界面:systemctl set -default  multi-user.target

设置完成后重启生效

[root@kongchao02 ~]# 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@kongchao02 ~]# systemctl get-default
multi-user.target
[root@kongchao02 ~]# 

重启后为: 

 chkconfig设置各个服务自启动/关闭

介绍

1、通过chkconfig命令可以给服务的各个运行级别设置自启动/关闭

2、chkconfig指令管理的服务在/etc/init.d查看

3、注意:Centos7.0后,很多服务使用systemctl管理

chkconfig基本语法

查看服务 chkconfig  --list  [| grep xxx]  

chkconfig  服务名  --list 

chkconfig  --level 5 服务名on/off

案例演示:对network服务进行各种操作

使用细节
chkconfig重新设置服务后自启动或关闭,需要重启机器reboot生效

 如上图:network在0、1、6级别是关闭的,其余都是开启的

也可使用过滤

 示例:对network服务进行各种操作,把network在3运行级别关闭自启动

关闭:chkconfig  --level  3  network  off

打开:chkconfig  --level  3  network  on

systemctl管理指令

systemctl管理指令

1,基本语法:systemctl  [start|stop|restart|status] 服务名

2.systemctl指令管理的服务在/usr/lib/systemd/system查看

systemctl设置服务的自启动状态

1.systemctl list-unit-files [|grep 服务名](查看服务开机启动状态,grep可以进行过滤)

2.systemctl enable 服务名 (设置服务开机启动)

3.systemctl disable 服务名 (关闭服务开机启动)

4.systemctl is-enabled  服务名 (查询某个服务是否是自启动的)

应用案例:
查看当前防火墙的状况,关闭防火墙和重启防火墙。

可以模糊查询

查看防火墙:systemctl status firewalld

关闭防火墙:systemctl stop  firewalld

重启防火墙:systemctl start  firewalld

细节讨论:

1.关闭或者前言防火墙后,立即生效。[telnet 测试  某个端口即可]

2.这种方式只是临时生效,当重启系统后,还是回归以前对服务的设置。

3.如果希望设置某个服务自启动或关闭永久生效,要使用 systemctl [enable|disable] 服务名,如firewalld防火墙

防火墙打开或者关闭指定端口

在真正的生产环境,往往需要将防火墙打开,但问题来了,如果我们把防火墙打开,那么外部请求数据包就不能跟服务器监听端口通讯。这时,需要打开指定的端口。比如80、22、8080等,

firewall 指令

查看协议:netstat -anp 

打开端口:firewall-cmd --permanent --add-port=端口号/协议

关闭端口:firewall-cmd --permanent --remove-port=端口号/协议

重新载入,才能生效:firewall-cmd--reload

查询端口是否开放:firewall-cmd --query-port=端口/协议

应用案例:

1、启用防火墙,测试111端口是否能telnet

2、开放111端口

3、再次关闭111端口

[root@kongchao03 ~]# firewall-cmd --permanent --add-port=111/tcp
success
[root@kongchao03 ~]# firewall-cmd --query-port=111/tcp
no
[root@kongchao03 ~]# firewall-cmd --reload
success
[root@kongchao03 ~]# firewall-cmd --query-port=111/tcp
yes
[root@kongchao03 ~]# firewall-cmd --permanent --remove-port=111/tcp
success
[root@kongchao03 ~]# firewall-cmd --query-port=111/tcp
yes
[root@kongchao03 ~]# firewall-cmd --reload
success
[root@kongchao03 ~]# firewall-cmd --query-port=111/tcp
no
[root@kongchao03 ~]# 

:打开或关闭后一定要使用firewall-cmd--reload才能生效

 动态监控进程

介绍:

top与ps命令很相似,他们都用来显示正在执行的进程,Top与ps最大的不同之处,在于top在执行一段数据可以更新正在运行的进程。

基本语法:top   [选项]

选项说明:

选项 功能
-d 秒数 指定top命令每隔几秒更新,默认是三秒
-i 是top不显示任何闲置或者僵死进程
-p 通过指定监控进程ID来仅仅监控某个进程的状态

 参数说明:

 交互操作说明:

操作 功能
P 以CPU使用率排序,默认就是此项
M 以内存的使用率排序
N 以PID排序
q 退出top

这些交互操作都是在输入完top后输入的。

示例1:监视特定用户

先输入top命令,按下回车键,查看执行的进程,后输入u回车,再输入用户名即可

示例2:终止指定的进程

先输入top命令,按下回车键,查看执行的进程,后输入k回车,再输入要结束的进程ID号

 netstat查看系统网络情况

基本语法:netstat   [选项]

选项说明

-an 按一定排序输出

-p  显示哪个进程在调用

应用示例

请查看服务名为sshd的服务信息。

[root@kongchao02 ~]# netstat -anp |grep sshd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1062/sshd           
tcp6       0      0 :::22                   :::*                    LISTEN      1062/sshd           
unix  3      [ ]         STREAM     CONNECTED     24143    1062/sshd            
[root@kongchao02 ~]# 

猜你喜欢

转载自blog.csdn.net/weixin_60719453/article/details/124623159