Linux学习(第三篇) 基础篇完结

Linux进程管理

  1. 在Linux中,每一个执行程序都称为一个进程,每一个进程都分配一个ID号(pid,进程号)
  2. 每个进程都可能以两种方式存在的,前台和后台,所谓前台就是用户目前的屏幕上可以进行操作到的,后台进程则是实际在操作,但是由于屏幕上无法看到的进程,通常使用后台方式执行
  3. 一般系统的服务都是以后台进程的方式存在,而且都会常驻在系统中,直到关机才结束
ps指令详解

ps 指令是用来查看目前系统中,有哪些正在执行,以及他们的执行情况的,可以不添加任何参数的。

ps-aux|grep xxx  #查看进程指令
# 指令说明
System V 展示风格
USER :用户名称
PID 进程号
%CPU:进程占用CPU的百分比
%MEM:进程占用物理内存的百分比
VSZ:进程占用的虚拟内存大小
RSS :进程占用的物理内存大小
TT:终端名称,缩写
STAT:进程状态,其中S-睡眠,S-表示该进程是会话的先导进程,N-表示进程拥有比普通优先级
更低的优先级,R-正在运行,D-短期等待,Z-僵死进程,T-被跟踪或者被停止等等
STARTED :进程的启动时间
TIME :CPU时间,即进程使用CPU的总时间
COMMENTAND:启动进程所用的命令和参数,如果时间过长会被截断显示
[root@VM-12-5-centos ~]# ps -aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1  52872  2964 ?        Ss   May29   5:00 /usr/lib/systemd/systemd --system --deserialize 17
root         2  0.0  0.0      0     0 ?        S    May29   0:01 [kthreadd]
root         4  0.0  0.0      0     0 ?        S<   May29   0:00 [kworker/0:0H]
root         6  0.0  0.0      0     0 ?        S    May29   2:55 [ksoftirqd/0]

显示系统执行的进程

要求:以全格式显示当前所有的进程,查看进程的父进程
ps -ef #是以全格式显示当前所有的进程
-e 显示所有进程 -f 全格式
ps -ef|grep XXX
是BSD 风格
UID 用户ID
PID 进程ID
PPID 父进程ID
C: CPU用于计算机执行优先级的因子,数值越大,表明进程是CPU密集型运算,执行优先级会降低
数值越小,表明进程是IO密集型运算,执行优先级会提高
STIME 进程启动的时间
TTY 完整的终端名称
TIME CPU时间
CMD 启动进程所用的命令和参数
[root@VM-12-5-centos ~]# ps -ef |  more
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 May29 ?        00:05:02 /usr/lib/systemd/systemd --system --deserialize 17
root         2     0  0 May29 ?        00:00:01 [kthreadd]
root         4     2  0 May29 ?        00:00:00 [kworker/0:0H]

终止进程

若是某个进程执行一半需要停止时,或是已消耗了很大的系统资源时,此时可以考虑停止该进程,使用kill命令来完成此项任务

kill [选项] 进程号 (通过进程号杀死/终止进程)
killall 进程名称 (通过进程名杀死进程,也支持通配符这在系统因负载过大而变得很慢时有用)
-9 :表示强迫进程立即停止

踢掉某个登录中的用户

#root控制端
[root@VM-12-5-centos ~]# ps -aux|grep sshd
root      1434  0.0  0.1 112984  2476 ?        Ss   May29   0:18 /usr/sbin/sshd -D
root      3700  0.0  0.2 156860  5460 ?        Ss   19:45   0:00 sshd: root@pts/1
root      8576  0.0  0.2 156860  5380 ?        Ss   20:18   0:00 sshd: fox [priv]
fox       8593  0.0  0.1 156860  2344 ?        S    20:18   0:00 sshd: fox@pts/2
root      8673  0.0  0.0 112816   968 pts/1    S+   20:18   0:00 grep --color=auto sshd
[root@VM-12-5-centos ~]# kill 8593

#在fox的控制端
Connection closed by foreign host.
#连接被外部主机关掉
Disconnected from remote host(43.142.126.141) at 20:19:01.

终止远程登录服务sshd,在适当的时候再次重启sshd服务

root      1434  0.0  0.1 112984  2476 ?        Ss   May29   0:18 /usr/sbin/sshd -D
kill sshd 对应的进程号 
/bin/systemctl start.service

终止多个gedit

 killall gedit

强制杀掉一个终端

kill -9 bash 对应的进程号吗
pstree指令
pstree [选项]  可以更加直观的来看进程的信息
-p :显示进程的PID
-u 显示进程的所有用户

在这里插入图片描述

服务管理

介绍:
服务(service)本质就是进程,但是运行在后台的,通常都会监听某个端口,等待其他进程的请求,比如mysql,sshd,防火墙等,因此我们又称之为守护进程

service 服务名 [start |stop |restart|reload|status]
在CentOS7.0后很多服务不再使用service而是systemctl 
service 指令管理的服务在/etc/init.d查看

**加粗样式**

