【Linux】Linux常用命令 ,生产环境变慢的查看思路

Linux操作系统

内容管理


Linux常用指令


前路任重道远,唯有先不断的走完全程,再回过头来给捡起掉下的东西

同时还hope给分享一下操作系统,JVM、计算机网络、并发问题的解决方案(底层的多线程技术,JUC包,项目中实际的解决方案、分布式集群,MyCat中间件分库分表,Redis缓存技术、MQ的异步技术…】 Linux过了接下来就是分享并发问题的解决方案了,从Redis开始…

Linux指令

Linux面向文件,所以指令操作十分重要,这里继续介绍指令系统的常见指令

文件系统(续)

之前已经介绍了多个文件系统的指令,比如ls、cd、pwd、echo、mkdir、rmdir、rm、mv、cat、less、more分别就是包含的文件的增删改移看

tail、head [-n] x 查看文件开头或者结尾X行

默认情况下,直接tail文件查看的就是后面10行;head同理,cat、less、more、tail、head都是查看文件

[root@localhost linux]# tail cfeng.txt
hello ereryone, my name is Cfeng, I'm now learning linux
I I believe I can. Fighting!I believe I can. Fighting!
I I believe I can. Fighting!I believe I can. Fighting!
I I believe I can. Fighting!I believe I can. Fighting!
I I believe I can. Fighting!I believe I can. Fighting!
I I believe I can. Fighting!I believe I can. Fighting!
I I believe I can. Fighting!I believe I can. Fighting!
I I believe I can. Fighting!I believe I can. Fighting!
I I believe I can. Fighting!I believe I can. Fighting!
I I believe I can. Fighting!I believe I can. Fighting!
[root@localhost linux]# tail -n 1 cfeng.txt
I I believe I can. Fighting!I believe I can. Fighting!
[root@localhost linux]# head -n 2 cfeng.txt
hello ereryone, my name is Cfeng, I'm now learning linux, and i hope to get many offers the next year. I believe I can. Fighting!
hello ereryone, my name is Cfeng, I'm now learning linux, and i hope to get many offers the next year. I believe I can. Fighting!

> 输出到(覆盖) >> 追加到结尾

这两个指令就类似java中的将指令输出到控制台之外的输出到文件IO流嘛

这里以 > 为例子, 可以将查询的所有的结果比如查看的文件的内容、显示的列表、echo输出的内容都写入文件

ll > ....txt

cat  文件  > ....txt

echo ""  > ....txt

head 文件  > ....txt

追加>>是相似的用法

[root@localhost ~]# ll > cfengStudy/linux/cfeng.txt
[root@localhost ~]# echo "I'm cfeng, I am now study linux instructions, I should do some things" >> cfengStudy/linux/cfeng.txt
[root@localhost ~]# cat cfengStudy/linux/cfeng.txt
总用量 8
-rw-------. 1 root root 2001 4月  19 19:13 anaconda-ks.cfg
drwxr-xr-x. 3 root root   19 4月  19 17:58 cfengStudy
-rw-r--r--. 1 root root 2025 4月  19 19:18 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 4月  19 19:18 公共
drwxr-xr-x. 2 root root    6 4月  19 19:18 模板
drwxr-xr-x. 2 root root    6 4月  19 19:18 视频
drwxr-xr-x. 2 root root    6 4月  19 19:18 图片
drwxr-xr-x. 2 root root    6 4月  19 19:18 文档
drwxr-xr-x. 2 root root    6 4月  19 19:18 下载
drwxr-xr-x. 2 root root    6 4月  19 19:18 音乐
drwxr-xr-x. 2 root root    6 4月  19 19:18 桌面
I'm cfeng, I am now study linux instructions, I should do some things

ln -s [源文件或目录] 软链接名 给源文件创建一个软链接

软链接名 标识软链接 软连接名/ 表示真实的物理路径目录

删除软链接 : rm -rf 软连接名

rm -rf 软连接名/ 删除软连接对应的真实目录的内容呢

创建之后可以通过ll查看,以.l结尾的就是软连接

[root@localhost ~]# ln  -s cfengStudy/linux/cfeng.txt  CfengLink
[root@localhost ~]# ll
总用量 8
-rw-------. 1 root root 2001 4月  19 19:13 anaconda-ks.cfg
lrwxrwxrwx. 1 root root   26 4月  21 13:12 CfengLink -> cfengStudy/linux/cfeng.txt
drwxr-xr-x. 3 root root   19 4月  19 17:58 cfengStudy

[root@localhost ~]# rm -rv CfengLink
rm:是否删除符号链接 "CfengLink"?Y
已删除"CfengLink"
[root@localhost ~]# ll
总用量 8
-rw-------. 1 root root 2001 4月  19 19:13 anaconda-ks.cfg

这里CentOS系统中会将文件夹标注出来,同时会将link给用另外i一种颜色标注

history 查看所有的执行命令

history可以查看之前执行的所有的指令,不随着关机而关机

[root@localhost ~]# history
    1  ifconfig
    2  vim /etc/sysconfig/network-scripts/ifcfg-ens33
    3  ifconfig
    4  service network restart
    5  ifconfig
    6  ls /etc/init.d
    7  ls /usr/lib/systemd/system
    8  systemctl status firewalld
	......

文件权限类

Linux系统是多用户系统,不同的用户具有的权限不同,就类似Windows系统的管理员,对某些系统文件的操作必须要root权限,普通的用户就没有权限

Linux系统文件的属性 的图像结果

[root@localhost ~]# ls -al 
总用量 64
dr-xr-x---. 16 root root 4096 4月  21 14:51 .
dr-xr-xr-x. 17 root root  237 4月  19 17:55 ..
-rw-------.  1 root root 2001 4月  19 19:13 anaconda-ks.cfg
-rw-------.  1 root root 2943 4月  21 15:21 .bash_history
-rw-r--r--.  1 root root   18 12月 29 2013 .bash_logout
-rw-r--r--.  1 root root  176 12月 29 2013 .bash_profile
-rw-r--r--.  1 root root  176 12月 29 2013 .bashrc
drwx------. 16 root root 4096 4月  19 16:42 .cache
drwxr-xr-x.  3 root root   19 4月  19 17:58 cfengStudy

从左到右十个字符, 后面的数字代表的链接数量【文件夹就是子文件夹的个数,文件就是硬链接的个数】

  • 0位表示 类型 : 可以表示目录、文件、链接文件; -代表文件, d代表目录,l代表链接文档
  • 1-3位代表属主【文件的所有者】对于该文件的操作权限,
  • 4-6代表属组【所有者的同组的用户】对于该文件的权限
  • 7-9代表的是其他的一般用户 对于该文件的权限

对于文件 : r读取, w修改,但是不代表可以删除,必须要有写权限才可以删除,x代表可以被系统执行

对于目录: r代表可以读取ls,w嗲表可以修改,目录内部可以增删改,x代表可执行,是否可以进入目录

后面的root root,前面的是属主,后面是数组,当创建用户的时候,如果不指定数组,那么就会默认创建一个同名的数组存放属主

chmod [{ugoa}{±=} {rwx}] 文件或者目录 改变权限

这里使用的就是chmod

  • u: 属主 g : 属组 o: 其他人 a : 所有人 r = 4, w = 2 , x =1
  • 可以用数字表示,比如 chmod 777 …txt 所有的都具有rwx权限,加上-R就是文件夹中的所有的文件都具有
[root@localhost linux]# cp cfeng.txt  ./
cp: "cfeng.txt" 与"./cfeng.txt" 为同一文件
[root@localhost linux]# touch cHuan.txt
[root@localhost linux]# cat cfeng.txt > cHuan.txt
[root@localhost linux]# mv cfeng.txt CFENG.txgt
[root@localhost linux]# ls
CFENG.txgt  cHuan.txt
[root@localhost linux]# mv CFENG.txgt CFENG.txt
[root@localhost linux]# ls
CFENG.txt  cHuan.txt
[root@localhost linux]# ll
总用量 8
-rw-r--r--. 1 root root 670 4月  21 13:08 CFENG.txt
-rw-r--r--. 1 root root 670 4月  22 16:42 cHuan.txt
[root@localhost linux]# chmod u-w cHuan.txt
[root@localhost linux]# ll
总用量 8
-rw-r--r--. 1 root root 670 4月  21 13:08 CFENG.txt
-r--r--r--. 1 root root 670 4月  22 16:42 cHuan.txt

这里就成功删除了属主的w权限,这个文件变成了只读文件,因为这是root用户,当进行vim修改的时候,会给出警告,只读文件,并且当插入的时候,会⚠readonly,需要强制使用!执行呢

chown [-R] [用户名] [文件或者目录] 改变属主

-R表示递归操作,文件权限类的操作都是使用的R来代表递归,而常规文件操作中,使用的是r代表的递归操作

最终用户可以使用 group:user 的方式来指定最终用户

[root@localhost linux]# ll
总用量 8
-rw-r--r--. 1 root root 670 4月  21 13:08 CFENG.txt
-r--r--r--. 1 root root 670 4月  22 16:42 cHuan.txt
[root@localhost linux]# tail -n 4 /etc/passwd
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
cfeng:x:1000:1000:cfeng:/home/cfeng:/bin/bash
[root@localhost linux]# chown cfeng CFENG.txt 
[root@localhost linux]# ll
总用量 8
-rw-r--r--. 1 cfeng root 670 4月  21 13:08 CFENG.txt
-r--r--r--. 1 root  root 670 4月  22 16:42 cHuan.txt
[root@localhost linux]# su cfeng
[cfeng@localhost linux]$ 

chgrp [最终用户组] [文件或目录]

修改文件属组,上面的chown是修改的属主,chgrp change group就是修改属组

[cfeng@localhost linux]$ ll
总用量 8
-rw-r--r--. 1 cfeng root 670 4月  21 13:08 CFENG.txt
-r--r--r--. 1 root  root 670 4月  22 16:42 cHuan.txt
[cfeng@localhost linux]$ tail -n 1 /etc/group
cfeng:x:1000:cfeng
[cfeng@localhost linux]$ chgrp cfeng CFENG.txt 
[cfeng@localhost linux]$ ll
总用量 8
-rw-r--r--. 1 cfeng cfeng 670 4月  21 13:08 CFENG.txt
-r--r--r--. 1 root  root  670 4月  22 16:42 cHuan.txt

搜索查找类

find [搜索范围] [选项] 查找文件或者目录

find指令是从指定的目录向下递归遍历各个子目录,将满足条件的目录显示在终端

  • -name <查询方式》 按照文件名查找模式查找文件
  • -user<用户名> 查找属于指定用户的所有文件
  • -size < 文件大小> 查找指定的文件大小查找, b【block】 — 块512字节 c ---- 字节 w【word 2字节】 k千字节 M 兆字节 G G字节 +代表大于,-代表小于,不写代表等于
查找主目录下面的 名为linux的目录
    
查找主目录下面 属主为cfeng的文件或目录
    
查找CfengStudy下面小于3字节的文件  
    
[root@localhost linux]# ll
总用量 12
-rw-r--r--. 1 cfeng cfeng 1440 422 17:15 CFENG.txt
-r--r--r--. 1 root  root   670 422 16:42 cHuan.txt
-rw-r--r--. 1 root  root     2 422 17:18 TEST.txt
[root@localhost linux]# cd ~
[root@localhost ~]# find ~  -name "TEST.txt"
/root/cfengStudy/linux/TEST.txt
[root@localhost ~]# find ~ -user cfeng
/root/cfengStudy/linux/CFENG.txt
[root@localhost ~]# find ~ -size -3

locate 文件 【先updatedb】 快速定位文件路径

locate指令利用实现建立好的locate数据库实现快速定位,Locate指令不需要遍历所有的目录,查询速度快,类似于Mysql的B树,第一次运行前一定要建立数据库,所以要随时更新数据库updatedb

[root@localhost ~]# updatedb
[root@localhost ~]# locate CFENG.txt
/root/cfengStudy/linux/CFENG.txt


------> 这里updatedb是需要时间的,但是查询locate就非常迅速,比上面的find 范围  -name快多了

grep [-n] 查找内容 源文件 过滤查找 【| 前一个命令的处理结果输出给后面的指令】

首先管道符==|的作用类似于之前的>>但是这是将内容交给后面的指令执行, 比如ls | grep -n test==

  • -n 显示匹配行和行号
通常用于list -l列表中,查找匹配的文件在第几行
-rw-------. 1 root root 2001 4月  19 19:13 anaconda-ks.cfg
drwxr-xr-x. 3 root root   19 4月  19 17:58 cfengStudy
-rw-r--r--. 1 root root 2025 4月  19 19:18 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 4月  19 19:18 公共
drwxr-xr-x. 2 root root    6 4月  19 19:18 模板
drwxr-xr-x. 2 root root    6 4月  19 19:18 视频
drwxr-xr-x. 2 root root    6 4月  19 19:18 图片
drwxr-xr-x. 2 root root    6 4月  19 19:18 文档
drwxr-xr-x. 2 root root    6 4月  19 19:18 下载
drwxr-xr-x. 2 root root    6 4月  19 19:18 音乐
drwxr-xr-x. 2 root root    6 4月  19 19:18 桌面
[root@localhost ~]# ll | grep -n cfengStudy
3:drwxr-xr-x. 3 root root   19 4月  19 17:58 cfengStudy

可以看到前面显示了行号,后面的3是链接数

压缩解压缩打包类

gizp/gunzip [文件] 压缩或者解压缩.gz文件 只能压缩文件

该类型命令只能压缩文件,所以使用的比较少,gunzip就是解压,gzip就是压缩

  • 只能压缩文件,不能压缩目录
  • 不保留原来的文件,同时压缩多个文件会产生多个压缩包

可以看出来限制很大,只能压缩文件,并且源文件还会删除,并且不能将多个文件压缩在一起

-rw-r--r--. 1 cfeng cfeng 1440 422 17:15 CFENG.txt
-r--r--r--. 1 root  root   670 422 16:42 cHuan.txt
-rw-r--r--. 1 root  root     2 422 17:18 TEST.txt
[root@localhost linux]# gzip TEST.txt 
[root@localhost linux]# ll
总用量 12
-rw-r--r--. 1 cfeng cfeng 1440 422 17:15 CFENG.txt
-r--r--r--. 1 root  root   670 422 16:42 cHuan.txt
-rw-r--r--. 1 root  root    31 422 17:18 TEST.txt.gz
[root@localhost linux]# gunzip TEST.txt.gz 
[root@localhost linux]# ls
CFENG.txt  cHuan.txt  TEST.txt

并且可以看到压缩和解压后的文件都在原理啊的文件所在的目录

zip [-r] XXX.zip 压缩的文件或目录 unzip [-d] 解压缩内容

zip/unzip和上面的有一些区别,那就是zip是可以压缩目录的,加上-r就可以递归压缩所有的文件,同时解压的时候也可以指定解压缩的目录,不像上面只能放在原来的目录之下【zip在linux/windows中都是可以使用的】

zip操作需要指定压缩后的名称为XXX.zip

  • -r 压缩目录
  • -d <目录> 指定解压后文件的存放目录里
[root@localhost linux]# zip Test.zip TEST.txt 
  adding: TEST.txt (stored 0%)
[root@localhost linux]# ll
总用量 16
-rw-r--r--. 1 cfeng cfeng 1440 4月  22 17:15 CFENG.txt
-r--r--r--. 1 root  root   670 4月  22 16:42 cHuan.txt
-rw-r--r--. 1 root  root     2 4月  22 17:18 TEST.txt
-rw-r--r--. 1 root  root   168 4月  23 16:12 Test.zip
[root@localhost linux]# unzip Test.zip -d ../
Archive:  Test.zip
 extracting: ../TEST.txt             
[root@localhost linux]# cd ..
[root@localhost cfengStudy]# ll
总用量 4
drwxr-xr-x. 2 root root 72 4月  23 16:12 linux
-rw-r--r--. 1 root root  2 4月  22 17:18 TEST.txt

tar [选项] XX.tar.gz 将要打包的内容 打包目录,如果压缩就是.gz

这里的选项有很多的类型【XX笔试连续考了两次】

  • -c 产生.tar打包文件 create
  • -v 显示详细信息 view
  • -f 指定压缩后的文件名 file
  • -z 打包的同时会进行压缩
  • -x 解包.tar文件
  • -C 解压到指定的目录
tar -zcvf XXX.tar.gz  XXXX  XXX XXX    打包压缩多个文件指定打包文件名,同时显示详细信息

tar  -zxvf   xxxx  -C  /....     解压缩到指定的目录,c是打包,x是解包


[root@localhost linux]# rm Test.zip 
rm:是否删除普通文件 "Test.zip"?Y
[root@localhost linux]# ll
总用量 12
-rw-r--r--. 1 cfeng cfeng 1440 4月  22 17:15 CFENG.txt
-r--r--r--. 1 root  root   670 4月  22 16:42 cHuan.txt
-rw-r--r--. 1 root  root     2 4月  22 17:18 TEST.txt
[root@localhost linux]# tar -zcvf  Test.tar.gz  CFENG.txt cHuan.txt  TEST.txt 
CFENG.txt
cHuan.txt
TEST.txt
[root@localhost linux]# ll
总用量 16
-rw-r--r--. 1 cfeng cfeng 1440 4月  22 17:15 CFENG.txt
-r--r--r--. 1 root  root   670 4月  22 16:42 cHuan.txt
-rw-r--r--. 1 root  root   467 4月  23 16:23 Test.tar.gz
-rw-r--r--. 1 root  root     2 4月  22 17:18 TEST.txt
[root@localhost linux]# tar -zxvf Test.tar.gz  -C ../
CFENG.txt
cHuan.txt
TEST.txt
[root@localhost linux]# cd ..
[root@localhost cfengStudy]# ll
总用量 12
-rw-r--r--. 1 cfeng cfeng 1440 4月  22 17:15 CFENG.txt
-r--r--r--. 1 root  root   670 4月  22 16:42 cHuan.txt
drwxr-xr-x. 2 root  root    75 4月  23 16:23 linux
-rw-r--r--. 1 root  root     2 4月  22 17:18 TEST.txt

指令要经常使用才可以,不使用就会忘记,比如rmdir删除空文件夹,rm就是删除一般的

时间日期类

时间日期类型的指令都是比较简单的,就不多赘述了

date [-d/-s] <+日期时间格式>/ date -d + “discription”

可以直接date就可以显示当前时间;或者加上描述显示非当前时间,就没有什么比较特别的地方

[root@localhost ~]# date -d "1 days ago"
2022年 04月 20日 星期三 14:07:18 CST
[root@localhost ~]# date -d "-a days ago"
2022年 04月 20日 星期三 23:07:40 CST
[root@localhost ~]# date -d "-1 days ago"
2022年 04月 22日 星期五 14:07:58 CST
[root@localhost ~]# date 
2022年 04月 21日 星期四 14:08:08 CST

-d 是显示系统的时间

  • -s set 设置系统当前的时间,比如
[]# date -s "2022-04-15 20:23:23"   系统时间如果正确就可以不需要修改

cal [某一年] 显示日历

cal — calendar 日历

如果不加上选项就是显示当前月份的日历,如果是加上具体的年份,那么就是显示指定年份的日期

[root@localhost ~]# cal 
      四月 2022     
日 一 二 三 四 五 六
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

[root@localhost ~]# cal 2002
                               2002                               

        一月                   二月                   三月        
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
       1  2  3  4  5                   1  2                   1  2
 6  7  8  9 10 11 12    3  4  5  6  7  8  9    3  4  5  6  7  8  9
13 14 15 16 17 18 19   10 11 12 13 14 15 16   10 11 12 13 14 15 16
20 21 22 23 24 25 26   17 18 19 20 21 22 23   17 18 19 20 21 22 23
27 28 29 30 31         24 25 26 27 28         24 25 26 27 28 29 30
                   

用户类

因为Centos7操作系统默认就是多用户系统,所以需要通过指令来管理用户之间的相关【该类型的指令也没什么特别的地方,所以集中演示】

这里可以统一先说一下, 用户的增useradd,passwd ,删userdel,查id ,查当前woami,改usermod,切换su,修改权限sudo

useradd [-g] groupname name 添加新用户

如果用户不是很多,比如小的项目,那么新建用户之后就可以不需要分组,直接新建即可

useradd 用户名

对于大型项目,可能需要注册很多的用户,那么就需要进行分组,那么就使用-g参数来确定分组

useradd -g gName uName

passwd 用户名 为用户设置密码

这个就很简单了,就是创建用户之后,是没有密码的,那么就使用

passwd 用户名 ------- 设置密码

id 用户名 查看用户是否存在

新建用户之后,可以通过id 用户名来查看用户是否存在,不存在就说明没有创建成功

/etc/passwd 存放用户

Linux是面向文件的,所以所有的用户信息是放在文件中进行管理,既然是配置信息,所以在/etc文件夹下面,passwd文件夹中就是用户的信息,所以这里可以查看这个文件,查看文件可以使用vim,或者之前的文件系统的指令cat、more、less等,甚至用man都可以

su [-] 用户名 切换用户

su ------ switch user 切换用户

不加- : 切换用户的执行权限,不能获得环境变量

加- : 切换用户,并且可以获得该用户的环境变量

userdel [-r] 用户名 删除用户

  • -r 删除用户,并且会删除所有的文件夹,包括主目录
  • userdel user 删除用户但是会保存用户的主目录

whoami 自身用户名称 wo am i 登录用户及时间

这里就是区分分开的时候是显示的当前登录用户的用户名和时间; 连接在一起是自身用户名称

sudo 设置普通用户具有root权限 先/etc/sudoers设置

sudoers中就是具有root权限的用户的信息,所以创建用户之后,可以

vim /etc/sudoers 进入添加新增用户

cfeng  ALL=(ALL)  ALL   //使用sudo命令需要密码,和root同
cfeng ALL=(ALL)  NOPASSWORD:ALL   //不需要密码

这样子设置之后然后就可以执行root的指令,当需要使用的时候加上sudo即可使用

usermod [-g] gName 用户 修改用户

-g : 修改用户的初始登录的组,给定的组要存在,默认id为1

[root@localhost ~]# vim /etc/passwd
[root@localhost ~]# useradd Chuan
[root@localhost ~]# passwd Chuan
更改用户 Chuan 的密码 。
新的 密码:
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@localhost ~]# tail -n 3 /etc/passwd
tcpdump:x:72:72::/:/sbin/nologin
cfeng:x:1000:1000:cfeng:/home/cfeng:/bin/bash
Chuan:x:1001:1001::/home/Chuan:/bin/bash
[root@localhost ~]# su Chuan
[Chuan@localhost root]$ su root
密码:
[root@localhost ~]# userdel -r Chuan
userdel: user Chuan is currently used by process 3760
[root@localhost ~]# kill -9 3760
已杀死
[root@localhost ~]# id Chuan
id: Chuan: no such user

