当前位置:主页 > 查看内容

Linux常用命令 & 实用命令总结

发布时间:2021-09-09 00:00| 位朋友查看

简介:Linux其实是前后端都必会的技能,前端目前很多童鞋都再走大前端路线,后端就更不用说了,小编给大家盘了上万字(由于字数限制本文已删减)关于Linux的实战基础命令,可以说这些都是必须掌握的,当然这些也是不够的,后面小编还会继续更新,这波可以收藏起来了……

Linux其实是前后端都必会的技能,前端目前很多童鞋都再走大前端路线,后端就更不用说了,小编给大家盘了上万字(由于字数限制本文已删减)关于Linux的实战基础命令,可以说这些都是必须掌握的,当然这些也是不够的,后面小编还会继续更新,这波可以收藏起来了。

ls

最高使用频率的命令之一。

命令格式: ls [OPTION]... [FILE]...单纯的输入:

  1. [root@iz2ze76ybn73dvwmdij06zz /]# ls 
  2. bin   dev  home  lib64       media  opt    proc  run   srv  tmp  var 
  3. boot  etc  lib   lost+found  mnt    patch  root  sbin  sys  usr  www 

加 option -l 它展示了权限,属主,属组,大小,时间,文件名称

  1. [root@iz2ze76ybn73dvwmdij06zz /]# ls -l 
  2. 总用量 68 
  3. lrwxrwxrwx    1 root root     7 8月  26 22:36 bin -> usr/bin 
  4. dr-xr-xr-x.   5 root root  4096 8月  26 22:39 boot 
  5. drwxr-xr-x   19 root root  2960 4月  11 12:59 dev 
  6. drwxr-xr-x.  81 root root  4096 9月   5 17:09 etc 
  7. drwxr-xr-x.   2 root root  4096 4月  11 12:59 home 
  8. lrwxrwxrwx    1 root root     7 8月  26 22:36 lib -> usr/lib 
  9. lrwxrwxrwx    1 root root     9 8月  26 22:36 lib64 -> usr/lib64 
  10. drwx------.   2 root root 16384 10月 15 2017 lost+found 
  11. drwxr-xr-x.   2 root root  4096 4月  11 12:59 media 
  12. drwxr-xr-x.   2 root root  4096 4月  11 12:59 mnt 
  13. drwxr-xr-x.   3 root root  4096 8月  26 22:40 opt 
  14. drwxr-xr-x    2 root root  4096 8月  26 23:00 patch 
  15. dr-xr-xr-x  110 root root     0 4月  11 12:59 proc 
  16. dr-xr-x---.  12 root root  4096 9月  11 20:59 root 
  17. drwxr-xr-x   26 root root   760 9月   7 21:50 run 
  18. lrwxrwxrwx    1 root root     8 8月  26 22:36 sbin -> usr/sbin 
  19. drwxr-xr-x.   2 root root  4096 4月  11 12:59 srv 
  20. dr-xr-xr-x   13 root root     0 4月  11 12:59 sys 
  21. drwxrwxrwt.   9 root root  4096 9月  15 03:19 tmp 
  22. drwxr-xr-x.  13 root root  4096 8月  26 22:36 usr 
  23. drwxr-xr-x.  19 root root  4096 8月  26 22:55 var 
  24. drwxr-xr-x    6 root root  4096 8月  26 22:39 www 

ls -a (all) linux '.'(点)开头的文件默认是隐藏的,-a可以查询所有的文件。

  1. [root@iz2ze76ybn73dvwmdij06zz /]# ls -a 
  2. .             bin   etc   lib64       mnt    .pearrc  run   sys    usr 
  3. ..            boot  home  lost+found  opt    proc     sbin  .test  var 
  4. .autorelabel  dev   lib   media       patch  root     srv   tmp    www 

ls -F 以"/"结尾的方式展示文件夹。

  1. [root@iz2ze76ybn73dvwmdij06zz /]# ls -F 
  2. bin@   dev/  home/  lib64@       media/  opt/    proc/  run/   srv/  tmp/  var/ 
  3. boot/  etc/  lib@   lost+found/  mnt/    patch/  root/  sbin@  sys/  usr/  www/ 

ls -r 倒叙展示,默认的顺序是(a,b,c,d)正序这样展示

  1. [root@iz2ze76ybn73dvwmdij06zz /]# ls -r 
  2. www  usr  sys  sbin  root  patch  mnt    lost+found  lib   etc  boot 
  3. var  tmp  srv  run   proc  opt    media  lib64       home  dev  bin 

ls -R ()递归展示子目录

  1. [root@iz2ze76ybn73dvwmdij06zz test]# ls -R 
  2. .: 
  3. data 
  4.  
  5. ./data: 
  6. img 
  7.  
  8. ./data/img: 
  9. car  product  user 
  10.  
  11. ./data/img/car: 
  12.  
  13. ./data/img/product: 
  14.  
  15. ./data/img/user

