Linux 基础入门 04
一、Linux 进程管理
1.1 什么是进程
进程是一个在系统中运行的程序
进程是已启动的可执行的运行实例,进程有以下部分组成:
(1)已分配内存的地址空间
(2)安全属性,包括所有权凭证和特权
(3)进程代码一个多个执行线程
(4)进程状态
1.2 程序与进程的区别
程序:是二进制文件,比如 /bin/
目录下的静态文件
进程:是程序运行的过程,动态的,有生命周期和运行状态
1.3 进程类型
守护进程:在系统引导过程中就启动的进程,跟终端无关的进程
前台进程:跟终端有关,可以通过终端启动的进程
1.4 进程的生命周期
父进程复制自己的地址空间(fork
)创建一个子进程。每个新进程分配一个唯一的进程ID(PID
),满足跟踪安全性需求。PID
和父进程ID (PPID
)是子进程的进程环境因素,任何进程都可以创建子进程,所有的进程都是第一个系统进程的后代。
PID
进程号,是唯一表示进程的ID
1.5 如何查看进程
ps #查看进程信息
a #查看所有终端进程
u #打印进程所有者信息
x #查看不属于任何终端的进程
-e #查看系统所有进程
-f #显示不包含资源使用率的相关信息
--sort #指定排序参数,例如--sort=-%mem 按照内存大小降序
实例1
#查看目前所有进程
[root@wentan ~]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.1 0.3 179000 13880 ? Ss 09:20 0:02 /usr/lib/systemd
root 2 0.0 0.0 0 0 ? S 09:20 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? I< 09:20 0:00 [rcu_gp]
列名 | 功能 |
---|---|
USER | 行程拥有者 |
PID | 进程ID |
%CPU | 占用的 CPU 使用率 |
%MEM | 占用的记忆体使用率 |
VSZ | 占用的虚拟记忆体大小 |
RSS | 占用的记忆体大小 |
TTY | 终端的次要装置号码 |
STAT | 该进程的状态 |
START | 行程开始时间 |
TIME | 执行的时间 |
COMMAND | 所执行的指令 |
1.6 系统状态类型
#STAT: 该进程的状态D: 无法中断的休眠状态 (通常 IO 的进程)
R: 正在执行中
S: 静止状态
s: 子进程
T: 暂停执行
Z: 僵尸进程,不存在但暂时无法消除
W: 没有足够的记忆体分页可分配
<: 高优先序的行程
N: 低优先序的行程
+: 后台进程
I: 多线程进程
L: 有记忆体分页分配并锁在记忆体内 (实时系统或捱A I/O)
实例1
#进程排序
[root@wentan ~]# ps aux --sort %cpu //按照CPU升序
[root@wentan ~]# ps aux --sort -%cpu //按照CPU降序
[root@wentan ~]# ps aux --sort %mem //按照内存升序
[root@wentan ~]# ps aux --sort rss //按照实际使用内存,有具体数值
实例2
我们还可以通过以下命令 ,来查看ssh这个服务的层级关系,按照父子进程来看
#父子进程层级关系
[root@wentan ~]# ps auxf | grep sshd
#说明:这里面f是表达程序间的相互关系
root 1107 0.0 0.1 92248 6720 ? Ss 09:20 0:00 /usr/sbin/sshd -D -oCiphers=aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr,aes256-cbc,aes128-gcm@openssh.com,aes128-ctr,aes128-cbc -oMACs=hmac-sha2-256-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha2-256,hmac-sha1,umac-128@openssh.com,hmac-sha2-512 -oGSSAPIKexAlgorithms=gss-gex-sha1-,gss-group14-sha1- -oKexAlgorithms=curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1 -oHostKeyAlgorithms=rsa-sha2-256,ecdsa-sha2-nistp256,ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384,ecdsa-sha2-nistp384-cert-v01@openssh.com,rsa-sha2-512,ecdsa-sha2-nistp521,ecdsa-sha2-nistp521-cert-v01@openssh.com,ssh-ed25519,ssh-ed25519-cert-v01@openssh.com,ssh-rsa,ssh-rsa-cert-v01@openssh.com -oPubkeyAcceptedKeyTypes=rsa-sha2-256,ecdsa-sha2-nistp256,ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384,ecdsa-sha2-nistp384-cert-v01@openssh.com,rsa-sha2-512,ecdsa-sha2-nistp521,ecdsa-sha2-nistp521-cert-v01@openssh.com,ssh-ed25519,ssh-ed25519-cert-v01@openssh.com,ssh-rsa,ssh-rsa-cert-v01@openssh.com
root 2396 0.0 0.2 159412 9472 ? Ss 09:23 0:00 \_ sshd: root [priv]
root 2417 0.0 0.1 159412 5172 ? S 09:23 0:00 \_ sshd: root@pts/0
root 3203 0.0 0.0 12112 1088 pts/0 S+ 10:33 0:00 \_ grep --color=auto sshd
实例3 查看整个系统的进程树(Centos7 系统不能直接看,需要安装)
#查看进程树
[root@wentan ~]# pstree
systemd─┬─ModemManager───2*[{ModemManager}]├─NetworkManager───2*[{NetworkManager}]├─VGAuthService├─accounts-daemon───2*[{accounts-daemon}]├─alsactl├─atd├─auditd─┬─sedispatch│ └─2*[{auditd}]├─avahi-daemon───avahi-daemon├─bluetoothd├─boltd───2*[{boltd}]├─chronyd├─colord───2*[{colord}]├─crond├─cupsd├─dbus-daemon───{dbus-daemon}├─dnsmasq───dnsmasq├─firewalld───{firewalld}├─gdm─┬─gdm-session-wor─┬─gdm-wayland-ses─┬─gnome-session-b─┬─gnome-shell─┬─Xwayla+│ │ │ │ │ ├─ibus-d+│ │ │ │ │ └─12*[{g+│ │ │ │ ├─gsd-a11y-settin───3*+│ │ │ │ ├─gsd-clipboard───2*[{+│ │ │ │ ├─gsd-color───3*[{gsd-+│ │ │ │ ├─gsd-datetime───2*[{g+│ │ │ │ ├─gsd-housekeepin───2*+│ │ │ │ ├─gsd-keyboard───3*[{g+│ │ │ │ ├─gsd-media-keys───3*[+│ │ │ │ ├─gsd-mouse───2*[{gsd-+│ │ │ │ ├─gsd-power───3*[{gsd-+│ │ │ │ ├─gsd-print-notif───2*+│ │ │ │ ├─gsd-rfkill───2*[{gsd+│ │ │ │ ├─gsd-screensaver───2*+│ │ │ │ ├─gsd-sharing───3*[{gs+│ │ │ │ ├─gsd-smartcard───5*[{+│ │ │ │ ├─gsd-sound───3*[{gsd-+│ │ │ │ ├─gsd-wacom───2*[{gsd-+│ │ │ │ ├─gsd-xsettings───3*[{+│ │ │ │ └─3*[{gnome-session-b}+│ │ │ └─2*[{gdm-wayland-ses}]│ │ └─2*[{gdm-session-wor}]│ └─2*[{gdm}]├─gssproxy───5*[{gssproxy}]├─ibus-x11───2*[{ibus-x11}]├─irqbalance───{irqbalance}├─ksmtuned───sleep├─libvirtd───16*[{libvirtd}]├─lsmd├─mcelog├─polkitd───11*[{polkitd}]├─rhsmcertd├─rngd───{rngd}├─rpcbind├─rsyslogd───2*[{rsyslogd}]├─rtkit-daemon───2*[{rtkit-daemon}]├─smartd├─sshd───sshd───sshd───bash───pstree├─sssd─┬─sssd_be│ └─sssd_nss├─systemd─┬─(sd-pam)│ ├─dbus-daemon───{dbus-daemon}│ └─pulseaudio───2*[{pulseaudio}]├─systemd─┬─(sd-pam)│ ├─at-spi-bus-laun─┬─dbus-daemon───{dbus-daemon}│ │ └─3*[{at-spi-bus-laun}]│ ├─at-spi2-registr───2*[{at-spi2-registr}]│ ├─dbus-daemon───{dbus-daemon}│ ├─ibus-portal───2*[{ibus-portal}]│ ├─pulseaudio───2*[{pulseaudio}]│ └─xdg-permission-───2*[{xdg-permission-}]├─systemd-journal├─systemd-logind├─systemd-machine├─systemd-udevd├─tuned───3*[{tuned}]├─udisksd───4*[{udisksd}]├─upowerd───2*[{upowerd}]├─vmtoolsd───{vmtoolsd}└─wpa_supplicant
1.7 常用命令
1.7.1 top 命令
用于显示process
动态
top [-] [d delay] [q] [c] [S] [s] [i] [n] [b]
#top
d : 改变显示的更新速度,或是在交谈式指令列( interactive command)按 s
q : 没有任何延迟的显示速度,如果使用者是有 superuser 的权限,则 top 将会以最高的优先序执行
c : 切换显示模式,共有两种模式,一是只显示执行档的名称,另一种是显示完整的路径与名称
S : 累积模式,会将己完成或消失的子进程 ( dead child process ) 的 CPU time 累积起来
s : 安全模式,将交谈式指令取消, 避免潜在的危机
i : 不显示任何闲置 (idle) 或无用 (zombie) 的进程
n : 更新的次数,完成后将会退出 top
b : 批次档模式,搭配 "n" 参数一起使用,可以用来将 top 的结果输出到文件内
实例
#显示进程信息
top
#显示完整命令
top -c
#以批处理模式显示程序信息
top -b
#以累积模式显示程序信息
top -S
#设置信息更新次数
top -n 2
//表示更新两次后终止更新显示
#设置信息更新时间
top -d 3
//表示更新周期为3秒
#显示指定的进程信息
top -p 139
//显示进程号为139的进程信息,CPU、内存占用率等
#显示更新十次后退出
top -n 10
#使用者将不能利用交谈式指令来对行程下命令
top -s
1.7.2 kill 命令
kill
命令用于删除执行中的程序或工作
kill
可以将指定的信号送到程序,默认信号是15,杀死, 可以杀死指定程序。
# kill选项
-l #查看所有的信号名称
-s #指定要送出信号是那个一个
程序的PID
就是具体指出是针对哪个程序
常用信号
编号 | 信号名 | 作用 |
---|---|---|
1 | SIGHUP | 重新加载配置 |
2 | SIGINT | 键盘中断^C |
3 | SIGQUIT | 键盘退出 |
9 | SIGKILL | 强制终止 |
15 | SIGTERM | 终止(正常结束),缺省信号 |
18 | SIGCONT | 继续 |
19 | SIGSTOP | 停止 |
20 | SIGTSTP | 暂停^Z |
实例
[root@wentan ~]# kill 5011 #正常终止进程5011
[root@wentan ~]# kill -9 5011 #强制终止进程5011
1.7.3 pkill 命令
用于杀死一个进程,与 kill
不同的是它会杀死指定名字的所有进程
实例1
[root@wentan ~]# pkill sshd #结束sshd所有进程
[root@wentan ~]# pkill -u user1 #杀死用户user1的所有进程
[root@wentan ~]# pkill -t pts/2 #杀死pts/2上所有进程
实例2
查看远程登录用户,并且踢出用户
[root@wentan ~]# w11:52:10 up 36 min, 4 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty2 tty2 11:16 36:21 34.01s 0.12s /usr/libexec/tracker-miner
root pts/0 192.168.200.1 11:17 15:30 0.06s 0.06s -bash
root pts/1 192.168.200.1 11:35 1.00s 0.04s 0.01s w
wentan pts/2 192.168.200.1 11:49 1:21 0.02s 0.02s -bash
发现wentan
用户正在通过IP地址192.168.200.1
登录,可以把这个用户踢下线
[root@wentan ~]# pikill -u wentan
用户下线
1.7.4 nice 命令
可以用来修改程序的优先级别,如果未指定程序,则会显示目前程序的优先级别
默认nice值为0
范围是-20~19
nice
数值越小表示程序会越优先处理,在系统运行缓慢的时候,nice
值越小,进程越优先执行,处理级别越高。
Linux 进程调度及多任务:
每个CPU(或CPU核心)在一个时间点上只能处理一个进程,通过时间片技术,Linux实际能够运行的进程(和线程数)可以超出实际可用的CPU及核心数量。Linux内核进程调度程序将多个进程在CPU核心上快速切换,从而给用户多个进程在同时运行的假象。
通过调整程序在计算机中的优先级别,可以让程序在时间片中占有更长的时间,从而获得更快的处理速度
实例 更改优先级
#在进程启动时,附加一个nice值
[root@wentan ~]# nice -n 5 vim /etc/passwd#在进程启用之后
[root@wentan ~]# renice -10 3174 (PID)//只有root才可以降低nice值
ps aux 找到该进程
二、Linux 软件仓库
软件仓库的名称叫 yum/dnf
红帽系列操作系统软件仓库位置在 /etc/yum.repos.d/
2.1 配置软件仓库两种方法
(1)已知软件仓库的基础url位置,自己手动编辑配置文件,例如
#手动书写
[root@wentan yum.repos.d]# vim /etc/yum.repos.d/rhel_dvd.repo
[baseos]
name = Red Hat Enterprise Linux 8.2 BaseOS
baseurl = .2/x86-64/dvd/BaseOS
enabled = 0
gpgcheck = 1
gpgkey = .2/gpgkey-base[appstream]
name = Red Hat Enterprise Linux 8.2 AppStream
baseurl = .2/x86-64/dvd/AppStream
enabled = 0
gpgcheck = 1
gpgkey = .2/gpgkey-base
(2)直接从各大网站现有提供软件仓库配置文件,直接下载即可
①接下来需要先卸载红帽原有仓库
#卸载原仓库
[root@wentan ~]# cd /etc/yum.repos.d/
[root@wentan yum.repos.d]# ll
total 4
-rw-r--r--. 1 root root 358 Jan 5 04:36 redhat.repo
[root@wentan yum.repos.d]# rm -f redhat.repo
[root@wentan yum.repos.d]# ll
total 0
②从阿里云下载软件仓库
#下载阿里云软件仓库
[root@wentan yum.repos.d]# curl -o /etc/yum.repos.d/Rhel8.repo .repo% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed
100 2595 100 2595 0 0 12416 0 --:--:-- --:--:-- --:--:-- 12416
[root@wentan yum.repos.d]# ll
total 4
-rw-r--r--. 1 root root 2595 Jan 12 13:43 Rhel8.repo
③需要先清除历史软件仓库缓存,再建立新仓库
#清空缓存
[root@wentan yum.repos.d]# yum clean all
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
17 files removed
#启用
[root@wentan yum.repos.d]# yum repolist all
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
CentOS-8 - AppStream - mirrors.aliyun.com 1.9 MB/s | 8.4 MB 00:04
CentOS-8 - Base - mirrors.aliyun.com 1.8 MB/s | 4.6 MB 00:02
CentOS-8 - Extras - mirrors.aliyun.com 4.8 kB/s | 10 kB 00:02
repo id repo name status
AppStream CentOS-8 - AppStream - mirrors.aliyun.com enabled: 5,596
PowerTools CentOS-8 - PowerTools - mirrors.aliyun.com disabled
appstream Red Hat Enterprise Linux 8.2 AppStream disabled
base CentOS-8 - Base - mirrors.aliyun.com enabled: 1,896
baseos Red Hat Enterprise Linux 8.2 BaseOS disabled
centosplus CentOS-8 - Plus - mirrors.aliyun.com disabled
extras CentOS-8 - Extras - mirrors.aliyun.com enabled: 38
注意:EPEL(Extra Packages for Enterprise Linux)为红帽操作系统提供了额外的软件仓库
[root@wentan yum.repos.d]# yum install -y epel-release
2.2 关于软件的安装、更新、卸载、查找
实例1
#软件仓库安装httpd
[root@wentan ~]# yum install -y httpd
#软件仓库更新httpd
[root@wentan ~]# yum update httpd
#软件仓库卸载httpd
[root@wentan ~]# yum remove -y httpd
实例2
yum list httpd #软件仓库查找叫httpd的软件列表
yum list installed #软件仓库查询已经下载的软件列表
yum info httpd #软件仓库查询已经安装的httpd软件信息
yum history #软件仓库历史命令
yum history info 4 #查找软件仓库历史命令4谁干的 干了什么
yum list #显示所有软件仓库可安装的软件
yum list |grep locate #显示所有软件仓库可安装的软件查找名字叫locate的
yum search locate #软件仓库中去搜寻名字带有locate的软件
yum provides /etc/ssh/sshd_config #软件仓库中查找 哪个软件提供了这个配置文件的功能
yum provides ifconfig #软件仓库查找谁提供了命令 ifconfig
yum provides pstree #软件仓库查找谁提供了命令 pstree
yum search gnome #软件仓库搜索带有桌面功能的软件
yum groupinfo "gnome" #查找软件包组桌面这个功能一共要安装多少软件
实例3 软件包组的安装:
yum group install gnome -y
通过软件包组,直接安装整个桌面的需要的所有软件包
2.3 关于软件仓库本身的更新
[root@wentan ~]# yum update
#更新最新的软件仓库目录,如果有新的软件加入软件仓库,不执行这个命令就来安装,有可能会显示这个软件找不到。一般在安装最新的有些工具的时候,需要先yum update完成软件仓库本身的更新
因为我们系统属于未注册 yum update --allowerasing
允许擦除不兼容的部分,防止多个软件之间冲突
2.4 rpm 命令
rpm用来安装软件包
rpm -i #安装
rpm -v #查看详细信息
rpm -e #删除移除
rpm -U #升级
实例 安装QQ for Linux
#首先可以下载 QQ for Linux 版本
wegt .0.0-b2-1089_x86_64.rpm
rpm -i linuxqq_2.0.0-b2-1089_x86_64.rpm
#卸载QQ for Linux
rpm -e linuxqq
三、服务管理和守护进程
systemd 守护进程 会伴随着Linux系统启动。在系统引导的时候,守护进程会激活系统资源启动
在红帽系中,systemd
守护进程的PID
是1
systemd
提供了激活进程和自启动服务
3.1 systemd 服务单元
service .service #定义系统服务
target .target #模拟运行级别
device .device #定义内核识别的设备
mount .mount #挂载的文件
socket .socket #标识进程间的通信
swap .swap #管理swap设备
automount .automount #实现文件开机自动挂载
path .path #定义文件系统目录路径
实例1
#列出所有的service的模块
[root@wentan ~]# systemctl list-units --type=service
UNIT LOAD ACTIVE SUB DESCRIPTION
accounts-daemon.service loaded active running Accounts Service
...#列出所有的target的模块
[root@wentan ~]# systemctl list-units --type=target
UNIT LOAD ACTIVE SUB DESCRIPTION
basic.target loaded active active Basic System
bluetooth.target loaded active active Bluetooth
cryptsetup.target loaded active active Local Encrypted Volumes
getty.target loaded active active Login Prompts
graphical.target loaded active active Graphical Interface
UNIT:服务单元名称
LOAD: systemd是否正确解析了单元的配置,并将该单元加载到内存中
ACTIVE:单元的高级别激活,此信息表明是否成功启动
SUB:单元的低级别激活状态 ,以单元的方式在执行的
DESCRIPTION:描述
参数
target systemctl #系统运行级别管理
graphical.target #系统支持多用户,多图形化和文本的登录
multi-user.target #系统支持多用户,基于文本的登录
rescue.target #系统初始化 需要root登录
emergency.target #只读挂载文件系统 需要用root登录
实例2
#查看当前系统的默认运行级别
[root@wentan ~]# systemctl get-defaut
#将系统运行级别设置为基于文本登录的方式
[root@wentan ~]# systemctl set-default multi-user.target
Removed /etc/systemd/system/default.target.
Created symlink /etc/systemd/system/default.target → /usr/lib/systemd/system/multi-user.target.
紧急模式下,我们不能对系统做任何操作,那怎么恢复正常模式呢?
首先重新启动
然后把光标移到需要启动的内核(第一个)
然后按e进行编辑
然后移动到linux
开头的行 在末尾输入
systemd.unit=graphical.target
然后ctrl+x
,就可以正常开机了
注意:这只是打断了当前本次开机进程
此时你的默认target
还是emergency
所以尽快修改为graphical.target
3.2 服务管理
systemctl start name.service | 启动服务 |
---|---|
systemctl stop name.service | 停止服务 |
systemctl restart name.service | 重启服务(没启动的服务会启动) |
systemctl try-restart name.service | 只重启正在运行的服务 |
systemctl reload name.service | 重载配置文件 |
systemctl status name.service systemctl is-active name.service | 检查服务状态检查服务是否启动 |
systemctl list-units --type service --all | 显示所有的服务状态 |
systemctl enable name.service | 启用开机自启服务 |
systemctl disable name.service | 停用自启服务 |
systemctl status name.service systemctl is-enabled name.service | 检查服务状态查看服务是否自启 |
systemctl list-unit-files --type service | 查看所有服务 |
systemctl list-dependencies --after | 列出在指定服务之前启动的服务(依赖) |
systemctl list-dependencies --before | 列出在指定服务之后启动的服务(被依赖 |
实例
[root@wentan ~]# systemctl start httpd
[root@wentan ~]# systemctl status httpd
[root@wentan ~]# systemctl stop httpd
[root@wentan ~]# systemctl restart httpd
[root@wentan ~]# systemctl reload httpd
[root@wentan ~]# systemctl enable httpd //开机自启
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@wentan ~]# systemctl disable httpd //屏蔽开机自启
Removed /etc/systemd/system/multi-user.target.wants/httpd.service.
#启动之前依赖的服务
[root@wentan ~]# systemctl list-dependencies --after httpd
#启动之后,被依赖的服务
[root@wentan ~]# systemctl list-dependencies --before httpd
httpd.service
● └─shutdown.target
3.3 服务屏蔽
为了防止系统之间服务冲突,所以可以屏蔽某些服务,防止别人意外启动
[root@wentan ~]# systemctl stop httpd
[root@wentan ~]# systemctl disable httpd
#屏蔽某个服务
[root@wentan ~]# systemctl mask httpd
Created symlink /etc/systemd/system/httpd.service → /dev/null.
#取消屏蔽服务
[root@wentan ~]# systemctl unmask httpd
Removed /etc/systemd/system/httpd.service.
说明:disable
的服务是可以手动启动的,因为他只是不让开机启动,
mask
的服务,是没法手动启动的,更不会开机启动
get.wants/httpd.service.
#启动之前依赖的服务
[root@wentan ~]# systemctl list-dependencies --after httpd
#启动之后,被依赖的服务
[root@wentan ~]# systemctl list-dependencies --before httpd
httpd.service
● └─shutdown.target
##### 3.3 服务屏蔽为了防止系统之间服务冲突,所以可以屏蔽某些服务,防止别人意外启动```bash
[root@wentan ~]# systemctl stop httpd
[root@wentan ~]# systemctl disable httpd
#屏蔽某个服务
[root@wentan ~]# systemctl mask httpd
Created symlink /etc/systemd/system/httpd.service → /dev/null.
#取消屏蔽服务
[root@wentan ~]# systemctl unmask httpd
Removed /etc/systemd/system/httpd.service.
说明:disable
的服务是可以手动启动的,因为他只是不让开机启动,
mask
的服务,是没法手动启动的,更不会开机启动
关注公众号:问渠清源
回复关键字 视频 获取视频教程
发布评论