[root@localhost ~]# su Cfeng
su: user Cfeng does not exist
[root@localhost ~]# su cfeng
[cfeng@localhost root]$ who am i
root     pts/0        2022-04-21 14:46 (:0)
[cfeng@localhost root]$ whoami
cfeng

每一个用户有一个用户组,系统可以对用户组中的所有的用户进行集中管理,有的系统: 用户属于与其同名的用户组,对用户组的操作起始就是操作文件**/etc/group**

groupadd 组名 新增一个组

通过组就更加方便管理用户,新增一个组就直接加上组名即可

groupdel 组名 删除一个组

组的增删改和用户的增删改类似,只是这里对组的删除没有-r,直接删除即可

groupmod -n 新组名 旧组名

这里注意选项

  • -n< newGroup > 指定工作组的新组名 【这里的新组名是放在前面的】

所以想要查看有什么组就查看/etc/group文件即可

[root@localhost ~]# tail -n 5 /etc/group
stapusr:x:156:
stapsys:x:157:
stapdev:x:158:
tcpdump:x:72:
cfeng:x:1000:cfeng
[root@localhost ~]# groupadd CHuan
[root@localhost ~]# tail -n 5 /etc/group
stapsys:x:157:
stapdev:x:158:
tcpdump:x:72:
cfeng:x:1000:cfeng
CHuan:x:1001:

