紅帽學習筆記[RHCSA]
環境
- rhel-server-7.0-x86_64-dvd.iso 鏡像
- VMware-workstation
第一課
關於Shell
- Shell是什么
Shell是系統的用戶界面,提供了用戶與內核進行交互操作的一種接口。它接收用戶輸入的命令並把它送入內核中執行。
- bash shell是大多數Linux的缺省shell
- 交互方式使用shell會顯示一個字符
# root用戶是 #
[root@c2c9702c7e20 /]#
# 普通用戶是 $
[test@c2c9702c7e20 /]$
- 使用shell 需要一個終端:虛擬控制台
# 如果是圖形化界面的
[CTRL+ALT]+F1 是圖形界面
[CTRL+ALT]F2-F6是5個紅帽的虛擬控制台
#如果是關閉圖形化界面
[CTRL+ALT] F1-F5是5個紅帽的虛擬控制台
命令的基礎知識
- 組成
#命令由三部分組成
命令:需要運行
選項:用於調整命令的行為
參數:通常是命令的目標
在終端中敲命令的快捷鍵
Ctrl+a : 跳到命令行頭部
Ctrl+e :跳到命令結尾
Ctrl+u :刪除到光標前的內容
Ctrl+k :刪除光標之后的內容
Ctrl+l : 刪除面板上的內容
Ctrl+r : 查找 history中內容
本次課程涉及的命令
ls
查看當前目錄下的文件以及文件夾的屬性。
ls
#查看當前文件夾下的n
[root@localhost /]# ls test
11.txt 22.txt 33 44
# ----------------------------
ls -d test/
# -d的意義會查看后面這個test目錄,如果不加,ls會查看test里面的內容
[root@localhost /]# ls -d test
test
# ----------------------------
ls -l 等價於 ll
# -l的意義是長格式查看,會顯示權限、所屬者、大小等信息
[root@localhost /]# ls -l test
total 0
-rw-r--r--. 1 root root 0 Aug 25 11:21 11.txt
-rw-r--r--. 1 root root 0 Aug 25 11:22 22.txt
drwxr-xr-x. 2 root root 6 Aug 25 11:22 33
drwxr-xr-x. 2 root root 6 Aug 25 11:22 44
# ----------------------------
ls -a
# -a會顯示隱藏文件
[root@localhost /]# ls -a test
. .. 11.txt 22.txt 33 44
# ----------------------------
ls -R
# 遞歸顯示目錄
[root@localhost /]# ls -R test
test:
11.txt 22.txt 33 44
test/33:
test/44:
mkdir
創建文件夾
mkdir -p /test/11/22/33
# -p參數會根據需要創建父級文件夾。以上,如果不-p的話 由於11文件夾不存在創建會失敗
passwd
修改密碼
[root@localhost /]# passwd
Changing password for user root.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
date
查看日期
# 查看當天
[kane@localhost /]$ date
Mon Aug 26 15:01:53 CST 2019
# 查看未來45天
[kane@localhost /]$ date -d "+45days"
Thu Oct 10 15:01:59 CST 2019
file
掃描內容的開頭,顯示文件類型。
[root@localhost /]# file test
test: directory
[root@localhost /]# file test/11.txt
test/11.txt: ASCII text, with very long lines
head
用來顯示文件的開頭
默認前10行。可以通過-n 設定顯示行數。也可以直接-number
[root@localhost test]# head 22.txt
1
2
3
4
5
6
7
8
9
10
[root@localhost test]# head -1 22.txt
1
[root@localhost test]# head -n 2 22.txt
1
2
tail
顯示文件末尾
默認顯示前10行。其他的與head類似,除了-f
[root@localhost test]# tail -f 22.txt
6
7
8
9
10
11
12
13
14
#除了顯示尾10行之外,還會監測22.txt文件的變化並輸出到屏幕上。
wc
計算文件見行數、字數、字符數
[root@localhost test]# wc 22.txt
15 14 34 22.txt
#行 字 字符 文件
#-l 單獨行數 -w單獨字數 -c單獨字符數
history
與!
歷史記錄,快速使用歷史命令
[root@localhost test]# history
225 head 22.txt
226 head -1 22.txt
227 head -n 2 22.txt
228 head --help
229 tail -f 22.txt
230 wc 22.txt
231 wc -l
232 wc -l 22.txt
233 wc -w 22.txt
234 wc -c 22.txt
235 history
# 使用編號為234的歷史命令
[root@localhost test]# !234
wc -c 22.txt
34 22.txt
# 使用首字母是wc的最后一個命令
[root@localhost test]# !wc
wc -c 22.txt
34 22.txt
# 清除歷史記錄
history -c # 重啟后歷史記錄還在
# 刪除某個編號的歷史記錄
history -d 234
第二課
常用的目錄結構與用途
/ 根目錄
/boot 存儲的是系統起動時的信息和內核等
/dev 存儲的是設備文件
/etc 存儲的是系統的配置文件
/root 存儲的是root用戶的家目錄
/home 存儲的是普通用戶的家目錄
/mnt 存儲的是自定義的掛在光盤或U盤
/var 存儲的系統里經常變化的信息,比如日志
/tmp 存儲的是臨時文件
/bin 出處的是系統里的基本命令。普通用戶就可以執行
/sbin 存儲的是管理類的命令,root用戶執行
/usr 存儲的是應用程序文件,占用磁盤最大的
/proc 偽文件系統,幾乎里面的東西全是內存中的數據,基本不占用磁盤空間。
本次課程涉及到的命令
cd
切換目錄
# cd 什么都不加會切換到當前用戶的家目錄
[root@localhost /]# cd
[root@localhost ~]#
# cd ~ 同樣的操作
[root@localhost /]# cd ~
[root@localhost ~]#
# cd -切換到上一次的目錄
[root@localhost ~]# cd -
/
[root@localhost /]#
# cd . 當前目錄
[root@localhost /]# cd .
[root@localhost /]#
# cd ..切換到上一級目錄
[root@localhost test]# cd ..
[root@localhost /]#
pwd
顯示當前所在目錄
[root@localhost test]# pwd
/test
[root@localhost test]#
$LANG
臨時切換語言環境
[root@localhost test]# LANG=zh_CN.UTF8
[root@localhost test]# aa
bash: aa: 未找到命令...
[root@localhost test]# LANG=zh_EN.UTF8
[root@localhost test]# aa
bash: aa: command not found...
touch
更新文件時間戳。
將文件的時間戳更新為當前的日期與時間,如果文件不出存在會創建文件
[root@localhost test]# touch 33.txt
[root@localhost test]#
[root@localhost test]# ls
11.txt 22.txt 33 33.txt 44
# 可以同時touch多個文件
[root@localhost test]# touch 11.txt 22.txt 33.txt 44.txt
[root@localhost test]# ls
11.txt 22.txt 33.txt 44.txt 55
# 關於同時創建多個文件的寫法
# 創建100個文件 test1.mul 到 test100.mul
[root@localhost test]# touch test{1..100}.mul
# 創建26個文件 testa.mul 到 testz.mul
[root@localhost test]# touch test{a..z}.mul
# 創建兩個文件 test_i.mul test_y.mul
[root@localhost test]# touch test{i,y}.nul
# ------------------------------------------------
# {}可以並列寫多個,可以嵌套寫,{..}表示一連串{,}表示並列。
cp
復制文件或者目錄
[root@localhost test]# cp 11.txt /test1
[root@localhost test]# ls test1
ls: cannot access test1: No such file or directory
[root@localhost test]# ls /test1
11.txt
mv
移動文件,或重命名文件
# test文件夾下
[root@localhost test]# ls
11.txt 22.txt 33 33.txt 44
# 將22.txt移動到 /test1下
[root@localhost test]# mv 22.txt /test1
# 將33.txt重命名為44.txt
[root@localhost test]# mv 33.txt 44.txt
#查看test下結果
[root@localhost test]# ls
11.txt 33 44 44.txt
#查看test1下結果
[root@localhost test]# ls /test1
11.txt 22.txt
rm
刪除文件或文件夾
# 查看test內容
[root@localhost test]# ls
11.txt 33 44 44.txt
# 刪除11.txt 提示是否確認刪除
[root@localhost test]# rm 11.txt
rm: remove regular file '11.txt'? y
# -f 強制刪除44.txt,不提示是否刪除
[root@localhost test]# rm -f 44.txt
# 刪除33文件夾失敗
[root@localhost test]# rm -f 33
rm: cannot remove '33': Is a directory
# -r 刪除文件夾
[root@localhost test]# rm -rf 33
[root@localhost test]# ls
44
rmdir
刪除空的文件夾
# 遞歸顯示55文件夾下有文件 44是空的文件夾
[root@localhost test]# ls -R
.:
44 55
./44:
./55:
11.txt
# 刪除空的44
[root@localhost test]# rmdir 44
# 刪除非空55失敗
[root@localhost test]# rmdir 55
rmdir: failed to remove '55': Directory not empty
ESC+.
使用上一個命令的最后一個參數
這是一個快捷鍵,使用是一條命令的最后一個參數
[root@localhost test]# ls 55
11.txt
# 下面這個 55 是通過ESC+.按出來的
[root@localhost test]# ls 55
clear
清空當前屏幕,但是不是真正的刪除
# CTRL + L相同的作用
man
和 firefox file:///usr/share/doc/
查看幫助
# 查看 ls 命令的幫助
[root@localhost test]# man ls
# 用 火狐打開幫助文檔
firefox file:///usr/share/doc/
第三課
關於Linux的輸入輸出
- 輸入輸出
0 stdin 標准輸入 僅讀取
1 stdout 標准輸出 僅寫入
2 stderr 標准錯誤 僅寫入
3 filename 其他文件 讀取和/或寫入
- 輸出重定向
# > file 或 1 > file 標准輸出重定向到file並覆蓋文件
[root@localhost test]# ls > stdout
[root@localhost test]# cat stdout
11.txt
22.txt
stdout
# >> file 或 1 >> file 標准輸出重定向到file追加到文件結尾
[root@localhost test]# ls >> stdout
[root@localhost test]# cat stdout
11.txt
22.txt
stdout
11.txt
22.txt
stdout
# 2 > file 將標准錯誤重定向到file >>用法一致
# 將標准輸出或者標准錯誤丟棄
[root@localhost test]# ls > /dev/null
# >file 2>&1 或 &>file 將標准錯誤重定向到標准輸出
本次課程涉及的命令
find
查找某個名字的文件或者文件夾
[root@localhost test]# find / -name test
/root/test
/var/lib/AccountsService/users/test
/var/db/sudo/test
/var/spool/mail/test
/usr/bin/test
/usr/lib/alsa/init/test
/usr/lib64/python2.7/test
/usr/lib64/python2.7/site-packages/OpenSSL/test
/usr/lib64/python2.7/unittest/test
/usr/share/espeak-data/voices/test
/home/test
/test
# / 是查找的目錄 test是查找的精確字符
# 使用 通配符 * 表示匹配任意
grep
在某個文件中查找字符
[root@localhost test]# grep boy 11.txt
I am boy
# 帶行數 -n
[root@localhost test]# grep bo 11.txt -n
1:I am boy
# -v不匹配
[root@localhost test]# grep bo 11.txt -nv
2:I am girl
3:hahahaha
# -B查看前面的行數-A查看后面的行,會帶出查找行以及前面或后面的行
[root@localhost test]# grep bo 11.txt -n -A1
1:I am boy
2-I am girl
|
管道前一個標准輸出作為后面的標准輸入
[root@localhost test]# cat /etc/passwd | grep root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator
tee
將標准輸入復制到標注輸出中,並且還會向標准輸出輸出內容。用於保存中間步驟的標注輸出
[root@localhost test]# cat /etc/passwd |tee passwd.txt| grep root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost test]# ls
11.txt 22.txt passwd.txt stderr stdout
vi
或vim
-
四種模式
- 命令模式
- 編輯模式
- 末行模式
- 可視化模式
-
切換模式
- 剛進入vi時,是
命令模式
insert
I
i
A
a
O
o
都可以進入編輯模式
I : 再本行行首進行插入 i: 光標前面插入 a: 光標后面插入 A: 本行得行尾進行插入 o: 新建一行插入 O: 本行得上一行進行插入 # ------------------- 按`ESC`退出編輯模式回到命令模式
shift+;
也是是:
可以進入末行模式
Ctrl+V
或Ctrl+v
進入可視化模式
- 剛進入vi時,是
-
命令模式命令匯總
x : 光標在哪里刪除哪里
u : 撤銷操作
Ctrl+r : 重做撤銷
dw : 刪除一個單詞
dd : 刪除一行
dG : 刪除光標行到文件末
dgg : 刪除光標行到文件首
yy : 復制一行
p : 粘貼
5yy : 復制多行
yw : 復制一個單詞
G : 到文件末尾
gg : 到文件首部
- 末行模式常用匯總
q #離開
w #保存
x #保存離開 wq
q! #強制離開
r /test/11.txt # 將11.txt文件的內容,追加到當前編輯文件的尾部
- Linux 自帶的
vimtutor
教程
第四課
關於Linux 的用戶
- 用戶分類:
# UID 是用戶ID
UID 0分配給超級用戶(root)
UID 1-200 是一系列的 系統用戶 靜態分配給紅帽的系統進程
UID 201-999 是一系列的 系統用戶,共文件系統中沒有自己的文件的系統進程使用。通常在安裝需要他們的軟件時,從可用池中動態分配他們。程序一這些五特權的系同用戶身份運行。一邊限制他們僅訪問正常運行的所需資源
UID 1000+ 普通用戶
- 用戶組
# 主要組或基本組
如果沒有指定用戶組,創建用戶的時候系統會默認同時創建一個和這個用戶名同名的組,這個組就是基本組。在創建文件時,文件的所屬組就是用戶的基本組。
# 附屬組
除了基本組之外,用戶所在的其他組,都是附加組。用戶是可以從附加組中被刪除的。
# ---------------------------
用戶不論為與基本組中還是附加組中,就會擁有該組的權限。一個用戶可以屬於多個附加組。但是一個用戶只能有一個基本組。
- 關於用戶與組的信息存放位置
/etc/passwd 用戶信息
/etc/shadow 用戶密碼相關
/etc/group 用戶組信息
/etc/gshadow 存放組密碼信息
- 關於3中文件的內容
# /etc/passwd
用戶名:密碼:UID:GID:描述信息:家目錄:shell環境
# /etc/shadow
用戶名:加密后的密碼:最后一次修改密碼的時間:密碼最小生存周期:密碼最大生存周期:密碼到期前一天開始警告:密碼到期之后還可以使用的天數:賬號過期時間:保留項
Linux 紅帽系破解root密碼
- 重啟 reboot
- 進入啟動項后,選擇非默認項,按鍵
e
進入編輯模式
# 我的常用默認項
Red Hat Enterprise Linux Server,with Linux 3.10.0-123.e17.x86_64
# 我的非默認項
Red Hat Enterprise Linux Server,with Linux 0-rescue-
- 找到
linux16
關鍵字所在行,在行末鍵入console=tty0 rd.break
- 按
Ctrl+x
,等待進入 switch_root模式 - 鍵入
mount -0 remount,rw /sysroot
- 鍵入
chroot /sysroot
- 鍵入
echo "new passwd"|passwd --stdin root
- 鍵入
touch /.autorelabel
- 鍵入兩次
exit
,等待自動重啟。
本次課程涉及的命令
id
查看用戶信息
# 查看當前用戶
[root@localhost Desktop]# id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
# 查看其他用戶
[root@localhost Desktop]# id user1
uid=1003(user1) gid=1003(user1) groups=1003(user1),10(wheel)
passwd
修改用戶密碼
# 修改當前用戶密碼
[root@localhost Desktop]# passwd
Changing password for user root.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
# root用戶修改普通用戶的密碼
[root@localhost Desktop]# passwd user1
Changing password for user user1.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
useradd
添加用戶
# 創建用戶
[root@localhost Desktop]# useradd testuser
[root@localhost Desktop]# id testuser
uid=1008(testuser) gid=1008(testuser) groups=1008(testuser)
# -u 指定UID
# -G 指定附屬組
# -g 指定主組(默認會創建一個與user同名的組)
# -s 指定一個shell
# 不讓用戶登陸系統
[root@localhost Desktop]# useradd test_no_login -s /sbin/nologin
[root@localhost Desktop]# id test_no_login
uid=1009(test_no_login) gid=1009(test_no_login) groups=1009(test_no_login)
[root@localhost Desktop]# su test_no_login
This account is currently not available.
# 發現不能夠登陸shell
usrmod
修改用戶一些信息。
# -u 修改UID
# -L 鎖定用戶
# -s 指定一個shell
# -U 解鎖用戶
# -g 修改主要組
# -G 修改附屬組
usrdel
刪除用戶
# 刪除用戶
[root@localhost Desktop]# userdel user2
# -r 刪除家目錄
[root@localhost Desktop]# userdel -r user2
su
切換用戶
# root用戶切換到任何用戶不需要輸入密碼
# su - test 是用這個用戶直接登陸。不加-是用它的shell環境
[root@localhost Desktop]# su kane
[kane@localhost Desktop]$
sudo
以root身份運行命令。注:需要再附屬組上增加(wheel)
[kane@localhost /]$ rm -rf test
rm: cannot remove ‘test’: Permission denied
[kane@localhost /]$ sudo rm -rf test
[sudo] password for kane:
[kane@localhost /]$
groupadd
添加一個組
# -g可以直接給一個組ID
groupmod
修改組的信息
# 修改組id
groupmod -g gid
# 修改組的名字
groupmod -n new_name old_name
groupdel
刪除組
chage
負責管理用戶密碼時效問題
# -m 兩次改變密碼之間相距的最小天數即"最小天數"
# -M 兩次改變密碼之間相距的最大天數即"最大天數"
# -W 過期警告天數即"警告天數"
# -E 過期日期 日期格式:YYYY-MM-DD
chage -E 2019-10-01 test
第五課
用戶權限
- 查看文件的權限
[kane@localhost /]$ ll
total 36
----------. 1 root root 1751 Aug 22 20:58 ~
lrwxrwxrwx. 1 root root 7 Aug 16 04:39 bin -> usr/bin
dr-xr-xr-x. 3 root root 4096 Aug 15 20:55 boot
drwxr-xr-x. 20 root root 3260 Aug 26 10:07 dev
drwxr-xr-x. 132 root root 8192 Aug 26 15:00 etc
- 關於查看為你教案權限結果的解釋
# 1位 :d是目錄 -是 文件 c代表字符設備 b代表塊設備 l代表得是連接設備
# -------------------------------------
# 2-10位:三組權限,每三個一組一次為:[用戶u][組g][其他o]
# r可讀 w可寫 x可執行- 是沒有這個權限
# 數字表示 r:4 w:2 x:1 -:0 [修改權限使用]
# -------------------------------------
# 11位 代表有多少的連接
# 12位 用戶
# 13位 所屬組
# 14位 文件大小,默認是字節單位
# 15位 最后修改的時間
# 16位 文件名字
特殊權限
- suid 表示以文件的擁有者身份運行
- sgid 表示以文件的所屬者身份運行
- sticky 表示目錄下面創建的文件將繼承目錄的組所屬的關系,而不是用戶的。只有對目錄有w全縣的用戶可以刪除擁有的的文件,其他擁有着都不可以刪除。
- 特殊權限的表示
分別占用 u g o 三組權限的得第三位
suid s 正常 S說明沒有x權限
guid s 正常 S說明沒有x權限
sticky t 正常 T說明沒有x權限
# --------------演示---------------
[root@localhost /]# chmod u+s test
[root@localhost /]# ls -dl test
drwsrwxrwx. 2 root root 6 Aug 26 16:10 test
# 去除執行權限s變成S
[root@localhost /]# chmod u-x test
[root@localhost /]# ls -dl test
drwSrwxrwx. 2 root root 6 Aug 26 16:10 test
# 其他兩個類似
# 數字法 suid =4 sgid 2 sticky 1
# 用數字法設置sgid
[root@localhost /]# chmod 2777 test
[root@localhost /]# ls -dl test
drwsrwsrwx. 2 2777 root 6 Aug 26 16:10 test
創建文件文件夾默認權限umask
- 查看umask
# root用戶 0022
[root@localhost /]# umask
0022
# 普通用戶0002
[root@localhost /]# su kane
[kane@localhost /]$ umask
0002
- 默認權限
# 目錄默認權限 777
777-umask
# 文件默認權限 666
666-umask
# 以root用戶為例
[root@localhost test]# touch 11.txt
[root@localhost test]# mkdir 11
[root@localhost test]# ll
total 0
drwxr-sr-x. 2 root root 6 Aug 26 16:33 11
-rw-r--r--. 1 root root 0 Aug 26 16:32 11.txt
# 11.txt權限是 664
# 11 文件夾權限是644
- 修改 umask
# 臨時修改
umask = 777
# 永久修改,不建議
修改 .bash_profile /etc/profile
本次課程涉及到的命令
chmod
修改文件權限
# 數字法修改,7是4(r)+2(w)+1(x) 三個7是設置3組
[root@localhost /]# chmod 777 test
# 符號法修改,可以直接等,可加,可減
[root@localhost /]# chmod u=rw test
[root@localhost /]# chmod u+x test
chown
修改用戶與組
chown user1 123 #將123的用戶改成user1
chown : user2 123 #將123的組改成user1
chown user3:user3 123 #同時設置用戶與組
chown --reference /tmp/ /test/ 復制文件得權限
第六課
進程
進程:已經啟動的可執行程序的運行中的實例。每個進程都有自己的地址空間,並占用了一定的系統資源。
如何產生一個進程
- 執行程序或命令
- 計划任務
在終端中對進程管理
- 運行一個前台進程
[root@master Desktop]# firefox
(process:3731): GLib-CRITICAL **: g_slice_set_config: assertion `sys_page_size == 0' failed
# 會占用當前得終端
- 運行一個后台進程
[root@master Desktop]# firefox &
[1] 3796
[root@master Desktop]#
(process:3796): GLib-CRITICAL **: g_slice_set_config: assertion `sys_page_size == 0' failed
[root@master Desktop]#
- jobs查看當前終端下得進程
[root@master Desktop]# jobs
[1]+ Running
- 前台后台切換
# 切換到前端 終端又被占用
root@master Desktop]# fg 1
firefox
# 切換到后端
root@master Desktop]# bg 1
Systemd 控制服務啟動,守護進程
- 服務單元的狀態
loaded 服務單元的配置文件已被處理
active (running) 運行中
active(exited) 某些一次性運行的服務已經陳工被執行並退出
active(waiting)服務已經運行,但正在等待某個時間
inactive 沒運行
disabled 開機不運行
enabled 開機運行
static 不能夠被設置為開機啟動
- systemd命令
# 查看服務狀態
systemctl status sshd
# 開啟
systemctl start sshd
# 結束
systemctl stop sshd
# 重啟
systemctl restart sshd
# 是否開機啟動
systemctl is-enabled sshd
# 是否活動
systemctl is-active sshd
# 列出所有的服務單元
systemctl list units
# 列出所有服務單元文件
systemctl list-unit-files
本次課程涉及到的命令
ps -aux
查看進程
注:該命令不加-
也是好用的。
# a顯示所有前台進程,x顯示所有后台進程 u顯示用戶
[root@centos ~]# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 51896 3256 ? Ss Jul02 1:59 /usr/lib/systemd/systemd --system --deserialize 21
root 2 0.0 0.0 0 0 ? S Jul02 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S Jul02 1:54 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< Jul02 0:00 [kworker/0:0H]
root 7 0.0 0.0 0 0 ? S Jul02 0:23 [migration/0]
root 8 0.0 0.0 0 0 ? S Jul02 0:00 [rcu_bh]
root 9 0.0 0.0 0 0 ? S Jul02 6:56 [rcu_sched]
root 10 0.0 0.0 0 0 ? S Jul02 1:31 [watchdog/0]
- 最上一行信息介紹
USER 哪個用戶產生進程
PID 進程號
%CPU 占用cpu資源的百分比
%MEM 占用物理內存的百分比
VSZ 虛擬內存的占用
RSS 占用物理內存的大小單位(kb)
TTY 只以哪一個控制台(終端)運行程序
STAT 狀態
R 表示正在運行
+表示后台進程
S表示睡眠狀態的進程
s包含子進程(父進程停止,子進程也會停止)
T停止狀態的進程(顯示T的可以被喚醒)
START 進程的啟動時間
TIME 進程占了CPU多長時間
COMMAND 這個進程哪個命令來的
ps -le
查看進程
# l顯示詳細信息 e顯示左右進程 f標准格式
[root@centos ~]# ps -efl
F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD
4 S root 1 0 0 80 0 - 12974 ep_pol Jul02 ? 00:01:59 /usr/lib/systemd/systemd --system --deserialize 21
1 S root 2 0 0 80 0 - 0 kthrea Jul02 ? 00:00:00 [kthreadd]
1 S root 3 2 0 80 0 - 0 smpboo Jul02 ? 00:01:54 [ksoftirqd/0]
1 S root 5 2 0 60 -20 - 0 worker Jul02 ? 00:00:00 [kworker/0:0H]
1 S root 7 2 0 -40 - - 0 smpboo Jul02 ? 00:00:23 [migration/0]
top
動態查看進程
top - 17:05:45 up 56 days, 6:46, 1 user, load average: 0.11, 0.05, 0.05
Tasks: 126 total, 1 running, 125 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.2 sy, 0.0 ni, 99.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 5946700 total, 783956 free, 3881272 used, 1281472 buff/cache
KiB Swap: 4194300 total, 4097288 free, 97012 used. 1686820 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
15056 root 20 0 990364 31204 7096 S 0.7 0.5 34:30.57 dockerd-current
12612 root 20 0 0 0 0 S 0.3 0.0 0:00.06 kworker/1:1
- top擁有交互式命令
? 或H 彈出幫助信息
P 以CPU的使用率排序
M 以內存的使用率排
N 以PID的順序排
q退出top
- top 最上層顯示的意義
# 第一行
當前時間 開機時間 幾個用戶 資源使用率:1分鍾,5分鍾,15分鍾
# 第二行
總共任務數 運行的 睡眠的 停止的 僵屍進程
`注:什么是僵屍進程:父進程停止了 子進程沒停止叫做僵屍進程。`
# 第三行
CPU使用率 us用戶占用sy 系統占用的 ni改變過優先級的進程占用的 id空閑的 wa 等待進程 hi 硬件中斷占用的 si 軟中斷占用的 st 有虛擬機是,虛擬占用的
# 第四行
物理內存:總數 使用中 空閑的 緩存區
# 第五行
虛擬內存:總數 使用中 空閑的 緩存區
# 第六行
PID USER PR 優先級 NI nice值 VIRT 申請的虛擬內存 RES常駐內存實際的 SHR 共享的 S 狀態
free
查看內存使用情況
# -h 是以人類能看的懂得樣式輸出
[root@centos ~]# free -h
total used free shared buff/cache available
Mem: 5.7G 3.7G 760M 33M 1.2G 1.6G
Swap: 4.0G 94M 3.9G
pstree
樹狀結構顯示進程
# -p參數是顯示子進程
[root@centos ~]# pstree -p
systemd(1)─┬─NetworkManager(701)─┬─{NetworkManager}(726)
│ └─{NetworkManager}(729)
├─abrt-watch-log(7869)
├─abrtd(7850)
├─atd(7480)
├─auditd(24144)───{auditd}(24153)
├─crond(31725)
├─dbus-daemon(660)
kill
殺掉進程
# -l 查看信號 -1 重啟(讓進程立即關閉,然后重新讀取配置文件)-2 強制結束 (終止,力度輕)-9 強制結束 (強制關閉,用來立即結束程序)
# -15 正常終止 (正常關閉)-18 恢復后台 (可以讓暫停的進程恢復執行,本信號不能被阻斷)-19 前台與運行的放在后台(該信號可以暫停前台進程,相當於CTRL+Z。本新號不能被阻斷)
[root@centos ~]# kill -15 pid