ls -lS 按照文件大小降序显示

  1. [root@iz2ze76ybn73dvwmdij06zz /]# ls -lS 
  2. 总用量 72 
  3. drwx------.   2 root root 16384 10月 15 2017 lost+found 
  4. dr-xr-xr-x.   5 root root  4096 8月  26 22:39 boot 
  5. drwxr-xr-x.  81 root root  4096 9月   5 17:09 etc 
  6. drwxr-xr-x.   2 root root  4096 4月  11 12:59 home 
  7. drwxr-xr-x.   2 root root  4096 4月  11 12:59 media 
  8. drwxr-xr-x.   2 root root  4096 4月  11 12:59 mnt 
  9. drwxr-xr-x.   3 root root  4096 8月  26 22:40 opt 
  10. drwxr-xr-x    2 root root  4096 8月  26 23:00 patch 
  11. dr-xr-x---.  12 root root  4096 9月  15 22:03 root 
  12. drwxr-xr-x.   2 root root  4096 4月  11 12:59 srv 
  13. drwxr-xr-x    3 root root  4096 9月  15 22:10 test 
  14. drwxrwxrwt.   9 root root  4096 9月  15 03:19 tmp 
  15. drwxr-xr-x.  13 root root  4096 8月  26 22:36 usr 
  16. drwxr-xr-x.  19 root root  4096 8月  26 22:55 var 
  17. drwxr-xr-x    6 root root  4096 8月  26 22:39 www 
  18. drwxr-xr-x   19 root root  2960 4月  11 12:59 dev 
  19. drwxr-xr-x   26 root root   760 9月   7 21:50 run 
  20. lrwxrwxrwx    1 root root     9 8月  26 22:36 lib64 -> usr/lib64 
  21. lrwxrwxrwx    1 root root     8 8月  26 22:36 sbin -> usr/sbin 
  22. lrwxrwxrwx    1 root root     7 8月  26 22:36 bin -> usr/bin 
  23. lrwxrwxrwx    1 root root     7 8月  26 22:36 lib -> usr/lib 
  24. dr-xr-xr-x  110 root root     0 4月  11 12:59 proc 
  25. dr-xr-xr-x   13 root root     0 4月  11 12:59 sys 

ls -g 不输出所有者(属主)信息

  1. [root@iz2ze76ybn73dvwmdij06zz /]# ls -g 
  2. 总用量 72 
  3. lrwxrwxrwx    1 root     7 8月  26 22:36 bin -> usr/bin 
  4. dr-xr-xr-x.   5 root  4096 8月  26 22:39 boot 
  5. -rw-r--r--    1 root     0 9月  15 22:20 c 
  6. drwxr-xr-x   19 root  2960 4月  11 12:59 dev 
  7. drwxr-xr-x.  81 root  4096 9月   5 17:09 etc 
  8. drwxr-xr-x.   2 root  4096 4月  11 12:59 home 
  9. lrwxrwxrwx    1 root     7 8月  26 22:36 lib -> usr/lib 
  10. lrwxrwxrwx    1 root     9 8月  26 22:36 lib64 -> usr/lib64 
  11. drwx------.   2 root 16384 10月 15 2017 lost+found 
  12. drwxr-xr-x.   2 root  4096 4月  11 12:59 media 
  13. drwxr-xr-x.   2 root  4096 4月  11 12:59 mnt 
  14. drwxr-xr-x.   3 root  4096 8月  26 22:40 opt 
  15. drwxr-xr-x    2 root  4096 8月  26 23:00 patch 
  16. dr-xr-xr-x  110 root     0 4月  11 12:59 proc 
  17. dr-xr-x---.  12 root  4096 9月  15 22:18 root 
  18. drwxr-xr-x   26 root   760 9月   7 21:50 run 
  19. lrwxrwxrwx    1 root     8 8月  26 22:36 sbin -> usr/sbin 
  20. drwxr-xr-x.   2 root  4096 4月  11 12:59 srv 
  21. dr-xr-xr-x   13 root     0 4月  11 12:59 sys 
  22. drwxr-xr-x    3 root  4096 9月  15 22:18 test 
  23. drwxrwxrwt.   9 root  4096 9月  15 03:19 tmp 
  24. drwxr-xr-x.  13 root  4096 8月  26 22:36 usr 
  25. drwxr-xr-x.  19 root  4096 8月  26 22:55 var 
  26. drwxr-xr-x    6 root  4096 8月  26 22:39 www 

ls -lG 隐藏所有组(属组)信息

  1. [root@iz2ze76ybn73dvwmdij06zz /]# ls -lG 
  2. 总用量 72 
  3. lrwxrwxrwx    1 root     7 8月  26 22:36 bin -> usr/bin 
  4. dr-xr-xr-x.   5 root  4096 8月  26 22:39 boot 
  5. -rw-r--r--    1 root     0 9月  15 22:20 c 
  6. drwxr-xr-x   19 root  2960 4月  11 12:59 dev 
  7. drwxr-xr-x.  81 root  4096 9月   5 17:09 etc 
  8. drwxr-xr-x.   2 root  4096 4月  11 12:59 home 
  9. lrwxrwxrwx    1 root     7 8月  26 22:36 lib -> usr/lib 
  10. lrwxrwxrwx    1 root     9 8月  26 22:36 lib64 -> usr/lib64 
  11. drwx------.   2 root 16384 10月 15 2017 lost+found 
  12. drwxr-xr-x.   2 root  4096 4月  11 12:59 media 
  13. drwxr-xr-x.   2 root  4096 4月  11 12:59 mnt 
  14. drwxr-xr-x.   3 root  4096 8月  26 22:40 opt 
  15. drwxr-xr-x    2 root  4096 8月  26 23:00 patch 
  16. dr-xr-xr-x  110 root     0 4月  11 12:59 proc 
  17. dr-xr-x---.  12 root  4096 9月  15 22:18 root 
  18. drwxr-xr-x   26 root   760 9月   7 21:50 run 
  19. lrwxrwxrwx    1 root     8 8月  26 22:36 sbin -> usr/sbin 
  20. drwxr-xr-x.   2 root  4096 4月  11 12:59 srv 
  21. dr-xr-xr-x   13 root     0 4月  11 12:59 sys 
  22. drwxr-xr-x    3 root  4096 9月  15 22:18 test 
  23. drwxrwxrwt.   9 root  4096 9月  15 03:19 tmp 
  24. drwxr-xr-x.  13 root  4096 8月  26 22:36 usr 
  25. drwxr-xr-x.  19 root  4096 8月  26 22:55 var 
  26. drwxr-xr-x    6 root  4096 8月  26 22:39 www 
  27. 10.ls -li 显示文件的索引号 
  28. [root@iz2ze76ybn73dvwmdij06zz /]# ls -li 