用户组的操作比用户操作简单,只是需要说明的是,当创建用户,没有指定组的时候,系统会创建一个同名的组

磁盘管理类

du [选项] 目录或文件 查看文件或者目录占用的磁盘空间

du: disk usage 磁盘使用情况

du 目录 就可以显示目录下面每一个子目录的磁盘使用情况

  • -h : 使用容易阅读的GBytes、MyBytes、KBytes等格式自行显示
  • -a : all 查看不仅仅是目录,还包括文件的占用的空间
  • -c : count 显示所有的大小和,并且会求总和
  • -s: sum 只显示总和
  • –max-depth = n 指定统计的目录的深度
[root@localhost /]# du --max-depth=1 -h 
140M	./boot
0	./dev
du: 无法访问"./proc/3389/task/3389/fd/3": 没有那个文件或目录
du: 无法访问"./proc/3389/task/3389/fdinfo/3": 没有那个文件或目录
du: 无法访问"./proc/3389/fd/4": 没有那个文件或目录
du: 无法访问"./proc/3389/fdinfo/4": 没有那个文件或目录
0	./proc
4.5G	./run
0	./sys
43M	./etc

df [-h]

df: disk free 空余磁盘 -h就是使用容易阅读的Kbytes、M、G表示

会整体显示磁盘的使用量,检查文件系统的磁盘占用情况