[root@VM-12-5-centos ~]# ls -l /etc/init.d/
total 168
-rwxr-xr-x 1 root root 10438 May 29 15:43 bt
-rwxr-xr-x 1 root root   370 May 29 15:55 bttomcat7
-rw-r--r-- 1 root root 18281 May 22  2020 functions
-rwxr-xr-x 1 root root 88250 Aug 23  2021 mst
-rwxr-xr-x 1 root root 10602 May 29 15:51 mysqld
-rwxr-xr-x 1 root root  4569 May 22  2020 netconsole
-rwxr-xr-x 1 root root  7928 May 22  2020 network
-rwxrwxr-x 1 root root  2753 Dec 21  2021 nginx
-rwxr-xr-x 1 root root  2405 Sep 25  2021 php-fpm-73
-rwxr-xr-x 1 root root  1447 Aug 17  2016 pure-ftpd
-rw-r--r-- 1 root root  1160 Feb  3  2021 README
[root@VM-12-5-centos ~]# service mysqld status  #查看服务状态
 SUCCESS! MySQL running (22606)

查看服务名:
方式一:setup - >系统服务 就可以看到全部了
在这里插入图片描述

空格键控制是否选中 * 代表启动服务 tab键可以控制退出

方式二:/etc/init.d 看到service指令管理的服务

服务的运行级别 runlevel

在这里插入图片描述
在这里插入图片描述

chkconfig指令

  1. 通过chkconfig命令可以给服务的各个运行级别设置自 启动/关闭
  2. chkconfig 指令管理的服务在 /etc/init.d查看
  3. 注意:Centos7.0后,很多服务使用systemctl管理