ls -li 显示文件的索引号

  1. [root@iz2ze76ybn73dvwmdij06zz /]# ls -li 
  2.  
  3. 总用量 72 
  4.   7628 lrwxrwxrwx    1 root root     7 8月  26 22:36 bin -> usr/bin 
  5. 1179650 dr-xr-xr-x.   5 root root  4096 8月  26 22:39 boot 
  6.   4923 -rw-r--r--    1 root root     0 9月  15 22:20 c 
  7.   1026 drwxr-xr-x   19 root root  2960 4月  11 12:59 dev 
  8. 917505 drwxr-xr-x.  81 root root  4096 9月   5 17:09 etc 
  9. 655362 drwxr-xr-x.   2 root root  4096 4月  11 12:59 home 
  10.     17 lrwxrwxrwx    1 root root     7 8月  26 22:36 lib -> usr/lib 
  11.     13 lrwxrwxrwx    1 root root     9 8月  26 22:36 lib64 -> usr/lib64 
  12.     11 drwx------.   2 root root 16384 10月 15 2017 lost+found 
  13. 786434 drwxr-xr-x.   2 root root  4096 4月  11 12:59 media 
  14. 131074 drwxr-xr-x.   2 root root  4096 4月  11 12:59 mnt 
  15. 393219 drwxr-xr-x.   3 root root  4096 8月  26 22:40 opt 
  16. 1966081 drwxr-xr-x    2 root root  4096 8月  26 23:00 patch 
  17.      1 dr-xr-xr-x  110 root root     0 4月  11 12:59 proc 
  18. 131073 dr-xr-x---.  12 root root  4096 9月  15 22:18 root 
  19.    218 drwxr-xr-x   26 root root   760 9月   7 21:50 run 
  20.     15 lrwxrwxrwx    1 root root     8 8月  26 22:36 sbin -> usr/sbin 
  21. 393220 drwxr-xr-x.   2 root root  4096 4月  11 12:59 srv 
  22.      1 dr-xr-xr-x   13 root root     0 4月  11 12:59 sys 
  23. 2097153 drwxr-xr-x    3 root root  4096 9月  15 22:18 test 
  24. 393217 drwxrwxrwt.   9 root root  4096 9月  15 03:19 tmp 
  25. 1048578 drwxr-xr-x.  13 root root  4096 8月  26 22:36 usr 
  26. 262145 drwxr-xr-x.  19 root root  4096 8月  26 22:55 var 
  27. 1441793 drwxr-xr-x    6 root root  4096 8月  26 22:39 www 

ls -lt 按照修改时间排序(倒序)依次创建文件 a b

  1. [root@iz2ze76ybn73dvwmdij06zz test]# ls -lt 
  2. 总用量 0 
  3. -rw-r--r-- 1 root root 0 9月  15 22:18 b 
  4. -rw-r--r-- 1 root root 0 9月  15 22:18 a 

ls -version

  1. [root@iz2ze76ybn73dvwmdij06zz test]# ls --version 
  2. ls (GNU coreutils) 8.22 
  3. Copyright (C) 2013 Free Software Foundation, Inc. 
  4. 许可证:GPLv3+:GNU 通用公共许可证第3 版或更新版本<http://gnu.org/licenses/gpl.html>。 
  5. 本软件是自由软件:您可以自由修改和重新发布它。 
  6. 在法律范围内没有其他保证。 
  7.  
  8. 由Richard M. Stallman 和David MacKenzie 编写。 

cat

cat,又是一个相对高频率的命令。比较官方的说法是命令用于链接文件并打印到标准输出设备上。这个文本输出命令可以用来查看文件内容,创建内容等,下面我们介绍几个比较实用的用法。命令格式 : cat [选项]... [文件]...