[root@localhost /]# df -h 
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 1.6G     0  1.6G    0% /dev
tmpfs                    1.6G     0  1.6G    0% /dev/shm
tmpfs                    1.6G   13M  1.6G    1% /run
tmpfs                    1.6G     0  1.6G    0% /sys/fs/cgroup
/dev/mapper/centos-root   35G  4.9G   31G   14% /
/dev/sda1               1014M  172M  843M   17% /boot
tmpfs                    318M   24K  318M    1% /run/user/0
/dev/sr0                 4.4G  4.4G     0  100% /run/media/root/CentOS 7 x86_64

lsblk -f

查看设备的挂载情况【一般情况下都是没有可视化的】,可以查看系统的设备挂载,ls — list

[root@localhost /]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   40G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   39G  0 part 
  ├─centos-root 253:0    0   35G  0 lvm  /
  └─centos-swap 253:1    0    4G  0 lvm  [SWAP]
sr0              11:0    1  4.4G  0 rom  /run/media/root/CentOS 7 x86_64

可以看到SWAP、/boot、/;这就是最初创建虚拟机的时候分配的大小

mount [-t vfstype] [-o options] device dir /unmount 挂载点 挂载、卸载

LInux是面向文件的,目录文件树的根结点就是root结点,/,是一个独立唯一的文件结构,Linux每一个分区组成整个文件系统的部分,可以使用挂载的处理方法,将分区和目录练习起来