查看服务 chkconfig --list [|grep xxx]
chkconfig 服务名 --list
chkconfig --level 5 服务名 on/off
[root@VM-12-5-centos ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.
bt             	0:off	1:off	2:on	3:on	4:on	5:on	6:off
mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
nginx          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
php-fpm-73     	0:off	1:off	2:on	3:on	4:on	5:on	6:off
pure-ftpd      	0:off	1:off	2:on	3:on	4:on	5:on	6:off
此输出仅显示 SysV 服务,不包括本机系统化服务。SysV 配置数据可能被本机覆盖 系统配置。
如果要列出 systemd 服务,请使用 'systemctl list-unit-files'。
查看在特定目标使用上启用的服务 'systemctl list-dependencies [target]'

在这里插入图片描述

将network在3运行级别关闭自启动
chkconfig --level 3 network off
chkconfig --level 3 network on

注意:–>:chkconfig 重新设置服务后自启动或关闭需要重启机器reboot生效

systemctl管理指令:

  1. 基本语法:systemctl 【start|stop|restart|status】服务名
  2. systemctl 指令管理的服务在 /usr/lib/systemd/system 查看

在这里插入图片描述

systemctl 设置服务的自启动状态:

  1. systemctl list-unit-files [ | grep 服务名] (查看服务开机启动状态,grep可以进行过滤)
[root@VM-12-5-centos ~]# ls -l /usr/lib/systemd/system |grep fire
-rw-r--r--  1 root root  657 Apr 28  2021 firewalld.service
[root@VM-12-5-centos ~]# systemctl list-unit-files | grep fire
firewalld.service                             enabled #防火墙处于自启动状态
  1. systemctl enable 服务名 (设置服务开机启动)
  2. systemctl disable 服务名 (关闭服务开机启动)
  3. systemctl is-enabled 服务名 (查询某个服务是否是自启动的)

查看防火墙的状况,关闭防火墙和重启防火墙

systemctl status firewalld  #查看服务状态 

[root@VM-12-5-centos ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2022-05-29 00:36:46 CST; 1 months 18 days ago
     Docs: man:firewalld(1)
 Main PID: 4791 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─4791 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
           
systemctl stop firewalld #关闭服务

在这里插入图片描述

systemctl stop firewalld #关闭防火服务
systemctl is-enabled firewalld 
enable #下次重启还是会启动的
systemctl disable firewalld #这样才是关闭服务开机启动

注意事项

  1. 关闭或者启用防火墙后,立即生效 [telnet 测试,某个端口即可]
  2. 这种方式只是临时生效的,当重新启动系统后,还是要回归以前对服务的设置
  3. 如果希望设置某个服务自启动或永久生效,要使用 systemctl [enable |disable ]服务名

端口服务管理

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

firewall指令

firewall-cmd --permanent --add-port=端口号/协议  #打开端口
firewall-cmd --permanent --remove-port=端口号/协议
重新载入,才生效 :firewall-cmd --reload
查询端口是否开放 :firewall-cmd --query-port =端口/协议

案例

比如说我个人的博客端口为 8080 关闭 8080端口尝试访问

[root@VM-12-5-centos ~]# firewall-cmd --query-port=8080/tcp
yes    #8080端口查询
[root@VM-12-5-centos ~]# firewall-cmd --permanent --remove-port=8080/tcp
success  #8080端口关闭
[root@VM-12-5-centos ~]# firewall-cmd --reload 
success  #重载
[root@VM-12-5-centos ~]# firewall-cmd --query-port=8080/tcp
no  # 再次查询

在这里插入图片描述
再次开放8080端口:

[root@VM-12-5-centos ~]# firewall-cmd --permanent --add-port=8080/tcp
success
[root@VM-12-5-centos ~]# firewall-cmd --reload 
success
[root@VM-12-5-centos ~]# firewall-cmd --query-port=8080/tcp
yes

动态监控系统

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

top [选项]

在这里插入图片描述
在这里插入图片描述在这里插入图片描述

监视待定用户 比如我们的fox用户

top :输入此指令,回车键,查看执行的程序
u:输入u回车再输入用户名即可

在这里插入图片描述

终止指定的进程,比如我们要结束fox用户登录

top :输入此指令,回车键,查看执行的程序
K: 输入此指令然后回车 再输入要结束的进程ID号

在这里插入图片描述

填入信号量 这里填 9代表强制结束

Send pid 4003 signal [15/sigterm] 

在这里插入图片描述

监控网络状态

查看系统网络情况 netstat

netstat [选项]
-an 按一定顺序排列输出
-p 显示哪个进程在调用

检测主机连接命令ping

[root@VM-12-5-centos ~]# netstat  -anp |more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      24700/./redis-serve 
tcp        0      0 10.0.12.5:8080          167.99.130.208:42691    SYN_RECV    -                   
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6477/nginx: worker  
tcp        0      0 0.0.0.0:21              0.0.0.0:*               LISTEN      6471/pure-ftpd (SER 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1434/sshd           
tcp        0      0 0.0.0.0:888             0.0.0.0:*               LISTEN      6477/nginx: worker  
tcp        0      0 0.0.0.0:8888            0.0.0.0:*               LISTEN      24186/python        
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      6099/master         
tcp        0      0 10.0.12.5:35550         45.223.129.36:8081      TIME_WAIT   -                   
tcp        0      0 10.0.12.5:8888          104.152.52.61:45203     ESTABLISHED 24186/python        
tcp        0      0 10.0.12.5:8888          104.152.52.73:44802     ESTABLISHED 24186/python        

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

netstat -anp | grep sshd

在这里插入图片描述

是一种网络检测工具,它主要是用检测远程主机是否正常,或是两部主机间的网线或网卡故障,如ping对方的地址

rpm和yum管理

rpm是用于互联网下载包的打包安装工具,它包含在某些Linux分发版中,他生成具有RPM扩展名的文件,RPM是RedHat Package Manage(RedHat软件包管理工具)的缩写,类似于setup.exe ,这一文件格式名称虽然打上了RedHat的标志,但理念还是存在的

查询已经安装的rpm列表

rpm -qa | grep xx
rpm -qa :查询安装的所有rpm软件包
rpm -qa|more
rpm -qa |grep X
rpm -q 软件包名 :查询软件是否已经安装
rpm -qi 软件包名 :查询软件包信息
rpm -ql 软件包名 :查询软件包中的文件
rpm -qf 文件全路径 查询文件所属的软件包

比如查询python的rpm
在这里插入图片描述
查询所有

[root@VM-12-5-centos ~]# rpm -qa
sqlite-3.7.17-8.el7_7.1.x86_64
ustr-1.0.4-16.el7.x86_64
libreport-2.1.11-53.el7.centos.x86_64
perl-ExtUtils-Install-1.58-299.el7_9.noarch
augeas-libs-1.4.0-10.el7.x86_64
kbd-misc-1.15.5-15.el7.noarch
mesa-libEGL-devel-18.3.4-12.el7_9.x86_64

rpm 包的基本格式

  1. 一个rpm包名:
  2. 名称
  3. 版本号
  4. 适用的操作系统
卸载rpm包

卸载的基本语法rpm -e RPM包的名称 //erase
如果其它软件包依赖于您要卸载的软件包,卸载时则会产生错误信息如果要强制删除可以增加参数–>--nodeps但是不推荐这样做,因为依赖于该软件包的程序可能无法正常运行

安装rpm包
rpm -ivh RPM 包全路径名称
i=install 安装
v=verbose 提示
h=hash 进度条
yum

yum是一个Shell前端软件包管理器,基于RPM包管理,能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖性关系,并且一次安装所有依赖的软件包

查询yum服务是否需要安装的软件

yum list | grep xx 软件列表

安装指定的yum包 

yum install xxx 下载安装

在这里插入图片描述

yum安装 firefox

[root@VM-12-5-centos containerd]# yum list | grep firefox
Repository epel is listed more than once in the configuration
firefox.i686                             91.10.0-1.el7.centos          updates  
firefox.x86_64                           91.10.0-1.el7.centos          updates  
firefox-noscript.noarch                  11.0.3-3.el7                  epel     
firefox-pkcs11-loader.x86_64             3.13.6-1.el7                  epel     
[root@VM-12-5-centos containerd]# yum install firefox
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值