cat -n 查看并显示行号

  1. [root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat -n a 
  2.     1  this is a txt 
  3.     2  hello every body 
  4.     3  hello world 
  5.     4  hahaha 

cat 创建一个空文件 b

  1. [root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat >b <<EOF 
  2. > EOF 
  3. [root@iz2ze76ybn73dvwmdij06zz linuxdemo]# ls 
  4. a  b 

cat 清空文件内容

  1. [root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat >a<<EOF 
  2. > EOF 
  3. [root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat a 

cat 写入内容(如果原来有内容将被覆盖)

  1. [root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat >a<<EOF 
  2. > today is a good day 
  3. > because off work 
  4. > hahaha 
  5. > EOF 
  6. [root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat a 
  7. today is a good day 
  8. because off work 
  9. hahaha 

cat 同时显示多个文件内容( b中写出happy)

  1. [root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat a > b 
  2. [root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat b 
  3. today is a good day 
  4. because off work 
  5. hahaha 

追加内容 可以看到用了连个>以后内容是追加的

  1. [root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat a >> b 
  2. [root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat b 
  3. today is a good day 
  4. because off work 
  5. hahaha 
  6. today is a good day 
  7. because off work 
  8. hahaha 

cat --help (如想学习更多,可以自己动手尝试)

  1. [root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat --help 
  2. 用法:cat [选项]... [文件]... 
  3. 将[文件]或标准输入组合输出到标准输出。 
  4.  
  5.  -A, --show-all           等于-vET 
  6.  -b, --number-nonblank    对非空输出行编号 
  7.  -e                       等于-vE 
  8.  -E, --show-ends          在每行结束处显示"$" 
  9.  -n, --number             对输出的所有行编号 
  10.  -s, --squeeze-blank      不输出多行空行 
  11.  -t                       与-vT 等价 
  12.  -T, --show-tabs          将跳格字符显示为^I 
  13.  -u                       (被忽略) 
  14.  -v, --show-nonprinting   使用^ 和M- 引用,除了LFD和 TAB 之外 
  15.      --help    显示此帮助信息并退出 
  16.      --version    显示版本信息并退出 
  17.  
  18. 如果没有指定文件,或者文件为"-",则从标准输入读取。 
  19.  
  20. 示例: 
  21.  cat f - g  先输出f 的内容,然后输出标准输入的内容,最后输出g 的内容。 
  22.  cat        将标准输入的内容复制到标准输出。 
  23.  
  24. GNU coreutils online help: <http://www.gnu.org/software/coreutils/> 
  25. 请向<http://translationproject.org/team/zh_CN.html> 报告cat 的翻译错误 
  26. 要获取完整文档,请运行:info coreutils 'cat invocation' 

用户权限

大家都知道linux是一个多任务,多用户的操作系统。多个用户可以在同一时间操作系统执行不同的任务,一个用户也可以多个人登陆操作。一般来说root的权限最大,我们经常使用的数据库一般会用一个mysql的用户,网页相关的我们可能用一个www的账号,大家都是知道删除跑路的故事,因此有一些操作权限是要交给一些专人管理的。为了系统的学习,我觉得有必要了解一下它的基本知识。

linux角色分类

超级用户:root (user id --UID) 0 最高的管理权限。

普通用户:

  系统用户 UID:1-999(rhel7 centos7) 1-499(rhel6)

  本地用户 UID:1000+(rhel7 centos7) 500+(rhel6)

UID:用户身份标示,唯一标示。

linux组分类

组:

组分类:

根据账号的功能分类:

  超级用户组:root GID:0

  普通用户组 :

      系统用户组: GID 1---999

      本地用户组: GID 1000+

linux用户和组的关系

一个用户属于一个组,也可以属于多个组,多个用户可以在一个组,可以在多个组。

linux用户和组的相关配置文件

1./etc/passwd:用户 and 属性信息。

2./etc/group: 组 and 属性信息。

3./etc/shadow: 用户密码 and 属性信息。

4./etc/gshadow:组密码 and 属性信息。

5./etc/default/useradd: 设置添加用户规则文件。

6./etc/login.defs:设置用户账号限制。

linux用户信息文件

用户管理中最重要的一个文件,这个文件是对所有用户开放的,每一行存一个用户的信息,每个属性之间用冒号分割。

  1. cat /etc/passwd 
  2. root:x:0:0:root:/root:/bin/zsh 
  3. bin:x:1:1:bin:/bin:/sbin/nologin 
  4. daemon:x:2:2:daemon:/sbin:/sbin/nologin 
  5. adm:x:3:4:adm:/var/adm:/sbin/nologin 
  6. lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 

举例:root:x:0:0:root:/root:/bin/zsh

1.登陆用户名:root

2.用户密码占位符:x

3.用户UID:0

4.用户组GID:0

5.对用户账户的描述:root

6.用户家目录位置:/root

7.用户默认的shell:/bin/zsh (默认是/bin/bash)

linux密码信息文件

该文件只有root用户有读权限,每一行存一个用户的记录,每个属性用冒号分割。

  1. cat /etc/shadow 
  2. root:$6$LxE7qiaZ$ZZXf/aCO1Zqs1oOS9WDutUk9rFOq4MOoSG9IeXq2f5sduGrgfYvzOWHZmJZFGC.0Of1DjCna0M.oQZTY8r00/.:17769:0:99999:7::: 
  3. bin:*:17110:0:99999:7::: 
  4. daemon:*:17110:0:99999:7::: 

举例:bin:*:17110:0:99999:7:::

1.登录用户名: bin

2.加密的密码: * 号代表密码被锁定

3.最近更改密码的日期: 17110代表到1970-1-1密码不可改的天数

4.密码修改期限:99999 代表永远不用改,如果是其他数字则表示从1970-1-1内的多少天必须修改密码。

5.更改密码最大有效天数: 代表密码保持有效的最大天数。

6.密码过期警告:密码到正式失效前有多少天(-1,永远不提示)

7.密码过期后多少天禁用用户:可登陆,单不能操作。

8.用户被禁用日期:多少天后账号过期,不能登陆。

9.保留参数

linux组信息文件

用户组的组信息存放在这,一行存一个组记录,属性用冒号分割。

  1. ➜  ~ cat /etc/group 
  2. root:x:0: 
  3. bin:x:1: 
  4. daemon:x:2: 
  5. sys:x:3: 
  6. adm:x:4: 
  7. tty:x:5: 
  8. disk:x:6: 
  9. lp:x:7: 
  10. mem:x:8: 
  11. kmem:x:9: 

举例:root:x:0:

1.组名:root

2.组密码占位符: x

3.组GID: 0

安装包管理基础

相信大部分人应该和我一样,最开始都是用 windows上手的,那在windows平台的安装包就再熟悉不过了,比如.exe,是不是太常见了,一般在windows安装过mysql的,相信.msi也是见过的。大多数的服务器都是linux的,因此我们来学习一下linux的安装包基础知识。不同发行版的系统用的工具也是不相同的。目前大多数的公司服务器是基于centos,因此后续会以centos为例比较多。

基于redhat

yum

zypper

rpm

基于centos

yum

rpm

rpm

全称: Redhat Package Manager

下载网址: http://rpmfind.net

先来看下包的命名格式,以 rpm-5.4.10-10.i586.rpm 为例。

rpm : 软件包名

5 : 主版本号

4 : 次版本号

10 : 修订号

i586 : cpu架构平台

rpm : 包后缀

yum

解决软件包安装的依赖关系问题,自动下载软件包,基于c/s架构。repo文件是yum源的配置文件,改文件定义软件仓库的信息。为什么说它是c/s架构,原来rpm软件的头(header)里会记录改软件的依赖信息,因此可以对头内容进行分析,就可以知道其相关的依赖,并将这些信息记录下来。那么服务端是提供这些rpm的软件包,然后分析包的依赖关系,将记录存在服务器某目录上。客户端在安装软件时下载其记录依赖关系文件并分析,最后一次性下载。

进程管理基础

一般来说程序分为两类,一种是系统程序,一种是应用程序。一个运行了程序,就可以说是一个进程了,进程是占用内存空间的,而当你杀掉进城时,资源也会随之释放。

进程的类型

用户进程:用户自己的程序,用户可以控制他的开启和关闭。

交互进程: 与用户进行交互操作的进程。

批处理进程:是一个进程集合,按顺序启动其他进程。

守护进程:一直运行的进程.crond。

进程的属性

进程ID : (pid) 唯一的数字标示,区分不同的进程。

进程有父进程和子进程。

启动进程的用户ID(uid)和用户的属组。

进程的三种状态:

运行 -- R

休眠 -- S

僵尸 -- Z

进程的优先级: 取值范围(-20,19) ,数值越小优先级越大,默认为0。

进程链接的终端。

进程占用资源情况。

父子进程的关系

父子进程的关系,父亲管理儿子。父进程终止时子进程一定终止,而子进程终止时父进程不一定终止。

进程管理工具

ps: 查看进程。

top: 可以查看进程的动态信息。

kill: 杀进程。

pstree: 查看进程树。

pgrep: 搜进程。

lsof: 查看进程打开的文件。

进程管理实践

ps

静态的显示当前进程的信息 。

命令: ps

语法: ps 参数

常用参数:

  a 显示所有用户的进程

  r 显示运行中的进程

  l 长格式输出

  u 按用户名和启动时间的顺序来显示进程

  f 用树形格式来显示进程

  x 显示没有控制终端的进程

操作:BSD格式来显示进程

  1. ➜  ~ ps aux 
  2. USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND 
  3. root         1  0.0  0.0  43364  3596 ?        Ss   11月03   0:01 /usr/lib/syst 
  4. root         2  0.0  0.0      0     0 ?        S    11月03   0:00 [kthreadd] 
  5. root         3  0.0  0.0      0     0 ?        S    11月03   0:00 [ksoftirqd/0] 
  6. root         5  0.0  0.0      0     0 ?        S<   11月03   0:00 [kworker/0:0H 
  7. root         7  0.0  0.0      0     0 ?        S    11月03   0:00 [migration/0] 
  8. root         8  0.0  0.0      0     0 ?        S    11月03   0:00 [rcu_bh] 
  9. root         9  0.0  0.0      0     0 ?        S    11月03   0:05 [rcu_sched] 
  10. root        10  0.0  0.0      0     0 ?        S    11月03   0:00 [watchdog/0] 
  11. root        11  0.0  0.0      0     0 ?        S    11月03   0:00 [watchdog/1] 
  12. root        12  0.0  0.0      0     0 ?        S    11月03   0:00 [migration/1] 
  13. root        13  0.0  0.0      0     0 ?        S    11月03   0:00 [ksoftirqd/1] 
  14. root        15  0.0  0.0      0     0 ?        S<   11月03   0:00 [kworker/1:0H 

标准格式显示进程unix风格

  1.  ~ ps -ef 
  2. UID        PID  PPID  C STIME TTY          TIME CMD 
  3. root         1     0  0 11月03 ?      00:00:01 /usr/lib/systemd/systemd --syste 
  4. root         2     0  0 11月03 ?      00:00:00 [kthreadd] 
  5. root         3     2  0 11月03 ?      00:00:00 [ksoftirqd/0] 
  6. root         5     2  0 11月03 ?      00:00:00 [kworker/0:0H] 
  7. root         7     2  0 11月03 ?      00:00:00 [migration/0] 
  8. root         8     2  0 11月03 ?      00:00:00 [rcu_bh] 
  9. root         9     2  0 11月03 ?      00:00:05 [rcu_sched] 
  10. root        10     2  0 11月03 ?      00:00:00 [watchdog/0] 
  11. root        11     2  0 11月03 ?      00:00:00 [watchdog/1] 
  12. root        12     2  0 11月03 ?      00:00:00 [migration/1] 
  13. root        13     2  0 11月03 ?      00:00:00 [ksoftirqd/1] 
  14. root        15     2  0 11月03 ?      00:00:00 [kworker/1:0H] 
  15. root        17     2  0 11月03 ?      00:00:00 [kdevtmpfs] 
  16. root        18     2  0 11月03 ?      00:00:00 [netns] 
  17. root        19     2  0 11月03 ?      00:00:00 [khungtaskd] 
  18. root        20     2  0 11月03 ?      00:00:00 [writeback] 
  19. root        21     2  0 11月03 ?      00:00:00 [kintegrityd] 

属性详解:

USER: 进程的属主

PID: 进程的ID

%CPU: 进程占cpu百分比

%MEM: 进程占内存的百分比

VSZ: 进程占用虚拟内存大小

RSS: 固定内存使用数量

STAT 进程状态

      R 正在运行可中在队列中可过行的;

      S 处于休眠状态;

      T 停止或被追踪;

      Z 僵尸进程;

      N 优先级较低的进程

      L 有些页被锁进内存;

      s 进程的领导者(在它之下有子进程)

START 启动进程的时间;

TIME 进程消耗CPU的时间;

COMMAND 命令的名称和参数;

按照指定属性排序:

按照cpu从小到大排序。

  1. ➜  ~ ps aux --sort %cpu 
  2. USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND 
  3. root         1  0.0  0.0  43364  3596 ?        Ss   11月03   0:01 /usr/lib/syst 
  4. root         2  0.0  0.0      0     0 ?        S    11月03   0:00 [kthreadd] 
  5. root         3  0.0  0.0      0     0 ?        S    11月03   0:00 [ksoftirqd/0] 
  6. root         5  0.0  0.0      0     0 ?        S<   11月03   0:00 [kworker/0:0H 
  7. root         7  0.0  0.0      0     0 ?        S    11月03   0:00 [migration/0] 
  8. root         8  0.0  0.0      0     0 ?        S    11月03   0:00 [rcu_bh] 
  9. root         9  0.0  0.0      0     0 ?        S    11月03   0:05 [rcu_sched] 
  10. root        10  0.0  0.0      0     0 ?        S    11月03   0:00 [watchdog/0] 
  11. root        11  0.0  0.0      0     0 ?        S    11月03   0:00 [watchdog/1] 

按照cpu从大到小排序。

  1. ➜  ~ ps aux --sort -%cpu 
  2. USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND 
  3. root     21342  0.3  0.4 137184 17112 ?        Ssl  03:24   1:16 /usr/local/aegi 
  4. root         1  0.0  0.0  43364  3596 ?        Ss   11月03   0:01 /usr/lib/syst 
  5. root         2  0.0  0.0      0     0 ?        S    11月03   0:00 [kthreadd] 
  6. root         3  0.0  0.0      0     0 ?        S    11月03   0:00 [ksoftirqd/0] 
  7. root         5  0.0  0.0      0     0 ?        S<   11月03   0:00 [kworker/0:0H 
  8. root         7  0.0  0.0      0     0 ?        S    11月03   0:00 [migration/0] 
  9. root         8  0.0  0.0      0     0 ?        S    11月03   0:00 [rcu_bh] 
  10. root         9  0.0  0.0      0     0 ?        S    11月03   0:05 [rcu_sched] 
  11. root        10  0.0  0.0      0     0 ?        S    11月03   0:00 [watchdog/0] 
  12. root        11  0.0  0.0      0     0 ?        S    11月03   0:00 [watchdog/1] 
  13. root        12  0.0  0.0      0     0 ?        S    11月03   0:00 [migration/1] 
  14. root        13  0.0  0.0      0     0 ?        S    11月03   0:00 [ksoftirqd/1] 

top

与ps相反的是top命令可以查看进程的动态信息。

命令: top

语法: top 参数

常用参数:

操作:

  1. ➜  ~ top 
  2. top - 11:38:42 up 13:15,  2 users,  load average: 0.01, 0.03, 0.05 
  3. Tasks:  98 total,   1 running,  97 sleeping,   0 stopped,   0 zombie 
  4. %Cpu(s):  0.2 us,  0.0 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st 
  5. KiB Mem :  3881688 total,   229264 free,   543932 used,  3108492 buff/cache 
  6. KiB Swap:  1049596 total,  1049596 free,        0 used.  3026944 avail Mem 
  7.  
  8.  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND 
  9.    1 root      20   0   43364   3596   2340 S   0.0  0.1   0:01.79 systemd 
  10.    2 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kthreadd 
  11.    3 root      20   0       0      0      0 S   0.0  0.0   0:00.14 ksoftirqd/0 
  12.    5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:+ 
  13.    7 root      rt   0       0      0      0 S   0.0  0.0   0:00.33 migration/0 
  14.    8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh 
  15.    9 root      20   0       0      0      0 S   0.0  0.0   0:05.73 rcu_sched 

属性解释:

前五行是数据的整理统计信息。

第一行:

   11:38:42 当前时间

   up 13:15 系统运行时间,格式为时:分

   2 user 当前登陆用户数量

   load average: 0.01, 0.03, 0.05 系统负载,任务队列平均长度 1分钟,5分钟,15分钟前到现在的平均值。

第二三行:

   98 total 进程总数量

  1 running 正在运行的进程数量

  97 sleeping 睡眠的进程数量

  0 stopped 停止的进程数量

  0 zombie 僵尸进程数量

  %Cpu(s):

       0.2 us 系统用户进程使用cpu百分比

       0.0 sy 内核进程占用cpu百分比

       0.0 ni 用户进程空间内改变过优先级的进程占用cpu百分比

           99.8 id 空闲cpu百分比

               0.0 wa 等待输入输出的cpu时间百分比

               0.0 hi 硬件cpu中断占用百分比

               0.0 si 软中断占用百分比

               0.0 st 虚拟机占用百分比

第四五行:

Mem : (单位K)

  3881688 total 物理内存总量

  229264 free 空闲内存总量

  543932 used 使用的物理内存总量

  3108492 buff/cache 内核缓存的内存量

Swap: (单位K)

  1049596 total 交换区总量

  1049596 free 空闲交换区总量

  0 used 使用的交换区总量

  3026944 avail Mem 可利用的内存量

标题行:

PID: 进程id

USER: 进程所有者用户名

PR: 优先级

NI: 进程优先级,nice值,负值 -> 高优先级,正值 -> 低优先级

VIRT: 虚拟内存总量 virt=swap + res

RES: 实际使用内存大小

SHR: 共享内存大小

S: 进程状态

d: 不可中断的睡眠状态 r: 运行 s: 睡眠 t: 跟踪 z: 僵尸进程

%CPU: 上次更新到现在cpu时间占用百分比

%MEM: 进程使用物理内存百分比

TIME+: 进程使用cpu的时间总计,单位 1/100秒

COMMAND: 命令行

kill

命令: kill

语法:

kill [-s signal|-p] [-q sigval] [-a] [--] pid... kill -l [signal]

常用参数:

-l 列出所有信号名称

-s 指定发送信号(默认)

-u 指定用户

操作:列出所有信号名称

  1. ➜  ~ kill -l 
  2. HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH POLL PWR SYS 

强制终止

  1. ➜  ~ kill -9 23423 

终止

  1. ➜ ~ kill -15 22323 

杀死指定用户的所有进程

  1. ➜ ~ kill -u superh 

proc目录

这个目录中有内核,进程运行状态的信息。

  1. ➜  /proc ls 
  2. 1      21320  21628  240   392   7          diskstats    loadavg       swaps 
  3. 10     21342  21976  242   42    722        dma          locks         sys 
  4. 10353  21465  21977  243   44    7561       driver       mdstat        sysrq-trigger 
  5. 10509  21613  21978  244   442   8          execdomains  meminfo       sysvipc 
  6. 107    21614  22     249   45    803        fb           misc          timer_list 
  7. 11     21615  220    26    46    8179       filesystems  modules       timer_stats 
  8. 11010  21616  22285  261   466   8307       fs           mounts        tty 
  9. 11102  21617  22287  262   467   8327       interrupts   mtrr          uptime 
  10. 12     21618  22361  267   469   9          iomem        net           version 
  11. 13     21619  22363  268   476   acpi       ioports      pagetypeinfo  vmallocinfo 
  12. 14461  21620  22396  3     477   buddyinfo  irq          partitions    vmstat 
  13. 15     21621  22467  31    485   bus        kallsyms     sched_debug   zoneinfo 
  14. 17     21622  225    3198  5     cgroups    kcore        schedstat 
  15. 18     21623  229    32    505   cmdline    keys         scsi 
  16. 19     21624  23     33    506   consoles   key-users    self 
  17. 2      21625  23884  336   5217  cpuinfo    kmsg         slabinfo 
  18. 20     21626  23885  34    5515  crypto     kpagecount   softirqs 
  19. 21     21627  24     357   65    devices    kpageflags   stat 

上面的每个标号即是用我们当前进程的pid所命名的目录名。

free

查看系统使用和空闲的内存情况。

命令: free

语法: free 参数

常用参数:

-b, --bytes byte为单位展示

-k, --kilo k为单位展示

-m, --mega m为单位展示

-g, --giga g为单位展示

操作:

  1. ➜  ~ free -m 
  2.              total        used        free      shared  buff/cache   available 
  3. Mem:           3790         536         210           2        3043        2950 
  4. Swap:          1024           0        1024 

单位为g显示。

  1. ➜  ~ free -h 
  2.             total        used        free      shared  buff/cache   available 
  3. Mem:           3.7G        538M        207M        2.4M        3.0G        2.9G 
  4. Swap:          1.0G          0B        1.0G 

文件查找

在做运维相关的事情时,我们少不了要查这样那样的文件,一般来说我们可能会查比如带xxx.conf名字的文件在哪呢,查日志啊,某个可执行文件的位置啊。

概览

which : 查看执行文件的位置。

whereis : 查看可执行文件位置和相关文件。

locate : 配合数据库缓存,快速查看文件的位置。

grep : 过滤匹配,他是一个文件搜索工具。

find : 可以根据条件查看文件。

which

命令:which

语法: which 参数 查询目标

常用命令:一般不加参数使用

--all, -a 显示所有的匹配路径

操作:

  1. ➜  ~ which pwd 
  2. /usr/bin/pwd 

whereis

命令: whereis

语法: whereis 参数 查询目标

常用命令:

一般不加参数使用

-b 只搜索二进制文件

-B <目录> 定义二进制文件查找路径

-m 只搜索 man 手册

-M <目录> 定义 man 手册查找路径

-s 只搜索源代码

-S <目录> 定义源代码查找路径

操作:

  1. ➜  ~ whereis ls 
  2. ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz 

locate

命令: locate

语法: locate 文件名

常用参数:无

操作:locate和find命令功能差不多,但是搜索效率更高,因为locate查的是数据库而find查找的是目录文件。

数据库:

  1. ➜  ~ ls /var/lib/mlocate/mlocate.db 
  2. /var/lib/mlocate/mlocate.db 

相关配置文件:

  1. ➜  ~ ls /etc/updatedb.conf 
  2. /etc/updatedb.conf 

相关定时任务:

  1. ➜  ~ ls /etc/cron.daily/mlocate 
  2. /etc/cron.daily/mlocate 

举例:

  1. ➜  ~ touch shafa 
  2. ➜  ~ locate shafa 
  3. ➜  ~ updatedb 
  4. ➜  ~ locate shafa 
  5. /root/shafa 

注:如果我们当天新建的文件查找,需要手动updatedb。

grep

命令:grep

语法:grep 参数 目标值 文件

常用参数:

-v 取反

-i 忽略大小写

-n 输出的同时打印行号

^* 以*开头

*$ 以*结尾

^$ 空行

操作:

查 /etc/passwd有root的行

  1. ➜  ~ grep root /etc/passwd 
  2. root:x:0:0:root:/root:/bin/zsh 
  3. operator:x:11:0:operator:/root:/sbin/nologin 

查 /etc/passwd 没有root的行(内容较多,禁展示几行)

  1. ➜  ~ grep -v root /etc/passwd 
  2. bin:x:1:1:bin:/bin:/sbin/nologin 
  3. daemon:x:2:2:daemon:/sbin:/sbin/nologin 
  4. adm:x:3:4:adm:/var/adm:/sbin/nologin 
  5. lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 
  6. sync:x:5:0:sync:/sbin:/bin/sync 

查 /etc/passwd 有www的行并显示行号

  1. ➜  ~ grep -n www /etc/passwd 
  2. 23:www:x:1000:1000::/home/www:/sbin/nologin 

find

命令:find

语法:find 路径 参数 输出

常用参数:

路径: 例如用 . 来表示当前目录,用 / 来表示系统根目录。 -print:显示的时候”\n”做为定界符, 换行 -print0:与xargs配套使用,以“\0”做为定界符

find常用命令选项:

-name 按照文件名查找文件。“名称”

-perm 按照文件权限来查找文件。666 777 等

-depth 在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找

-user 按照文件属主来查找文件

-atime -ctime (单位是天)

-mmin -cmin -amin(单位是分钟)

-size n [c] 查找文件长度为n块的文件,带有c时表示文件长度以字节计

-follow 如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。

操作:

查看当前目录下test.开头的文件

  1. ➜  ~ find . -name "test*" 
  2. ./test.txt 
  3. ./autojump/tests 
  4. ./test.1 

查看当前目录下test.开头的文件,输出结果换行显示(默认)

  1. ➜  ~ find . -name "test*" -print 
  2. ./test.txt 
  3. ./autojump/tests 
  4. ./test.1 

查看当前目录下test.开头的文件,输出结果不换行显示

  1. ➜  ~ find . -name "test*" -print0 
  2. ./test.txt./autojump/tests./test.1 

对查找的内容执行相应命令

默认执行动作 : -print

常用动作:

-exec 这个参数后可以跟自定义shell命令

操作:

查询.txt结尾的文件并用ls查询其更多属性

  1. ➜  test find . -name "*.txt" -exec ls -l {} \; 
  2. -rw-r--r-- 1 root root 0 11月  4 14:56 ./xq1.txt 
  3. -rw-r--r-- 1 root root 0 11月  4 14:56 ./xq.txt 
  4. -rw-r--r-- 1 root root 0 11月  4 14:56 ./xq2.txt 

查询.txt结尾的文件并变成.bak结尾的文件

  1. ➜  test find . -name "*.txt" -exec mv {} {}.bak \; 
  2. ➜  test ls 
  3. xq1.txt.bak  xq2.txt.bak  xq.txt.bak 

逻辑查询:

-a 并且 -o 或者 + 高于 - 低于

操作:查看 .sh或者.q结尾的文件。

  1. ➜  test find . -name "*.sh" -o -name "*.q" 
  2. ./book.q 
  3. ./stop.sh 
  4. ./start.sh 

查看.sh结尾并且s开头的文件。

  1. ➜  test find . -name "*.sh" -a -name "s*" 
  2. ./stop.sh 
  3. ./start.sh 

在文件中插入内容后查看。

  1. ➜  test ll 
  2. 总用量 12K 
  3. -rw-r--r-- 1 root root  9 11月  4 15:10 book.q 
  4. -rw-r--r-- 1 root root 13 11月  4 15:10 start.sh 
  5. -rw-r--r-- 1 root root  4 11月  4 15:10 stop.sh 
  6. -rw-r--r-- 1 root root  0 11月  4 14:56 xq1.txt.bak 
  7. -rw-r--r-- 1 root root  0 11月  4 14:56 xq2.txt.bak 
  8. -rw-r--r-- 1 root root  0 11月  4 14:56 xq.txt.bak 

查看/etc 大于40k小于50k的文件

  1. ➜  test find /etc/ -size +40k -a -size  -50k 
  2. /etc/selinux/targeted/active/modules/100/sysadm/hll 
  3. /etc/selinux/targeted/contexts/files/file_contexts.homedirs.bin  

本文转载自网络,原文链接:https://mp.weixin.qq.com/s/FeETF66XLbUVkleptIWEVw
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!
上一篇:Linux学习笔记——CentOS 下一篇:没有了

推荐图文


随机推荐