要挂载在虚拟机创建的时候就要有光驱,之前图形化进行分区,其实也可以使用指令来进行操作

  • -t vfstype: 指定文件系统的类型,mount一般都可以自动选择合适的类型,比如光盘选择iso,文件系统vfat等
  • -o options 描述挂载方式,比如rw 读写方式等【 这里感觉有点boring,康康情况再深入】
  • device 要挂载的设备
  • dir 设备的挂载点【文件】

开机自动挂载可以再配置文件中设置,配置文件是**/etc/fstab**

fdisk [-l] 磁盘设备名 root用户可用 查看磁盘的分区

fdisk -l : 查看磁盘的分区详情 find disk

fdisk 磁盘设备名: 对新增的磁盘进行分区的操作

Linux分区

Device : 分区的序列, Boot: 引导 Start: X磁柱开始 End: Y磁柱开始 Blocks:容量 Id:分区类型

m: 显示列表 p: 当前分区 n: 新增 w: 写入退出 q: 不保存直接退出

上面的命令和vim编辑的相关命令有些类似

[root@localhost ~]# fdisk -l

磁盘 /dev/sda:42.9 GB, 42949672960 字节,83886080 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理)512 字节 / 512 字节
I/O 大小(最小/最佳)512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000a84bf

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    83886079    40893440   8e  Linux LVM

磁盘 /dev/mapper/centos-root:37.6 GB, 37576769536 字节,73392128 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理)512 字节 / 512 字节
I/O 大小(最小/最佳)512 字节 / 512 字节


磁盘 /dev/mapper/centos-swap:4294 MB, 4294967296 字节,8388608 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理)512 字节 / 512 字节
I/O 大小(最小/最佳)512 字节 / 512 字节

进程管理类

上面的磁盘管理类的主要命令都是查看的,比如du、df查看消耗和剩余,fdisk 查看磁盘分区 lsblk 显示磁盘的分区 mount和unmount进行挂载

而对于进程的管理要注意一下,进程和线程是操作系统中最基本的东西,后面博主会补充详细的专业知识的

ps aux | grep XXX ps -ef | grep XXX 查看进程状态

ps aux | grep xxx 查看系统中的所有的进行

ps -ef | grep xxx 查看子父进程之间的关系

  • a 所有用户的进程【带有终端】
  • x 当前用户的所有进程,包括没有终端的
  • u 面向用户【和上面的-h一样友好】
  • -e 列出所有进程
  • -u 关联某个用户的所有进程
  • -f 完整格式的进程列表

之前的时候区分过服务和进程,服务常驻内存,控制服务的命令是systemctr

查看进程信息显示的

aux 查看 PID : processId 进程的ID号码, %CPU 该进程占用CPU资源的百分比,越高越耗费资源, %MEM : 占用物理内存的百分比,越高越费

-ef查看 PPID : parent PID 父进程I

所以一般查看进程占用率,就使用aux,查看父进程就使用-ef

[root@localhost ~]# ps aux | grep network
root       6707  0.0  0.0 112824   980 pts/1    S+   17:26   0:00 grep --color=auto network

//第二列就是PID 后面依次就是%CPU 和%MEM

[root@localhost ~]# ps -ef | grep network
root       6783   4921  0 17:27 pts/1    00:00:00 grep --color=auto network

kill [-9] PID killall 进程名称 终止进程

这里的终止进程-9代表强迫进程立即停止,类似于之前的8080端口占用的时候Windows中的操作,都是杀死进程即可【kill 是PID,killall 是按照名称进程匹配】

//浏览器进程杀死
[root@localhost ~]# ps aux | grep fire-fox
root       7545  0.0  0.0 112824   980 pts/1    S+   17:35   0:00 grep --color=auto fire-fox
[root@localhost ~]# killall fire-fox
fire-fox: no process found
[root@localhost ~]# kill -9 7545
bash: kill: (7545) - 没有那个进程

ptree [-u/-p]

  • -p 显示进程的id
  • -u 显示进程的所属用户

整个也会显示所有的进程,通过-p显示id,-u显示用户

[root@localhost ~]# pstree -p
systemd(1)─┬─ModemManager(815)─┬─{ModemManager}(829)
           │                   └─{ModemManager}(848)
           ├─NetworkManager(973)─┬─{NetworkManager}(979)
           │                     └─{NetworkManager}(982)
           ├─VGAuthService(812)
           ├─abrt-watch-log(858)
           ├─abrt-watch-log(868)
           ├─abrtd(855)

可以看出来就是完整显示了一颗进程树,包含父子关系,进程名称和其ID,通过-u就可以显示所属用户

top [-d/-i/-p] 实时监控系统进程状态

  • -d 秒数 ,指定top指令更新时间,默认是3s后执行
  • -i 不显示闲置或者僵化进程
  • -p 通过指定process ID 来监控某个进程状态

操作 : P 以CPU使用率排序【默认】, M 内存使用率排序 N 以PID排序 q 退出top监控

查询的结果: 第一列任务队列,第二列是进程信息;第三为CPU,第四为物理内存,第五swap信息

netstat-anp | grep 进程/端口号 查看进程网络信息,查看端口占用

这个类似之前windows中的【netstat -pid | findstr 8080】

netstat -nlp | grep port 查看网络端口号的占用情况

  • -a 显示所有的套接字socket
  • -n 拒绝显示别名,全部为数字 no
  • -l 仅仅列出正在监听的服务的转台
  • -p 显示哪个进程在调用
[root@localhost ~]# netstat -nlp | grep 232
unix  2      [ ACC ]     STREAM     LISTENING     32321    1507/master          private/error
unix  2      [ ACC ]     STREAM     LISTENING     32324    1507/master          private/retry
unix  2      [ ACC ]     STREAM     LISTENING     32327    1507/master          private/discard

这里就可以看到端口号的占用情况

crontab [-e/-l/-r] 定时任务设置 /tmp/crontab

系统定时任务的服务,首先就是要重新启动,restart即可

systemctl restart crond
  • -e : edit 编辑定时任务
  • -l : 查询crontab任务
  • -r: 删除当前用户的所有的任务

进入任务编辑页面,就会打开vim编辑任务

*****  
第一个* 一个小时的第几分钟 0-59
第二个: 一天中的第几个小时 0-23
第三: 一个月中的第几天 1-31
第四: 一年中的第几月  1-12
第五: 一周中的星期几 0-7


----统配
*   任何时间
,  不连续时间       0 6,12,18 * * *  每天的6,12,18时0分
-   连续时间        0 6-7 * * *  每天6,7 0分
*/n   间隔多久执行一次   */10 * * * *   每隔十分钟执行一次


比如:
45 22 * * * 每天的22:45执行命令

这里可以来一个操作

间隔一分钟,向cfengStudy/linux/TEST.txt中写入一句话

systemctl restart crond

需要注意的是,定时任务的指令,就需要加上路径/bin,系统才会去/bin下面找到指令,比如这里就echo
*/1 * * * * /bin/echo "I am Cfeng" >> ~/cfengStudy/linux/TEST.txt

去打开文件

1
I am Cfeng

说明已经在开始写入了

还有的就是RPM安装软件包,克隆虚拟机等easy问题就不解释了,注意是掌握上面的指令呢

生产环境服务器变慢,诊断思路和性能评估?

变慢的思路的诊断思路: 总 —> 分

  • 首先就是top查看整机的状态【直接输入top可以看到cpu占用率的进程】
  • 然后就是诊断CPU, 使用命令vmstat
  • 查看内存的使用,使用free
  • 硬盘的占用情况,df disk free 空余的磁盘
  • 磁盘的io情况: iostat
  • 网络的io情况 ifstat
[root@localhost ~]# vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 453536    808 1807764    0    0    13     1   20   29  0  0 100  0  0
[root@localhost ~]# iostat
Linux 3.10.0-1160.el7.x86_64 (localhost.localdomain) 	2022年04月23日 	_x86_64_(8 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.08    0.00    0.09    0.00    0.00   99.82

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               1.75        99.72         7.32    1611527     118286
scd0              0.02         0.11         0.00       1716          0
dm-0              1.74        98.94         7.19    1598955     116167
dm-1              0.01         0.14         0.00       2208          0

[root@localhost ~]# free
              total        used        free      shared  buff/cache   available
Mem:        3247916      985908      453364       29772     1808644     1948604
Swap:       4194300           0     4194300
[root@localhost ~]# ifstat
#kernel
Interface        RX Pkts/Rate    TX Pkts/Rate    RX Data/Rate    TX Data/Rate  
                 RX Errs/Drop    TX Errs/Drop    RX Over/Rate    TX Coll/Rate  
lo                    68 0            68 0          5920 0          5920 0      
                       0 0             0 0             0 0             0 0      
ens33               8644 0          2959 0         2330K 0        393871 0      
                       0 0             0 0             0 0             0 0      
virbr0                 0 0             0 0             0 0             0 0      
                       0 0             0 0             0 0             0 0      
[root@localhost ~]# df
文件系统                   1K-块    已用     可用 已用% 挂载点
devtmpfs                 1606888       0  1606888    0% /dev
tmpfs                    1623956       0  1623956    0% /dev/shm
tmpfs                    1623956   12392  1611564    1% /run
tmpfs                    1623956       0  1623956    0% /sys/fs/cgroup
/dev/mapper/centos-root 36678148 5080672 31597476   14% /
/dev/sda1                1038336  175616   862720   17% /boot
tmpfs                     324792      24   324768    1% /run/user/0
/dev/sr0                 4600876 4600876        0  100% /run/media/root/CentOS 7

细化一下: 生产环境的CPU占用过高的分析思路

和Windows类似,首先就是定位哪个进程占用CPU最高,然后进一步分析定位,使用命令查看是进程中的哪一个线程的问题,然后解决即可,

  • 先用top命令找到占用内存最高的进程; top
  • 之后查看占用率最高的线程, ps -ef | grep XX
  • ps -mp XXX 查看进程中的线程信息
  • 线程ID转换为16进制
  • jstack 进程id | grep tid线程id

这样子就可以定位到资源占用最高的位置并进行相关的处理了🎉, 关于Shell编程后面操作系统review的时候再分析啦

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值