Linux常用命令

319 阅读8分钟

跟老男孩学Linux运维

1. Linux命令行介绍

Linux命令行查看命令帮助
- #号,是使用超级用户root登录后的命令行结尾提示符
- $号,是使用普通用户登录后的命令行结尾提示符
- Ctrl + l : 清除屏幕上所有内容
- Ctrl + r : 搜索历史命令记录
- Ctrl + a :  光标回到命令行首
- Ctrl + e: 光标回到命令行尾
- Ctrl + Inset : 复制命令行内容
- Shift + Insert: 粘贴复制命令
重启或关机命令: shutdown

与shutdown功能类似的命令还有init、halt、poweroff、reboot

通常情况下,我们执行的shutdown命令为shutdown-h now或shutdown-r now

image.png

2. 文件和目录操作

pwd: 文件显示当前所在位置
cd : 切换目录
tree : 以树形结构显示目录下的内容

image.png

mkdir: 创建目录

image.png

touch : 创建空文件或者改变文件的时间戳属性

​ touch命令有两个功能:一是创建新的空文件;二是改变已有文件的时间戳属性。

image.png

ls:显示目录下的内容及相关属性信息

image.png

cp:复制文件或目录

image.png

mv:移动或重命名文件

image.png

rm:删除文件或目录

image.png

rmdir:删除空目录

image.png

ln:硬链接与软链接

image.png

find:查找目录下的文件

find命令用于查找目录下的文件,同时也可以调用其他命令执行相应的操作。

image.png

rename:重命名文件

image.png

md5sum:计算和校验文件的MD5值

image.png

image.png

生成MD5值

[root@oldboy ~]# md5sum oldboy.txt            #<==md5sum命令直接接文件就可以得出
                                             文件的MD5值。
e871a0fceeae39f07e1e2cd6c6bdebc6  oldboy.txt      #<==结果有两部分,第一部分是MD5值,
                                             后面是文件名。

校验MD5值

[root@oldboy ~]# md5sum oldboy.txt >md5.log      #<==先生成校验文件。
[root@oldboy ~]# cat md5.log
e871a0fceeae39f07e1e2cd6c6bdebc6  oldboy.txt
[root@oldboy ~]# md5sum -c md5.log            #<==检查使用-c参数。
oldboy.txt: OK                              #<==结果ok表示文件没有变化。
[root@oldboy ~]# echo "oldboy">oldboy.txt      #<==修改文件。
[root@oldboy ~]# md5sum -c md5.log
oldboy.txt: FAILED                        #<==文件被修改,md5值肯定发生了改变,
                                             验证失败。
md5sum: WARNING: 1 of 1 computed checksum did NOT match
[root@oldboy ~]# md5sum --status -c md5.log      #<==不直接输出结果,改用返回值判断
                                             结果。
[root@oldboy ~]# echo $?
1
chown:改变文件或目录的用户和用户组

image.png

更改文件所属的用户属性


[root@oldboy data]# ll file1.txt
-rw-r--r-- 1 root root 0 Nov  4 18:24 file1.txt      #<==当前用户是root。
[root@oldboy data]# chown xxx file1.txt
chown: invalid user: 'xxx'                  #<==被授权的用户需要存在,否则会出错。
[root@oldboy data]# chown oldboy file1.txt      #<==授权oldboy用户,oldboy用户要
                                             提前创建。
[root@oldboy data]# ll file1.txt
-rw-r--r-- 1 oldboy root 0 Nov  4 18:24 file1.txt
chmod:改变文件或目录权限

chmod命令是用来改变文件或目录权限的命令,但是只有文件的属主和超级用户root才能够执行这个命令。

3. 文件过滤 及 内容编辑处理命令

cat:合并文件或查看文件内容

image.png

表3-2针对该命令的参数选项进行了说明。

image.png

less:分页显示文件内容

image.png

tail:显示文件内容尾部

image.png

split:分割文件

image.png


按行分割文件,以及指定后缀形式

[root@oldboy data]# wc -l inittab                  #<==wc命令可以查看文件的行
                                                    数,后面会详细讲解。
26 inittab
[root@oldboy data]# split -l 10 inittab new_            #<==每10行分割一次,分割的
                                                    文件名以new_开头。
[root@oldboy data]# ls new_*
new_aa  new_ab  new_ac
[root@oldboy data]# wc -l new_*
    10 new_aa
    10 new_ab
     6 new_ac
    26 total
[root@oldboy data]# split -l 10 -a 3 inittab new2_      #<==参数-a指定后缀长度。
[root@oldboy data]# wc -l new2_*
    10 new2_aaa
    10 new2_aab
     6 new2_aac
    26 total
[root@oldboy data]# split -l 10 -d inittab num_      #<==参数-d使用数字后缀。
[root@oldboy data]# wc -l num_*
    10 num_00
    10 num_01
     6 num_02
    26 total
按行分割文件,以及指定后缀形式

[root@oldboy data]# cp /sbin/lvm .            #<==复制一个测试文件。
[root@oldboy data]# ll -h
total 1.3M
-r-xr-xr-x 1 root root 1.3M Aug 30 15:57 lvm
[root@oldboy data]# split -b 500K -d lvm lvm_      #<==每500KB分割一次文件。
[root@oldboy data]# ll -h
total 2.6M
-r-xr-xr-x 1 root root 1.3M Aug 30 15:57 lvm
-rw-r--r-- 1 root root 500K Aug 30 15:59 lvm_00
-rw-r--r-- 1 root root 500K Aug 30 15:59 lvm_01
-rw-r--r-- 1 root root 308K Aug 30 15:59 lvm_02

sort:文本排序

image.png

默认以行为单位进行比较。

[root@oldboy ~]# cat oldboy.txt
10.0.0.4
10.0.0.4
10.0.0.4
10.0.0.5
10.0.0.4
10.0.0.8
[root@oldboy ~]# sort oldboy.txt #<==不接收任何参数,会将文件内容转换成ASCII码,然后进行比较。因为在ASCII码中,数字的排序和我们的认知是一样的,因此结果如下所示。
10.0.0.4
10.0.0.4
10.0.0.4
10.0.0.4
10.0.0.5
10.0.0.8
uniq:去除重复行

image.png

去重测试案例。

[root@oldboy ~]# cat oldboy.txt
10.0.0.4
10.0.0.4
10.0.0.4
[root@oldboy ~]# uniq oldboy.txt    #<==不接任何参数即去除重复行。
10.0.0.4
[root@oldboy ~]# uniq -c oldboy.txt #<==参数-c显示相应行出现的次数。
      3 10.0.0.4
结合sort去重

[root@oldboy ~]# cat oldboy.txt
10.0.0.4
10.0.0.4
10.0.0.4
10.0.0.5
10.0.0.4
10.0.0.8
[root@oldboy ~]# uniq oldboy.txt
10.0.0.4
10.0.0.5
10.0.0.4
10.0.0.8
#<==说明:还有2行一样的。
[root@oldboy ~]# sort -n oldboy.txt|uniq -c
      4 10.0.0.4
      1 10.0.0.5
      1 10.0.0.8
vi/vim:纯文本编辑器

vim可分为三种模式:普通模式、编辑模式、命令模式

  1. 普通模式:用vim命令打开一个文件,默认的状态就是普通模式。在这个模式中,不能进行编辑输入操作,但可以按“上下左右”键来移动光标,也可以执行一些操作命令进行如删除、复制、粘贴等之类的工作

  2. 编辑模式:在普通模式下不能进行编辑输入操作,只有按下“i,I,o,O,a,A,r,R,s,S”(其中“I”最常用)等字母进入编辑模式之后才可以执行录入文字等编辑操作。

  3. 命令模式:在普通模式下,输入“:”或“/”或“?”时,光标会自动定位在那一行,在这个模式中,可以执行保存、退出、搜索、替换、显示行号等相关操作

image.png

image.png

image.png

4. 文本处理三剑客

grep:文本过滤工具

image.png

image.png

请使用grep过滤不包含oldboy字符串的行(-v参数实践)

[root@oldboy oldboy]# cat test1.txt    #<==查看待测试的文件。
test
liyao
oldboy
[root@oldboy oldboy]# grep -v "oldboy" test1.txt   #<==过滤不包含oldboy字符串的
                                                  行,注意被过滤的字符串,尽
                                                  可能使用双引号。
test
liyao
使用grep命令显示过滤后的内容的行号

[oldboy@oldboy ~]$ cat test2.txt            #<==查看待测试的文件。
lisir
oldboy
oldboy linux
ALEX
[oldboy@oldboy ~]$ grep -n "oldboy" test2.txt      #<==输出包含oldboy字符串的行,并显
                                             示行号。
2:oldboy
3:oldboy linux
[oldboy@oldboy ~]$ grep -n "." test2.txt           #<==显示所有行的行号(类似cat -n test2.txt),这里的"."代表匹配任意单个字符,即匹配了所有的内容,所以,显示了所有行的行号。
1:lisir
2:oldboy
3:oldboy linux
4:ALEX

-i不区分大小写参数实践

[oldboy@oldboy ~]$ grep "alex" test2.txt     #<==过滤小写alex的行,结果没有内容。
[oldboy@oldboy ~]$ grep -i "alex" test2.txt  #<==使用-i参数不区分大小写过滤alex。
ALEX
计算匹配的字符串的数量(-c参数实践)

[oldboy@oldboy ~]$ grep "oldboy" test2.txt
oldboy
oldboy linux
[oldboy@oldboy ~]$ grep -c "oldboy" test2.txt
2
sed:字符流编辑器

sed是操作、过滤和转换文本内容的强大工具。常用功能包括对文件实现快速增删改查(增加、删除、修改、查询),其中查询的功能中最常用的两大功能是过滤(过滤指定字符串)、取行(取出指定行

image.png

image.png

  1. 基础范例:为了更好地测试sed命令的用法,准备测试的内容及文件如下

    
    [root@oldboy ~]# cat >persons.txt<<EOF
    101,oldboy,CEO
    102,zhangyao,CTO
    103,Alex,COO
    104,yy,CFO
    105,feixue,CIO
    EOF #<==这里要敲回车才能结束,另外,EOF必须成对出现,但也可以用别的成对标签来替换。例如:oldboy字符标签。
    
    
  2. 在文件指定行后追加文本。

    [root@oldboy ~]# sed '2a 106,dandan,CSO' persons.txt  #<==这里使用了sed内置命令a追加功能。
    101,oldboy,CEO
    102,zhangyao,CTO
    106,dandan,CSO
    103,Alex,COO
    104,yy,CFO
    105,feixue,CIO
    
    

    ·sed后面单引号中的内容为:2a 106,dandan,CSO

    ·2表示对第2行进行操作,其他的行忽略。

    ·a表示追加,2a即在第2行后追加文本。

    ·2a后面加上空格,然后接着输入想要追加的文本内容(106,dandan,CSO)即可。

  3. 在文件指定的行前插入文本。

    [root@oldboy ~]# sed '2i 106,dandan,CSO' persons.txt   #<==这里使用了sed内置命令i插入功能。
    101,oldboy,CEO
    106,dandan,CSO
    102,zhangyao,CTO
    103,Alex,COO
    104,yy,CFO
    105,feixue,CIO
    
    
  4. 在文件指定行后追加多行文本。

    [root@oldboy ~]# sed '2a 106,dandan,CSO\n107,bingbing,CCO' person.txt
    101,oldboy,CEO
    102,zhangyao,CTO
    106,dandan,CSO
    107,bingbing,CCO
    103,Alex,COO
    104,yy,CFO
    105,feixue,CIO
    
  5. 删除文件中一行指定的文本

    [root@oldboy ~]# sed '2d' person.txt   #<==这里使用了sed内置命令d实现删除功能,指
                                           定删除第2行的文本102,zhangyao,CTO。
    101,oldboy,CEO
    103,Alex,COO
    104,yy,CFO
    105,feixue,CIO
    
  6. 删除文件中指定的多行文本。

    [root@oldboy ~]# sed '2,5d' person.txt   #<==“2,5”是一个数字地址的组合,用逗号作为分隔,其作用是删除文件的第二行到第五行(删除多行)文本,包括第2行和第5行,因此只剩下第1行。
    101,oldboy,CEO
    
  7. 使用sed命令替换文本内容。

    [root@oldboy ~]# sed 's#zhangyao#dandan#g' person.txt  #<==这里使用了sed内置命令s来实现替换功能,并且使用了全局替换标志g表示替换文件中匹配zhangyao的所有字符串。需要注意一下语法格式,将需要替换的文本“zhangyao”放在第一个和第二个“#”之间,将替换后的文本“dandan”放在第二个和第三个“#”之间。结果为第二行的“zhangyao”替换为“dandan”。
    101,oldboy,CEO
    102,dandan,CTO
    103,Alex,COO
    104,yy,CFO
    105,feixue,CIO
    
  8. 打印输出文件的指定行的内容

    [root@oldboy ~]# sed '2p' person.txt  #<==这里使用了sed内置命令p来实现查询功能,并结合数字地址指定查询第2行的内容,但是我们会发现结果不只是输出第2行,文件的其他内容也显示出来了,这是因为sed命令有一个默认输出的功能。
    101,oldboy,CEO   #<==默认输出的行。
    102,zhangyao,CTO #<== p命令输出的行。
    102,zhangyao,CTO #<==默认输出的行。
    103,Alex,COO     #<==默认输出的行。
    104,yy,CFO       #<==默认输出的行。
    105,feixue,CIO   #<==默认输出的行。
    [root@oldboy ~]# sed -n '2p' person.txt    #<==为了解决上面命令显示多余内容的问题,使用选项-n取消默认输出,只输出匹配行的文本,因此大家只需要记住使用命令p必用选项-n。
    102,zhangyao,CTO
    [root@oldboy ~]# sed -n '2,3p' person.txt  #<==当然使用地址范围“2,3”能够查看第2
                                                行到第3行的内容。
    102,zhangyao,CTO
    103,Alex,COO
    
awk基础入门

image.png

image.png

image.png

  1. 基础范例: 测试文件及内容如下:

    [root@oldboy ~]# cat oldboy.txt
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
    
    
  2. 显示文件中的第5行。

    [root@oldboy ~]# cat -n oldboy.txt        #<==打印内容并在每行内容的开头显示行号。
         1  root:x:0:0:root:/root:/bin/bash
         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
         7  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
         8  halt:x:7:0:halt:/sbin:/sbin/halt
         9  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
        10  uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
    [root@oldboy ~]# awk 'NR==5' oldboy.txt
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin   #<==和上面内容对比确实是第5行。
    

    首先NR在awk中表示行号(记录号),NR==5表示行号等于5的行

  3. 例如显示2-6行

    [root@oldboy ~]# awk 'NR==2,NR==6' oldboy.txt
    bin:x:1:1:bin:/bin:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    sync:x:5:0:sync:/sbin:/bin/sync
    
  4. 用awk实现给文件每行的内容之前加上行号。

    [root@oldboy ~]# awk '{print NR,$0}' oldboy.txt
    1 root:x:0:0:root:/root:/bin/bash
    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
    7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    8 halt:x:7:0:halt:/sbin:/sbin/halt
    9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
    

    这里的NR还是表示行号,$0表示一整行的内容(一行的内容)。

    print关键字表示显示的内容,相当于是awk内部的一个命令。

    那么print命令为何要放在花括号中呢?因为这个命令(动作)是“很害羞”的,需要“城墙”保护(花括号)。

image.png

  1. 显示oldboy.txt文件的第2行到6行,并打印行号。

    [root@oldboy ~]# awk 'NR==2,NR==6 {print NR,$0}' oldboy.txt  #<==注意位置和写法。
    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
    
  2. 显示oldboy.txt文件的第一列、第三列和最后一列

    [root@oldboy ~]# awk -F ":" '{print $1,$3,$NF}' oldboy.txt
    root 0 /bin/bash
    bin 1 /sbin/nologin
    daemon 2 /sbin/nologin
    adm 3 /sbin/nologin
    lp 4 /sbin/nologin
    sync 5 /bin/sync
    shutdown 6 /sbin/shutdown
    halt 7 /sbin/halt
    mail 8 /sbin/nologin
    uucp 10 /sbin/nologin
    

    awk -F ":" '{print 1,1,3,$NF}' oldboy.txt

    awk指定冒号为分隔符 '{显示 第一列和第三列和最后一列}' oldboy.txt

  3. 把文件中的/sbin/nologin替换为/bin/bash(awk函数功能实践)

    [root@oldboy ~]# awk '{gsub("/sbin/nologin","/bin/bash",$0);print $0}' oldboy.txt
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/bin/bash
    daemon:x:2:2:daemon:/sbin:/bin/bash
    adm:x:3:4:adm:/var/adm:/bin/bash
    lp:x:4:7:lp:/var/spool/lpd:/bin/bash
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt
    mail:x:8:12:mail:/var/spool/mail:/bin/bash
    uucp:x:10:14:uucp:/var/spool/uucp:/bin/bash
    

image.png

  1. 企业面试题——统计域名访问次数

    [root@oldboy ~]# cat oldgirl.txt
    http://www.etiantian.org/index.html
    http://www.etiantian.org/1.html
    http://post.etiantian.org/index.html
    http://mp3.etiantian.org/index.html
    http://www.etiantian.org/3.html
    http://post.etiantian.org/2.html
    
    取出每行中的域名
    [root@oldboy ~]# awk -F '/' '{print $3}' oldgirl.txt
    www.etiantian.org
    www.etiantian.org
    post.etiantian.org
    mp3.etiantian.org
    www.etiantian.org
    post.etiantian.org
    
    排序(让相同的域名相邻)
    [root@oldboy ~]# awk -F '/' '{print $3}' oldgirl.txt|sort
    mp3.etiantian.org
    post.etiantian.org
    post.etiantian.org
    www.etiantian.org
    www.etiantian.org
    www.etiantian.org
    
    去重计数
    [root@oldboy ~]# awk -F '/' '{print $3}' oldgirl.txt|sort|uniq -c
          1 mp3.etiantian.org
          2 post.etiantian.org
          3 www.etiantian.org
    

5. Linux信息显示与搜索文件

uname:显示系统信息

image.png

du:统计磁盘空间使用情况

image.png

[root@oldboy ~]# du -s                #<==显示当前目录的总大小。
100     .
[root@oldboy ~]# du -h                #<==-h参数会换算成K、M、G这种易读易理解的结果。
100K    .
[root@oldboy ~]# du -sh                #<==-sh是常用的命令组合,也是推荐大家使用的方法。
100K
[root@oldboy ~]# du -sh /usr/local/  #<==显示指定目录的总大小。
132K    /usr/local/
echo:显示一行文本

image.png


将单行文本输入到某个文件中。

[root@oldboy ~]# echo "hello world" >>hello.txt      #<==使用追加重定向符号“>>”
                                                    将文本写入文件。
[root@oldboy ~]# cat hello.txt
hello world
which:显示命令的全路径

image.png

[root@oldboy ~]# echo $PATH      #<==先查看环境变量。
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@oldboy ~]# which date      #<==查看date命令的全路径。
/bin/date
[root@oldboy ~]# which which      #<==如果对指定命令设置了别名,那么使用which功能还将
                                  会显示别名的情况。
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
        /usr/bin/which
[root@oldboy ~]# which cd      #<== Bash内置命令无法使用which。
/usr/bin/which: no cd in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
whereis:显示命令及其相关文件全路径

image.png

将相关的文件都查找出来。

[root@oldboy ~]# whereis svn
svn: /usr/bin/svn /usr/share/man/man1/svn.1.gz
[root@oldboy ~]# whereis -b svn  #<==只查找可执行文件。
svn: /usr/bin/svn
[root@oldboy ~]# whereis -m svn  #<==只查找man帮助文件。
svn: /usr/share/man/man1/svn.1.gz
[root@oldboy ~]# whereis -s svn  #<==只查找源代码文件。
svn:                             #<==没有找到相应的文件。

6. 文件备份与压缩命令

tar:打包备份

image.png

压缩:常用的打包命令组合zcvf


[root@oldboy ~]# mkdir -p /var/www/html/oldboy/test      #<==先生成测试文件。
[root@oldboy ~]# touch /var/www/html/{1..10}.html
[root@oldboy ~]# ls /var/www/html/
10.html  1.html  2.html  3.html  4.html  5.html  6.html  7.html  8.html  9.html  oldboy
[root@oldboy ~]# cd /var/www/      #<==进入到目标目录的上一级目录进行打包。
[root@oldboy www]# ls
html
[root@oldboy www]# tar zcvf www.tar.gz ./html/            #<==选项v会显示打包的过程,大家需要记住常用的打包命令组合zcvf,如果不想显示打包过程,则可以省略v选项,即选项组合为zcf。
./html/
./html/10.html
./html/8.html
./html/1.html
./html/7.html
./html/5.html
./html/3.html
./html/9.html
./html/oldboy/
./html/oldboy/test/
./html/2.html
./html/4.html
./html/6.html
[root@oldboy www]# ll -h www.tar.gz
-rw-r--r-- 1 root root 260 Nov 18 17:26 www.tar.gz
解开压缩包


[root@oldboy /]# tar zxvf www.tar.gz -C /tmp/ #<==选项C指定解压路径,若不加C则解
                                               压到当前目录。
./html/
./html/10.html
./html/8.html
./html/1.html
./html/7.html
……
[root@oldboy www]# ls /tmp/html/
10.html  1.html  2.html  3.html  4.html  5.html  6.html  7.html  8.html  9.html  oldboy [root@oldboy /]# tar xf www.tar.gz -C /tmp/ #<==如果不想看到太多的输出,则可以去掉v选项,功能不受影响。同时z选项也可以省略,只要涉及解压的操作,tar命令都能自动识别压缩包的压缩类型,但是压缩时必须要加上z选项。
说明:tar xfC www.tar.gz /tmp/这种格式也可以,但是没有上面的命令直观好记忆。
gzip:压缩或解压文件

image.png

把目录下的每个文件都压缩成单独的.gz文件。

[root@oldboy html]# ls
10.html  1.html  2.html  3.html  4.html  5.html  6.html  7.html  8.html  9.html  oldboy
[root@oldboy html]# gzip *.html #<==使用gzip命令压缩当前目录下所有以“.html”结尾
                                   的文件。
[root@oldboy html]# ls
10.html.gz  2.html.gz  4.html.gz  6.html.gz  8.html.gz  oldboy
1.html.gz   3.html.gz  5.html.gz  7.html.gz  9.html.gz  #<==“.gz”后缀是gzip
                                                      命令自动添加的。
#<==gzip命令的缺点是压缩后源文件不见了,它的特性是压缩、解压都会自动删除源文件。
解压文件,并显示解压过程。


[root@oldboy html]# gzip -dv *.gz  #<==使用-d参数解压文件,使用-v参数显示解压过程。
10.html.gz:       0.0% -- replaced with 10.html
1.html.gz:        0.0% -- replaced with 1.html
2.html.gz:        0.0% -- replaced with 2.html
3.html.gz:        0.0% -- replaced with 3.html
4.html.gz:        0.0% -- replaced with 4.html
5.html.gz:        0.0% -- replaced with 5.html
6.html.gz:        0.0% -- replaced with 6.html
7.html.gz:        0.0% -- replaced with 7.html
8.html.gz:        0.0% -- replaced with 8.html
9.html.gz:        0.0% -- replaced with 9.html
[root@oldboy html]# ls   #<==查看解压后的结果,gz文件不存在了,只剩下html文件。
10.html  1.html  2.html  3.html  4.html  5.html  6.html  7.html  8.html  9.html  oldboy
zip:打包和压缩文件

image.png

压缩文件

[root@oldboy tmp]# cp /etc/services .
[root@oldboy tmp]# ll -h
total 628K
-rw-r--r-- 1 root root 626K Nov 23 11:37 services
[root@oldboy tmp]# zip services.zip ./services   #<==格式:zip 压缩包名 被压缩的文件。
  adding: services (deflated 80%)              #<==deflated压缩率。
[root@oldboy tmp]# ll -h
total 756K
-rw-r--r-- 1 root root 626K Nov 23 11:37 services
-rw-r--r-- 1 root root 125K Nov 23 11:37 services.zip
压缩目录

[root@oldboy tmp]# cd /
[root@oldboy /]# zip tmp.zip ./tmp/
    adding: tmp/ (stored 0%)            #<==这样只是压缩目录这一个文件,目录下的文件
                                        没有压缩。
[root@oldboy /]# zip -r tmp.zip ./tmp/      #<==使用-r选项递归压缩。
updating: tmp/ (stored 0%)
    adding: tmp/services.zip (stored 0%)
    adding: tmp/services (deflated 80%)
    adding: tmp/.ICE-unix/ (stored 0%)
unzip:解压zip文件

image.png

常规解压文件的例子

[root@oldboy /]# unzip tmp.zip    #<==在根下直接解压文件,因为源文件还存在,因此会出
                                    现下面的提示。
Archive:  tmp.zip
replace tmp/services.zip? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
#<==是否替换文件,y是  n否    A所有文件都替换   N所有文件都不替换   r重命名
 extracting: tmp/services.zip
replace tmp/services? [y]es, [n]o, [A]ll, [N]one, [r]ename: n  #<==输入n取消。
[root@oldboy /]# unzip -v tmp.zip #<==解压时显示一些信息。
Archive:  tmp.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
       0  Stored        0   0% 11-23-2015 12:00 00000000  tmp/
  127362  Stored   127362   0% 11-23-2015 11:37 731c1fc9  tmp/services.zip
  641020  Defl:N   127196  80% 11-23-2015 11:37 33bd3343  tmp/services
       0  Stored        0   0% 11-23-2015 10:33 00000000  tmp/.ICE-unix/
--------          -------  ---                            -------
  768382           254558  67%                            4 files
[root@oldboy /]# unzip -o tmp.zip  #<==解压时不提示是否覆盖。
指定解压目录解压文件


[root@oldboy /]# unzip -d /tmp tmp.zip      #<==可以使用-d选项接目录来指定解压目录。
Archive:  tmp.zip
   creating: /tmp/tmp/
 extracting: /tmp/tmp/services.zip
  inflating: /tmp/tmp/services
   creating: /tmp/tmp/.ICE-unix/
[root@oldboy /]# tree /tmp            #<==解压成功。
/tmp
├── services
├── services.zip
└── tmp
    ├── services
    └── services.zip
1 directory, 4 files
scp:远程文件复制

image.png

推送(从本地服务器复制到远程服务器)文件或目录


[root@oldboy ~]# ll -h /etc/services                 #<==这是将要复制的文件。
-rw-r--r--. 1 root root 626K Oct  2  2013 /etc/services
[root@oldboy ~]# scp /etc/services 10.0.0.9:/tmp
#<==scp 传送的文件名 目标主机IP地址:想要传到的目录。
The authenticity of host '10.0.0.9 (10.0.0.9)' can't be established.
RSA key fingerprint is cc:7c:98:b9:be:23:ea:93:98:c9:01:b2:6c:c4:6a:8f.
Are you sure you want to continue connecting (yes/no)? yes      #<==第一次scp就和SSH第一次登录一样。
Warning: Permanently added '10.0.0.9' (RSA) to the list of known hosts.
root@10.0.0.9's password:                              #<==此处需要输入远
                                                           程机器密码。
services                                        100%  626KB 626.0KB/s   00:00
[root@linux-node3 ~]# ll -h /tmp/services                  #<==这是10.0.0.9的
                                                           远程主机窗口。
-rw-r--r-- 1 root root 626K Nov 23 16:55 /tmp/services      #<==可以看到赋值后的
                                                           文件时间有变化。
[root@oldboy ~]# scp -p /etc/services 10.0.0.9:/tmp            #<==使用-p选项保持
                                                           文件属性传输。
root@10.0.0.9's password:
services                                        100%  626KB 626.0KB/s   00:00
[root@linux-node3 ~]# ll -h /tmp/services                  #<==这是10.0.0.9的
                                                           远程主机。
-rw-r--r-- 1 root root 626K Oct  2  2013 /tmp/services      #<==加-p,赋值后的文件时间属性保持不变。
[root@oldboy ~]# scp -p /tmp 10.0.0.9:/tmp
root@10.0.0.9's password:
/tmp: not a regular file                              #<==不能直接复制目录。
[root@oldboy ~]# scp -rp /tmp 10.0.0.9:/tmp            #<==需要使用-r选项复制目录,
                                                    选项记忆方法:人品rp。
root@10.0.0.9's password:
services.zip                                    100%  124KB 124.4KB/s   00:00
services                                        100%  626KB 626.0KB/s   00:00
services.zip                                    100%  124KB 124.4KB/s   00:00
services                                        100%  626KB 626.0KB/s   00:00
[root@linux-node3 ~]# tree /tmp/                  #<==在远程主机10.0.0.9下执
                                                   行tree。
/tmp/
├── services
├── tmp
│   ├── services
│   ├── services.zip
│   └── tmp
│       ├── services
│       └── services.zip
└── yum.log
2 directories, 6 files
从远程服务器将数据复制到本地服务器(拉取)

[root@oldboy ~]# cd /tmp
[root@oldboy tmp]# scp 10.0.0.9:/etc/services .      #<==与推送的命令顺序对调即可,从10.0.0.9主机上将/etc/services文件下载到当前目录。
root@10.0.0.9's password:
services                                        100%  626KB 626.0KB/s   00:00
[root@oldboy tmp]# scp -rp 10.0.0.9:/tmp .            #<==拉取10.0.0.9主机的tmp
                                                   目录到当前目录。
root@10.0.0.9's password:
services                                        100%  626KB 626.0KB/s   00:00
services.zip                                    100%  124KB 124.4KB/s   00:00
services                                        100%  626KB 626.0KB/s   00:00
services.zip                                    100%  124KB 124.4KB/s   00:00
services                                        100%  626KB 626.0KB/s   00:00

7. Linux用户管理

useradd:创建用户

1)使用useradd常规添加用户工作原理流程在使用useradd命令时,若不加任何参数选项,后面直接跟所添加的用户名,那么系统首先会读取/etc/login.defs(用户定义文件)和/etc/default/useradd(用户默认配置文件)文件中所定义的参数和规则,然后根据所设置的规则添加用户,同时还会向/etc/passwd(用户文件)和/etc/group(组文件)文件内添加新用户和新用户组记录,向/etc/shadow(用户密码文件)和/etc/gshadow(组密码文件)文件里添加新用户和组对应的密码信息的相关记录。同时系统还会根据/etc/default/useradd文件所配置的信息建立用户的家目录,并将/etc/skel中的所有文件(包括隐藏的环境配置文件)都复制到新用户的家目录中。

2)useradd不加选项-D的参数选项及说明

image.png

范例7-1:不加任何参数添加用户的例子


[root@oldboyedu ~]# useradd ett
[root@oldboyedu ~]# ls -ld /home/ett/
drwx------ 2 ett ett 4096 Jul 12 10:10 /home/ett/


创建用户的同时还会创建一个与用户名相同的用户组。

在这个例子中,我们添加了一个名为ett的系统用户,当查看/home/目录时,会发现系统自动建立了一个ett的目录,其就是用户登入后的起始目录,即家目录。下面再来查看/etc/passwd文件中有关新用户ett的记录:


[root@oldboyedu ~]# grep -w ett /etc/passwd
ett:x:506:506::/home/ett:/bin/bash  #<==这里的506:506就是根据/etc/login.defs内容预设的。

范例7-2:useradd的-g、-u参数,执行useradd[参数]username添加用户


[root@oldboy ~]# groupadd -g 801 sa         #<==创建用户组sa并指定gid为801,这个
                                             是groupadd命令的用法,后面会讲到。
[root@oldboy ~]# useradd -g sa -u 901 oldgirl   #<==创建用户oldgirl属于sa组,uid为901。
[root@oldboy ~]# id oldgirl                 #<==查看用户和用户组的基本信息。
uid=901(oldgirl) gid=801(sa) groups=801(sa) #<==可以看出,这里符合创建的需求。
usermod:修改用户信息

image.png

userdel:删除用户

image.png

不加参数删除用户zuma。

[root@oldboy ~]# tail -4 /etc/passwd            #<==当前系统有4个多余的用户,准备删除。
oldgirl:x:901:801::/home/oldgirl:/bin/bash
tingting:x:1001:1001::/home/tingting:/sbin/nologin
inca:x:888:1002:TmpUser:/home/inca:/sbin/nologin
zuma:x:1002:1003::/home/zuma:/bin/sh
[root@oldboy ~]# ll /home/zuma/ -ld                #<==查看zuma用户的家目录。
drwx------. 2 zuma zuma 59 Jul 12 06:34 /home/zuma/
[root@oldboy ~]# userdel zuma                     #<==删除zuma用户。
[root@oldboy ~]# grep -w zuma /etc/passwd      #<==查看删除后的情况。
[root@oldboy ~]# ll /home/zuma/ -ld             #<==zuma家目录依然存在。
drwx------. 2 1002 1003 59 Jul 12 06:34 /home/zuma/

加-r参数删除用户及家目录。

[root@oldboy ~]# ls -ld /home/oldgirl/                #<==查看inca用户的家目录。
drwx------. 2 oldgirl sa 59 Jul 12 05:58 /home/oldgirl/
[root@oldboy ~]# grep -w oldgirl /etc/passwd      #<==查看oldgirl的用户信息。
oldgirl:x:901:801::/home/oldgirl:/bin/bash
[root@oldboy ~]# userdel -r oldgirl            #<==带-r参数删除oldgirl用户。
[root@oldboy ~]# grep -w oldgirl /etc/passwd      #<==用户信息没了。
[root@oldboy ~]# ls -ld /home/oldgirl/
ls: cannot access /home/oldgirl/: No such file or directory
                                          #<==oldgirl用户的家目录也没了。
groupadd:创建新的用户组

image.png

范例7-9:指定gid添加用户组的例子。

[root@oldboy ~]# groupadd -g 123 test1  #<==添加GID为123的test1用户组。
[root@oldboy ~]# tail -1 /etc/group
test1:x:123:
[root@oldboy ~]# tail -1 /etc/gshadow
test1:!::
groupdel:删除用户组
范例7-10:删除用户组的例子。


[root@oldboy ~]# groupdel root      #<==删除root用户组失败,因为root用户还存在。
groupdel: cannot remove the primary group of user 'root'
[root@oldboy ~]# groupdel test      #<==删除oldboy用户成功。
[root@oldboy ~]# grep -w test /etc/group 
passwd:修改用户密码

image.png

范例7-11:修改用户密码的例子。


[root@oldboy ~]# passwd      #<==修改当前用户root自身的密码。
Changing password for user root.
New password:            #<==输入密码123456,系统不会输出用户输入的密码。
BAD PASSWORD: it is too simplistic/systematic
                        #<==如果密码过于简单,则会给出警告,但不会阻止。
BAD PASSWORD: is too simple
Retype new password:      #<==再次输入密码123456。
passwd: all authentication tokens updated successfully. #<==还是设置成功了。
2)设置及修改普通用户的密码:


[root@oldboy ~]# useradd oldgirl  #<==添加oldgirl用户。
[root@oldboy ~]# passwd oldgirl      #<==为oldgirl用户添加密码。
Changing password for user oldgirl.
New password:                    #<==输入密码123456。
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:            #<==再次输入密码123456。
passwd: all authentication tokens updated successfully. #<==设置成功。
[root@oldboy ~]# su - oldgirl      #<==su命令可以切换用户身份,后面会讲解。
[oldgirl@oldboy ~]$ whoami      #<==当前用户为oldgirl。
oldgirl
[oldgirl@oldboy ~]$ passwd      #<==修改用户自身的密码。
Changing password for user oldgirl.
Changing password for oldgirl.
(current) UNIX password:       #<==先输入用户的当前密码。
New password:                  #<==输入新密码。
BAD PASSWORD: it is too short       #<==如果密码太短则不允许设置(root用户修改密码只是警告)
New password: 
BAD PASSWORD: it is too short
New password:                  #<==必须设置足够复杂的密码。
Retype new password:            #<==重复设置。
passwd: all authentication tokens updated successfully.

范例7-14:要求oldgirl用户7天内不能更改密码,60天以后必须修改密码,过期前10天通知用户,过期后30天后禁止用户登录。


[root@oldboy ~]# passwd -n 7 -x 60 -w 10 -i 30 oldgirl  #<==参数含义详见7.6.1节的表7-7。
Adjusting aging data for user oldgirl.
passwd: Success
[root@oldboy ~]# passwd -S oldgirl
oldgirl PS 2017-07-12 7 60 10 30 (Password set, SHA512 crypt.)
[root@oldboy ~]# chage -l oldgirl
Last password change      : Jul 11, 2017
Password expires            : Sep 09, 2017
Password inactive            : Oct 09, 2017 #<==过期后30天后禁止用户登录,-i控制。
Account expires            : never
Minimum number of days between password change      
                        : 7  #<==7天内不能更改密码,-n控制。
Maximum number of days between password change
                        : 60 #<==60天以后必须修改密码,-x控制。
Number of days of warning before password expires
                        : 10 #<==过期前10天通知用户,-w控制。
su:切换用户

image.png

范例7-19:切换用户例子。

[oldboy@oldboy ~]$ whoami   #<==当前登录的为普通用户oldboy。
oldboy
[oldboy@oldboy ~]$ su  root #<==切换用户,root可省略。
Password: 
[root@oldboy oldboy]# pwd   #<==查看当前路径。
/home/oldboy
[root@oldboy oldboy]# env|egrep "USER|MAIL|PWD|LOGNAME" #<==查看环境变量。
USER=oldboy
MAIL=/var/spool/mail/oldboy
PWD=/home/oldboy
LOGNAME=oldboy
sudo:以另一个用户身份执行命令

image.png

范例7-23:查看用户被visudo授权后拥有的权限。

[oldboy@oldboy ~]$ ls /root
ls: cannot open directory /root: Permission denied 
#<==可以看到,oldboy用户是无法直接访问/root家目录的
[oldboy@oldboy ~]$ sudo ls /root
#<==如果授权配置中含有NOPASSWD,则执行时不会提示密码,否则会要求输入当前用户的密码。
anaconda-ks.cfg  install.log  install.log.syslog  
#<==通过sudo命令,使得oldboy用户具备了访问/root目录的权限。
who:显示已登录用户信息

image.png

范例7-27:显示已登录用户的信息的不同参数实践例子。

[root@oldboy ~]# who      #<==一般不需要任何参数就可以使用。
root     pts/0        2017-07-13 11:49 (10.0.0.1)
root     pts/1        2017-07-13 12:00 (10.0.0.1)
root     pts/2        2017-07-13 17:10 (10.0.0.1)
[root@oldboy ~]# who -b      #<==显示启动时间。
    system boot  2017-07-13 11:48
[root@oldboy ~]# who -d      #<==显示已退出的用户。
    pts/3        2017-07-13 17:46              2377 id=ts/3  term=0 exit=0
[root@oldboy ~]# who -l      #<==显示登录的进程。
LOGIN    tty1         2017-07-13 11:48              1326 id=1
LOGIN    tty2         2017-07-13 11:48              1328 id=2
LOGIN    tty3         2017-07-13 11:48              1330 id=3
LOGIN    tty4         2017-07-13 11:48              1332 id=4
LOGIN    tty5         2017-07-13 11:48              1334 id=5
LOGIN    tty6         2017-07-13 11:48              1340 id=6
[root@oldboy ~]# who -H         #<==显示标题。
NAME     LINE         TIME             COMMENT
root     pts/0        2017-07-13 11:49 (10.0.0.1)
root     pts/1        2017-07-13 12:00 (10.0.0.1)
root     pts/2        2017-07-13 17:10 (10.0.0.1)
范例7-28:显示最全的登录用户的信息。

[root@oldboy ~]# who -H -a   #<==使用-H参数显示标题,使用-a参数显示所有信息。
NAME       LINE         TIME             IDLE     PID COMMENT  EXIT
      system boot  2017-07-13 11:48
      run-level 3  2017-07-13 11:48
LOGIN      tty1         2017-07-13 11:48          1326 id=1
LOGIN      tty2         2017-07-13 11:48          1328 id=2
LOGIN      tty3         2017-07-13 11:48          1330 id=3
LOGIN      tty4         2017-07-13 11:48          1332 id=4
LOGIN      tty5         2017-07-13 11:48          1334 id=5
LOGIN      tty6         2017-07-13 11:48          1340 id=6
root     + pts/0        2017-07-13 11:49 00:05    1344 (10.0.0.1)
root     + pts/1        2017-07-13 12:00 00:02    1401 (10.0.0.1)
root     + pts/2        2017-07-13 17:10   .      2213 (10.0.0.1)
           pts/3        2017-07-13 17:46          2377 id=ts/3  term=0 exit=0

8. Linux磁盘与文件管理命令

fdisk:磁盘分区工具

image.png


范例8-1:显示磁盘分区列表(-l参数)例子。

[root@oldboy ~]# fdisk -l                 #<==查看当前系统所有磁盘的分区信息。
Disk /dev/sda: 8589 MB, 8589934592 bytes  #<==磁盘/dev/sda的大小。
255 heads, 63 sectors/track, 1044 cylinders   #<==255个虚拟磁头,63个扇区/磁道,
                                           1044个柱面。
Units=cylinders of 16065*512=8225280 bytes  #<==一个柱面大小8225280 bytes。
Sector size (logical/physical): 512 bytes / 512 bytes  #<==每个扇区的字节数。
I/O size (minimum/optimal): 512 bytes / 512 bytes      #<==每次读写的字节数。
Disk identifier: 0x00048cb1
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      102400   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              13         854     6749184   83  Linux
Partition 2 does not end on cylinder boundary.
/dev/sda3             854        1045     1536000   82  Linux swap / Solaris

Device:分区,这里有三个分区;
Boot:启动分区,用*表示的是启动分区;
Start:表示开始的柱面;
End:表示结束的柱面;
Blocks:block块数量;
Id:分区类型Id;
System:分区类型。
mount:挂载文件系统

image.png

image.png

范例8-26:对系统的光驱进行挂载。

[root@oldboy ~]# mount /dev/cdrom /mnt  #<==这里没有指定-t iso9660,但mount命令
                                            会自动识别。
mount: block device /dev/sr0 is write-protected, mounting read-only
                                        #<==提示设备写保护,只读挂载。
[root@oldboy ~]# ll -h /dev/cdrom 
lrwxrwxrwx 1 root root 3 Jan 17 11:42 /dev/cdrom -> sr0
                                        #<==cdrom是sr0的软链接。
[root@oldboy ~]# df -h  #<==查看是否挂载。
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2       6.3G  1.5G  4.5G  26% /
tmpfs           238M   12K  238M   1% /dev/shm
/dev/sda1        93M   27M   61M  31% /boot
/dev/sr0        4.4G  4.4G     0 100% /mnt   #<==已经挂载成功。
[root@oldboy ~]# ls /mnt/                    #<==查看光盘的内容。
CentOS_BuildTag  GPL    Packages    RPM-GPG-KEY-CentOS-6    RPM-GPG-KEY-CentOS-Testing-6  EFI    images    RELEASE-NOTES-en-US.html
[root@oldboy ~]# umount /mnt #<==执行卸载操作,保留挂载点,后面可以继续用来进行挂载测试。
umount:卸载文件系统

image.png


范例8-30:卸载已挂载的光盘。

[root@oldboy ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2       6.3G  1.6G  4.4G  26% /
tmpfs           491M     0  491M   0% /dev/shm
/dev/sda1        93M   27M   61M  31% /boot
/dev/sr0        4.4G  4.4G     0 100% /mnt  #<==请确认已经挂载,然后再使用卸载命令。
[root@oldboy ~]# umount /mnt  #<==接挂载点就可以卸载,umount /dev/cdrom这种卸载
                                  方式也可以。
[root@oldboy ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2       6.3G  1.6G  4.4G  26% /
tmpfs           491M     0  491M   0% /dev/shm
/dev/sda1        93M   27M   61M  31% /boot
范例8-31:生产场景下强制卸载的例子。

[root@oldboy mnt]# cd /mnt
[root@oldboy mnt]# umount /mnt/    #<==因为当前在mnt目录中,所以无法卸载,此处执行
                                       方法一,退出当前目录卸载。
umount: /mnt: device is busy.
   (In some cases useful info about processes that use
    the device is found by lsof(8) or fuser(1)) 
[root@oldboy mnt]# umount -lf /mnt/  #<==方法二:使用-lf参数进行强制卸载。
[root@oldboy mnt]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2       6.3G  1.6G  4.4G  26% /
tmpfs           491M     0  491M   0% /dev/shm
/dev/sda1        93M   27M   61M  31% /boot  #<==已经看不到挂载的内容了。
df:报告文件系统磁盘空间的使用情况

image.png

范例8-32:显示磁盘的使用情况

[root@oldboy ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda2        6512168 2554936   3619776  42% /
tmpfs             243112       0    243112   0% /dev/shm
/dev/sda1          95054   27599     62335  31% /boot
范例8-34:了解参数-h的用法。

[root@oldboy ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2       6.3G  2.5G  3.5G  42% /
tmpfs           238M     0  238M   0% /dev/shm
/dev/sda1        93M   27M   61M  31% /boot
范例8-37:参数-T列出了文件系统的类型。

[root@oldboy ~]# df -T
Filesystem     Type  1K-blocks    Used Available Use% Mounted on
/dev/sda2      ext4    6512168 2554924   3619788  42% /
tmpfs          tmpfs    243112       0    243112   0% /dev/shm
/dev/sda1      ext4      95054   27599     62335  31% /boot

9. Linux进程管理命令

ps:查看进程

image.png

范例9-1:ps命令不接任何参数。

[root@oldboy ~]# ps
   PID TTY          TIME CMD
  2000 pts/1    00:00:00 bash
  2080 pts/1    00:00:00 ps
范例9-2:ps命令常用操作组合(命令1)

 [root@oldboy ~]# ps -ef  #<== UNIX格式参数,使用-e参数显示所有进程,使用-f参数
                               额外显示UID、PPID、C与STIME栏位。
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 11:18 ?        00:00:01 /sbin/init
root          2      0  0 11:18 ?        00:00:00 [kthreadd]
root          3      2  0 11:18 ?        00:00:00 [migration/0]
root          4      2  0 11:18 ?        00:00:00 [ksoftirqd/0]
root          5      2  0 11:18 ?        00:00:00 [stopper/0]
root          6      2  0 11:18 ?        00:00:00 [watchdog/0]
root          7      2  0 11:18 ?        00:00:25 [events/0]
root          8      2  0 11:18 ?        00:00:00 [events/0]
root          9      2  0 11:18 ?        00:00:00 [events_long/0]
root         10      2  0 11:18 ?        00:00:00 [events_power_ef]
root         11      2  0 11:18 ?        00:00:00 [cgroup]
root         12      2  0 11:18 ?        00:00:00 [khelper]
root         13      2  0 11:18 ?        00:00:00 [netns]
root         14      2  0 11:18 ?        00:00:00 [async/mgr]
root         15      2  0 11:18 ?        00:00:00 [pm]
root         16      2  0 11:18 ?        00:00:00 [sync_supers]
…………

输出信息中各列的说明如下。
·UID:进程被该UID所拥有。
·PID:进程的标识号。
·PPID:进程的父进程的标识号。
·C:CPU使用的资源百分比。
·STIME:进程开始的时间。
·TTY:该进程是在哪个终端机上面运作,若与终端机无关,则显示“?”
ps与grep的组合用法,用于查找特定进程,比如查找sshd进程等

[root@oldboy ~]# ps -ef|grep ssh  #<==使用grep查找关键字ssh。
root       1297      1  0 11:18 ?        00:00:00 /usr/sbin/sshd
root       1945   1297  0 16:18 ?        00:00:00 sshd: root@pts/0 
root       1998   1297  0 16:26 ?        00:00:00 sshd: root@pts/1 
root       2100   2000  0 17:10 pts/1    00:00:00 grep ssh
范例9-3:ps命令常用操作组合(命令2)。

 [root@oldboy ~]# ps aux  #<== BSD格式参数,使用a选项和x选项显示所有进程,
                               使用u选项显示进程的用户信息。
USER     PID %CPU %MEM    VSZ   RSS TTY   STAT START   TIME COMMAND
root       1  0.0  0.3  19232  1512 ?     Ss   11:18   0:01 /sbin/init
root       2  0.0  0.0      0     0 ?     S    11:18   0:00 [kthreadd]
root       3  0.0  0.0      0     0 ?     S    11:18   0:00 [migration/0]
root       4  0.0  0.0      0     0 ?     S    11:18   0:00 [ksoftirqd/0]
root       5  0.0  0.0      0     0 ?     S    11:18   0:00 [stopper/0]
root       6  0.0  0.0      0     0 ?     S    11:18   0:00 [watchdog/0]
root       7  0.1  0.0      0     0 ?     S    11:18   0:25 [events/0]
root       8  0.0  0.0      0     0 ?     S    11:18   0:00 [events/0]
root       9  0.0  0.0      0     0 ?     S    11:18   0:00 [events_long/0]
root      10  0.0  0.0      0     0 ?     S    11:18   0:00 [events_power_ef]
root      11  0.0  0.0      0     0 ?     S    11:18   0:00 [cgroup]
root      12  0.0  0.0      0     0 ?     S    11:18   0:00 [khelper]
root      13  0.0  0.0      0     0 ?     S    11:18   0:00 [netns]
root      14  0.0  0.0      0     0 ?     S    11:18   0:00 [async/mgr]
root      15  0.0  0.0      0     0 ?     S    11:18   0:00 [pm]
root      16  0.0  0.0      0     0 ?     S    11:18   0:00 [sync_supers]
…………
范例9-4:显示指定用户的相关进程信息。


[root@oldboy ~]# ps -u root  #<==UNIX格式参数,使用参数-u显示指定用户相关的进程信息。
   PID TTY          TIME CMD
1 ?        00:00:01 init
2 ?        00:00:00 kthreadd
3 ?        00:00:00 migration/0
4 ?        00:00:00 ksoftirqd/0
5 ?        00:00:00 stopper/0
6 ?        00:00:00 watchdog/0
7 ?        00:00:25 events/0
…………
范例9-5:以详细的格式显示进程状况。

[root@oldboy ~]# ps -l  #<== UNIX格式参数,使用参数-l以详细的格式显示进程的状况。
F S   UID    PID   PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0   2000   1998  0  80   0 - 27085 wait   pts/1    00:00:00 bash
4 R     0   2101   2000  0  80   0 - 27033 -      pts/1    00:00:00 ps
范例9-6:显示进程树。

[root@oldboy ~]# ps -eH  #<==UNIX格式参数,使用-e参数显示所有进程,使用-H参数显示
                             进程树。
   PID    TTY          TIME CMD
…………
   531    ?        00:00:00   udevd
   954    ?        00:00:00     udevd
  1277    ?        00:00:00   rsyslogd
  1297    ?        00:00:00   sshd
  1945    ?        00:00:00     sshd
  1947    pts/0    00:00:00       bash
  1969    pts/0    00:00:00         man
  1972    pts/0    00:00:00           sh
  1973    pts/0    00:00:00             sh
  1977    pts/0    00:00:00               less
  1998    ?        00:00:00     sshd
  2000    pts/1    00:00:00       bash
  2387    pts/1    00:00:00         ps
  1309   1309   1309 ?        00:00:00   crond
…………
[root@oldboy ~]# ps axf  #<==BSD格式参数,使用a和x参数显示所有进程,使用f参数显示进程树。
PID TTY    STAT    TIME COMMAND
 2 ?        S      0:00 [kthreadd]
 3 ?        S      0:00  \_ [migration/0]
 4 ?        S      0:00  \_ [ksoftirqd/0]
 5 ?        S      0:00  \_ [stopper/0]
 6 ?        S      0:00  \_ [watchdog/0]
 7 ?        S      0:11  \_ [events/0]
 8 ?        S      0:00  \_ [events/0]
 9 ?        S      0:00  \_ [events_long/0]
10 ?        S      0:00  \_ [events_power_ef]
…………
kill:终止进程

image.png

范例9-13:列出所有信号的名称。

[root@oldboy ~]# kill -l   #<==参数-l显示系统的所有信号。
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1
11) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR
31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX
[root@oldboy ~]# kill -l SIGKILL  #<==可以使用-l参数对信号名和数字信号互换。
9
[root@oldboy ~]# kill -l 9
KILL

image.png


范例9-14:终止进程

kill指令默认使用的信号为15,用于结束进程。如果进程忽略此信号,则可以使用信号9强制终止进程。

一般是先通过ps等命令获取到要终止进程的进程号,然后直接使用“kill进程号”就可以了。

kill 2203        #<==kill命令默认使用的信号为15,这种格式也是最常用的。
kill -s 15 2203  #<==这种格式使用-s参数明确指定发送值为15的信号,效果和kill 2203一样。
kill -15 2203    #<==上面的-s 15可以简写为-15。

如果用上面的方法还是无法终止进程,那么我们就可以用KILL(9)信号强制终止进程。

kill -9 2203      #<==信号9会强行终止进程,这会带来一些副作用,如数据丢失,或者终端无法恢复到正常状态等,因此应尽量避免使用,除非进程使用其他信号无法终止。

pkill:通过进程名终止进程

image.png

范例9-17:通过进程名终止进程。

[root@oldboy ~]# /etc/init.d/crond status   #<==查看定时任务程序的运行状态。
crond (pid  1339) is running...
[root@oldboy ~]# pkill crond  #<==终止定时任务进程。
[root@oldboy ~]# /etc/init.d/crond status   #<==再次查看定时任务程序的运行状态。
crond dead but subsys locked  #<==进程被终止。
范例9-18:通过终端名终止进程。

[root@oldboy ~]# w  #<==第二列TTY就是用户运行的终端
17:48:07 up  1:00,  2 users,  load average: 0.03, 0.02, 0.00 
USER     TTY      FROM           LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1      -               17:47    6.00s  0.02s  0.02s vim /etc/rc.local
root     pts/0    10.0.0.1       16:52    0.00s  0.01s  0.00s w
#<==说明:在tty1终端,用户正在编辑/etc/rc.local文件。
[root@oldboy ~]# pkill -t tty1  #<==使用-t选项杀死指定终端的进程。
范例9-19:通过用户名终止进程。

[root@oldboy ~]# w
 18:16:10 up 0 min,  2 users,  load average: 0.14, 0.05, 0.02
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    10.0.0.1         18:15    0.00s  0.01s  0.00s w
oldboy   pts/1    10.0.0.1         18:15    4.00s  0.00s  0.00s top
[root@oldboy ~]# pkill -u oldboy  #<==使用-u选项杀死指定用户的所有进程,最好还是同时再指定进程名去杀,以免误杀服务。
top:实时显示系统中各个进程的资源占用状况

image.png

image.png

范例9-20:显示进程信息。

[root@oldboy ~]# top #<==使用top命令通常不接任何参数,若需要其他更强大的功能则需要配合交互命令。
top - 02:39:54 up 1 day, 16:36,  2 users,  load average: 0.00, 0.00, 0.00
Tasks:  82 total,   1 running,  81 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:    486224k total,   401188k used,    85036k free,    42352k buffers
Swap:  1535996k total,        0k used,  1535996k free,   216936k cached
PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 1 root      20   0 19232 1588 1300 S  0.0  0.3   0:00.74 init
 2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd
 3 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/0
 4 root      20   0     0    0    0 S  0.0  0.0   0:00.13 ksoftirqd/0
……
范例9-22:将进程按照使用内存排序。


[root@oldboy ~]# top -a  #<==使用参数-a将进程按照使用内存排序。
top - 12:06:10 up 17:53,  3 users,  load average: 0.00, 0.00, 0.00
Tasks:  84 total,   1 running,  83 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:    486224k total,   321636k used,   164588k free,    39072k buffers
Swap:  1535996k total,        0k used,  1535996k free,   172136k cached
   PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
  6010 root      20   0 98376 4244 3280 S  0.0  0.9   0:00.08 sshd
  6039 root      20   0  185m 3360 2652 S  0.0  0.7   0:00.01 sudo
  6195 root      20   0  185m 3360 2652 S  0.0  0.7   0:00.06 sudo
  6665 root      20   0  185m 3360 2652 S  0.0  0.7   0:00.01 sudo
  6041 root      20   0  160m 2180 1728 S  0.0  0.4   0:00.00 su
……
范例9-23:以批处理模式显示进程信息。

[root@oldboy ~]# top -b  #<==使用参数-b可以看到命令执行结果不停地向下刷新。
……
 48579 oldboy    20   0  105m 1776 1416 S  0.0  0.4   0:00.00 bash
 48604 root      20   0  185m 3360 2652 S  0.0  0.7   0:00.01 sudo
 48606 root      20   0  160m 2176 1728 S  0.0  0.4   0:00.00 su
 48608 root      20   0  105m 1956 1436 S  0.0  0.4   0:00.00 bash
 48689 root      20   0     0    0    0 S  0.0  0.0   0:00.00 flush-8:0
 48701 root      20   0 15016 1156  912 R  0.0  0.2   0:00.00 top
^C   #<==退出使用快捷键Ctrl+C。
范例9-24:显示进程的完整路径。

[root@oldboy ~]# top -c   #<==使用参数-c显示进程的整个命令路径,而不是只显示命令名称。
top - 02:55:01 up 1 day, 16:51,  2 users,  load average: 0.00, 0.00, 0.00
Tasks:  78 total,   1 running,  77 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:    486224k total,   399916k used,    86308k free,    42388k buffers
Swap:  1535996k total,        0k used,  1535996k free,   216944k cached
   PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 16232 mysql     20   0  760m  26m 6028 S  0.3  5.6   0:13.13 /application/mysql-5.5.32/bin/mysqld --defaults-
1 root      20   0 19232 1588 1300 S  0.0  0.3   0:00.74 /sbin/init
2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 [kthreadd]
3 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 [migration/0]
4 root      20   0     0    0    0 S  0.0  0.0   0:00.14 [ksoftirqd/0]  
……
init:初始化Linux进程

init命令是Linux下的进程初始化工具,init进程是所有Linux进程的父进程,它的进程号为1。init命令的主要任务是依据配置文件“/etc/inittab”创建Linux进程。

范例9-43:切换运行级别。

[root@oldboy ~]# init 0  #<==关机,这里的数字含义请参考范例9-42的说明。
[root@oldboy ~]# init 6  #<==重启。
service:管理系统服务

service命令用于对系统服务进行管理,比如启动(start)、停止(stop)、重启(restart)、重新加载配置(reload)、查看状态(status)等,该命令在CentOS 7里被systemctl取代。

image.png

范例9-44:查看当前服务状态。

[root@oldboy ~]# service --status-all  #<==显示所有服务状态。
abrt-ccpp hook is not installed
abrtd is stopped
abrt-dump-oops is stopped
acpid is stopped
atd is stopped
auditd is stopped
cpuspeed is stopped
crond (pid  1332) is running...
hald is stopped
ip6tables: Firewall is not running.
iptables: Firewall is not running.
irqbalance is stopped
……
范例9-45:管理系统服务。

[root@oldboy ~]# service crond  #<==命令语句没有敲完时会显示帮助信息,crond是定时
                      任务服务名。
Usage: /etc/init.d/crond        #<==/etc/init.d/crond等同于service crond。
{start|stop|status|restart|condrestart|try-restart|reload|force-reload}
[root@oldboy ~]# service crond stop   #<==停止服务。
Stopping crond:                                            [  OK  ]
[root@oldboy ~]# service crond start   #<==启动服务。
Starting crond:                                            [  OK  ]
[root@oldboy ~]# service crond restart #<==重启服务。
Stopping crond:                                            [  OK  ]
Starting crond:                                            [  OK  ]
[root@oldboy ~]# service crond status  #<==查看服务状态。
crond (pid  5692) is running...

在工作中,推荐使用/etc/init.d/crond这种格式管理系统服务,因为这种格式支持tab键补齐,如果你忘记了服务名的书写,那就可以使用tab键。

/etc/init.d/crond stop
/etc/init.d/crond start
/etc/init.d/crond restart
/etc/init.d/crond status

10. Linux网络管理

ifconfig:配置或显示网络接口信息

ifconfig命令用于配置网卡IP地址等网络参数或显示当前网络的接口状态,其类似于Windows下的ipconfig命令,这两个命令很容易混淆,读者需要区分一下。此外,ifconfig命令在配置网卡信息时必须以root用户的身份来执行。如果系统中没有ifconfig命令,那就需要安装一下,安装命令为yum-y install net-tools。

image.png

范例10-1:显示当前系统开启的所有网络接口信息。

[root@oldboy ~]# ifconfig  #<==显示系统中所有的网卡信息。
eth0      Link encap:Ethernet  HWaddr 00:0C:29:13:10:CF  
     inet addr:10.0.0.12  Bcast:10.0.0.255  Mask:255.255.255.0
     inet6 addr: fe80::20c:29ff:fe13:10cf/64 Scope:Link
     UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
     RX packets:7452 errors:0 dropped:0 overruns:0 frame:0
     TX packets:7415 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:1000 
     RX bytes:684295 (668.2 KiB)  TX bytes:2485901 (2.3 MiB)
lo        Link encap:Local Loopback  
     inet addr:127.0.0.1  Mask:255.0.0.0
     inet6 addr: ::1/128 Scope:Host
     UP LOOPBACK RUNNING  MTU:65536  Metric:1
     RX packets:32 errors:0 dropped:0 overruns:0 frame:0
     TX packets:32 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:0 
     RX bytes:2432 (2.3 KiB)  TX bytes:2432 (2.3 KiB)
     
     
     对于上面的命令结果,eth0表示第1块网卡,第2块网卡为eth1,依此类推;lo表示回环接口。下面以eth0的结果作进一步说明。
     第1行:显示连接类型为Ethernet(以太网),HWaddr表示硬件的MAC地址。
     第2行:依次显示网卡的IP地址(inet addr)、广播地址(Bcast)和子网掩码(Mask)。
     第3行:IPv6地址的配置信息,由于没有使用IPv6地址,因此这里没有IP地址显示。
     第4行:“UP”代表网卡的开启状态,“RUNNING”代表网卡上的网线处于连接状态,“MULTICAST”代表支持组播,“MTU:1500”表示最大传输单元为1500字节。
     第5、6行:显示了网卡接收、发送数据包的统计信息。
     第8行:显示了网卡接收、发送数据字节数的统计信息。
范例10-2:显示指定网卡的信息。

[root@oldboy ~]# ifconfig eth0  #<==命令接上网卡名可以指定显示该网卡的信息。
eth0      Link encap:Ethernet  HWaddr 00:0C:29:13:10:CF  
     inet addr:10.0.0.12  Bcast:10.0.0.255  Mask:255.255.255.0
     inet6 addr: fe80::20c:29ff:fe13:10cf/64 Scope:Link
     UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
……
netstat:查看网络状态

image.png

范例10-25:常用选项组合(一)。

[root@oldboy ~]# netstat -an  #<==常用组合-a和-n,显示所有的连接信息。
Active Internet connections (servers and established)  #<==活动的TCP/IP网络连接。
Proto Recv-Q Send-Q Local Address             Foreign Address         State
tcp        0      0 0.0.0.0:22                0.0.0.0:*               LISTEN
tcp        0      0 10.0.0.12:22              10.0.0.1:57106          ESTABLISHED 
tcp        0     64 10.0.0.12:22              10.0.0.1:58180          ESTABLISHED 
tcp        0      0 :::22                     :::*                    LISTEN
Active UNIX domain sockets (servers and established) #<==活动的unix socket连接。
Proto RefCnt Flags       Type       State         I-Node Path
unix  2      [ ACC ]     STREAM     LISTENING     8124   @/com/ubuntu/upstart
unix  2      [ ]         DGRAM                    8532   @/org/kernel/udev/udevd
unix  5      [ ]         DGRAM                    10447  /dev/log
unix  2      [ ]         DGRAM                    12427  
unix  2      [ ]         DGRAM                    10993  
unix  2      [ ]         DGRAM                    10521  
unix  3      [ ]         DGRAM                    8550   
unix  3      [ ]         DGRAM                    8549  

image.png

针对该命令的第6列内容进行了说明。

image.png

范例10-26:常用选项组合(二)。


[root@oldboy ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address   Foreign Address   State   PID/Program name
tcp        0    0 0.0.0.0:22        0.0.0.0:*         LISTEN   1314/sshd 
tcp        0    0 :::22            :::*              LISTEN   1314/sshd
范例10-29:统计各个状态的网络连接个数。

[root@Backend-184 ~]# netstat -n |awk '/^tcp/ {++oldboy[$NF]} END {for(a in oldboy) print a, oldboy[a]}'   #<==这个范例利用了awk数组的功能,awk的使用请参考本书第4章。
TIME_WAIT 6163
FIN_WAIT1 42
FIN_WAIT2 1056
ESTABLISHED 4542
SYN_RECV 53
LAST_ACK 30

ping:测试主机之间网络的连通性

image.png

范例10-33:测试到目标主机的网络连通性。


[root@oldboy ~]# ping www.oldboyedu.com  #<==ping命令直接接域名或IP,会一直显示ping的结果。
PING www.oldboyedu.com (101.200.195.98) 56(84) bytes of data.
#<==显示ping的域名及其IP地址,发送的是56字节的数据。
64 bytes from 101.200.195.98: icmp_seq=1 ttl=128 time=45.5 ms
#<==从目标主机收到的数据是64字节,icmp_seq是收到包的序列号,ttl是数据包的生存期,time是时延。
64 bytes from 101.200.195.98: icmp_seq=2 ttl=128 time=45.4 ms
64 bytes from 101.200.195.98: icmp_seq=3 ttl=128 time=45.5 ms
64 bytes from 101.200.195.98: icmp_seq=4 ttl=128 time=45.5 ms
^C  #<==直到Ctrl+C强制终止。
--- www.oldboyedu.com ping statistics ---#<==这里是ping的统计结果
5 packets transmitted, 4 received, 20% packet loss, time 4049ms #<==发了5个包,收到4个,丢失了20%的包,时间为4049ms。
rtt min/avg/max/mdev = 45.418/45.515/45.577/0.060 ms
#<==rtt是传输的时间延迟。min/avg/max/mdev==>最小/平均/最大/算术平均差。
范例10-35:使用ping参数的不同组合的例子。


[root@oldboy ~]# ping -c 3 -i 3 -s 1024 -t 255 www.oldboyedu.com
PING www.oldboyedu.com (101.200.195.98) 1024(1052) bytes of data.
1032 bytes from 101.200.195.98: icmp_seq=1 ttl=128 time=46.0 ms
1032 bytes from 101.200.195.98: icmp_seq=2 ttl=128 time=45.8 ms
1032 bytes from 101.200.195.98: icmp_seq=3 ttl=128 time=45.8 ms
--- www.oldboyedu.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 6053ms
rtt min/avg/max/mdev = 45.861/45.929/46.035/0.190 ms

以下是命令说明。
·-c 3:发送3次ICMP包。
·-i 3:每次发包时间间隔为3s。
·-s 1024:设置发送的数据包大小为1024字节。
·-t 255:设置发送数据包的ttl值为255。
telnet:远程登录主机

现在使用telnet命令的场景主要是判断远端服务器的端口是否开放


范例10-40:测试ssh端口是否开放。

[root@oldboy ~]# telnet 10.0.0.12 22  #<==这里的10.0.0.12换成读者自己的IP,22是SSH服务的默认端口号。
Trying 10.0.0.12...
Connected to 10.0.0.12.
Escape character is '^]'.
SSH-2.0-OpenSSH_5.3  #<==看到这种结果,就证明SSH服务的22端口已经开放了。
#<==此时命令行已经挂起了,不能再进行其他操作,Ctrl+C也无法退出。根据提示输入“Ctrl+]”,然后进入telnet命令行,输入quit就能退出。
^]
telnet> quit
Connection closed.
#<==输入有误需要退格使用Ctrl+Backspace或Ctrl+W。
ssh:安全地远程登录主机

image.png


范例10-47:远程登录服务器。

[root@oldboy ~]# ssh 10.0.0.29   #<==这是远程登录服务器的简写命令,等同于ssh -p 22 root@10.0.0.29
#<==下面四行内容只有在第一次连接远程服务器时会提示,再次连接就不会提示了。
The authenticity of host '10.0.0.29 (10.0.0.29)' can't be established.
RSA key fingerprint is cc:7c:98:b9:be:23:ea:93:98:c9:01:b2:6c:c4:6a:8f.
Are you sure you want to continue connecting (yes/no)? yes  #<==输入yes即可。
Warning: Permanently added '10.0.0.29' (RSA) to the list of known hosts.
root@10.0.0.29's password:   #<==如果省略用户,则默认是当前执行ssh命令的用户。此处输入远端服务器的密码123456,密码不可见。
#<==下面就登录到远程服务器了,然后我们快速查看一下IP地址。
Last login: Wed Feb  8 12:18:08 2017 from 10.0.0.1
[root@LNMP ~]# hostname -I  #<==查看IP地址。
10.0.0.29 172.16.1.29
[root@LNMP ~]# logout       #<==输入Ctrl+D注销登录。
Connection to 10.0.0.29 closed.

如果不想使用root用户登录远程服务器,那么我们可以明确指定登录用户,也可以同时指定端口:

[root@oldboy ~]# ssh -p 22 oldboy@10.0.0.29  #<==使用oldboy用户登录远程服务器,这个用户必须是远程服务器已有的用户,-p指定22端口。
oldboy@10.0.0.29's password:   #<==输入密码123456
[oldboy@LNMP ~]$ pwd           #<==查看当前所在的目录。
/home/oldboy
范例10-48:远程执行命令的例子。

[root@oldboy ~]# ssh 10.0.0.29  "free -m"  #<==直接将要远程执行的命令放置到最后即可,用引号更规范,这里是查看所在服务器的内存信息。
root@10.0.0.29's password:   #<==输入密码123456
        total       used       free     shared    buffers     cached
Mem:           981        229        752          0         34         58
-/+ buffers/cache:        135        845 
Swap:         1023          0       1023
wget:命令行下载工具

image.png


范例10-50:使用wget下载单个文件。

[root@mysql ~]# wget http://www.oldboyedu.com/favicon.ico  #<==wget接上下载地址即可。
--2015-04-18 13:50:03--  http://www.oldboyedu.com/favicon.ico
Resolving www.oldboyedu.com... 10.0.0.5
Connecting to www.oldboyedu.com|10.0.0.5|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1150 (1.1K) [image/x-icon]
Saving to: “favicon.ico”
100%[===============================================>] 1,150  --.-K/s  in 0s
2015-04-18 13:50:03 (63.3 MB/s) - “favicon.ico” saved [1150/1150]
范例10-51:使用-O选项指定下载文件的保存文件名。

[root@oldboy ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo #<==这是一个更新epel源的命令,将epel-6.repo下载并放入/etc/yum.repos.d/目录,改名为epel.repo。
范例10-52:通过--limit-rate限速下载。


[root@mysql ~]# wget --limit-rate=3k http://www.oldboyedu.com/favicon.ico  #<==使用--limit-rate参数设置最高下载速度为3K/s。
--2015-04-18 13:53:23--  http://www.oldboyedu.com/favicon.ico
Resolving www.oldboyedu.com... 10.0.0.5
Connecting to www.oldboyedu.com|10.0.0.5|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1150 (1.1K) [image/x-icon]
Saving to: “favicon.ico.1”
100%[============================>] 1,150  2.99K/s  in 0.4s  #<==下载速度接近3k。
2015-04-18 13:53:23 (2.99 KB/s) - “favicon.ico.1” saved [1150/1150]
范例10-54:使用wget-b后台下载文件。


[root@mysql ~]# wget -b http://www.oldboyedu.com/favicon.ico
Continuing in background, pid 18158.
Output will be written to “wget-log”.
[root@mysql ~]# tail wget-log  #<==查看下载进度的日志文件。
Resolving www.oldboyedu.com... 10.0.0.5
Connecting to www.oldboyedu.com|10.0.0.5|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1150 (1.1K) [image/x-icon]
Saving to: “favicon.ico.2”
0K .                                                     100%  227M=0s
2015-04-18 13:55:15 (227 MB/s) - “favicon.ico.2” saved [1150/1150]
nslookup:域名查询工具

image.png

范例10-65:交互模式。

[root@oldboy ~]# cat /etc/resolv.conf  #<==默认是从该文件读取DNS服务器地址的。
# Generated by NetworkManager
nameserver 10.0.0.2
[root@oldboy ~]# nslookup 
> www.oldboyedu.com        #<==符号“>”是nslookup命令的提示符。可以在此提示符下
                                输入要查询的域名信息进行查询。
Server:            10.0.0.2  #<==默认DNS服务器。
Address:      10.0.0.2#53      #<==上面的DNS服务器的IP地址与端口号。
Non-authoritative answer:  #<==非授权域名服务器的应答,说明本域名服务器给出的域名解析信息是从其他域名服务器那里查询所得到的信息,而非自己管理的域。
Name:      www.oldboyedu.com
Address: 101.200.195.98    #<==显示域名对应的IP地址。
采用非交互查模式,指定域名服务器地址,查询www.oldboyedu.com对应的域名记录:


[root@oldboy ~]# nslookup www.oldboyedu.com 223.5.5.5  #<==非交互查询www.oldboyedu.com域名。
Server:            223.5.5.5
Address:      223.5.5.5#53
Non-authoritative answer:
Name:      www.oldboyedu.com
Address: 101.200.195.98  #<==这里就是域名对应的IP。
tcpdump:监听网络流量

image.png

范例10-80:不加参数运行tcpdump命令监听网络。


[root@oldboy ~]# tcpdump   #<==默认情况下,直接启动tcpdump将监视第一个网络接口上所有流过的数据包。
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
10:35:26.660614 IP bogon.ssh > bogon.56423: Flags [P.], seq 659599530:659599738, ack 4154901889, win 634, length 208
10:35:26.661571 IP bogon.54493 > bogon.domain: 61300+ PTR? 1.0.0.10.in-addr.arpa. (39)
10:35:26.665393 IP bogon.domain > bogon.54493: 61300 1/0/0 PTR bogon. (58)
10:35:26.665614 IP bogon.43229 > bogon.domain: 30637+ PTR? 12.0.0.10.in-addr.arpa. (40)
10:35:26.668974 IP bogon.domain > bogon.43229: 30637 1/0/0 PTR bogon. (59)
10:35:26.669226 IP bogon.45649 > bogon.domain: 2242+ PTR? 2.0.0.10.in-addr.arpa. (39)
10:35:26.669322 IP bogon.ssh > bogon.56423: Flags [P.], seq 208:384, ack 1, win 634, length 176
10:35:26.669526 IP bogon.56423 > bogon.ssh: Flags [.], ack 384, win 256, length 0
……
^C  #<==tcpdump命令在运行期间可以使用组合键Ctrl+C终止程序。
847packets captured    #<==最后3行就是按Ctrl+C后输出的监听到的数据包汇总信息。
847packets received by filter
0 packets dropped by kernel
范例10-81:精简输出信息。

[root@oldboy ~]# tcpdump -q   #<==默认情况下,tcpdump命令的输出信息较多,为了显示精简的信息,可以使用-q选项。
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
11:40:08.498459 IP 10.0.0.12.ssh > 10.0.0.1.56425: tcp 208
11:40:08.498721 IP 10.0.0.1.56425 > 10.0.0.12.ssh: tcp 0
11:40:08.499061 IP 10.0.0.12.44779 > 10.0.0.2.domain: UDP, length 39
11:40:08.620262 IP 10.0.0.2.domain > 10.0.0.12.44779: UDP, length 39
……
^C
3128packets captured
3129packets received by filter
0 packets dropped by kernel
[root@oldboy ~]# tcpdump -c 5  #<==使用-c选项指定监听的数据包数量,这样就不需要使用Ctrl+C了。
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
12:14:38.307268 IP bogon.ssh > bogon.57697: Flags [P.], seq 3071612667:3071612875, ack 3264504201, win 634, length 208
12:14:38.307733 IP bogon.55287 > bogon.domain: 64552+ PTR? 1.0.0.10.in-addr.arpa. (39)
12:14:38.313788 ARP, Request who-has bogon tell bogon, length 46
12:14:38.313809 ARP, Reply bogon is-at 00:0c:29:13:10:cf (oui Unknown), length 28
12:14:38.313873 IP bogon.domain > bogon.55287: 64552 1/0/0 PTR bogon. (58)
5 packets captured
11 packets received by filter
0 packets dropped by kernel
范例10-82:监听指定网卡收到的数据包。

[root@oldboy ~]# tcpdump -i eth0  #<==使用-i选项可以指定要监听的网卡
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
11:34:51.216587 IP 10.0.0.12.ssh > 10.0.0.1.56425: Flags [P.], seq 3075433603: 3075433811, ack 1160620205, win 634, length 208
11:34:51.216767 IP 10.0.0.1.56425 > 10.0.0.12.ssh: Flags [.], ack 208, win 256, length 0
11:34:51.217058 IP 10.0.0.12.53851 > 10.0.0.2.domain: 49947+ PTR? 1.0.0.10.in-addr.arpa. (39)
11:34:51.337645 IP 10.0.0.2.domain > 10.0.0.12.53851: 49947 NXDomain 0/0/0 (39)
11:34:51.337798 IP 10.0.0.12.48236 > 10.0.0.2.domain: 31229+ PTR? 12.0.0.10.in-addr.arpa. (40)
……
11:34:51.487545 IP 10.0.0.12.ssh > 10.0.0.1.56425: Flags [P.], seq 47600:47872, ack 65, win 634, length 272
^C   
347 packets captured       
347 packets received by filter
0 packets dropped by kernel

以下是命令结果说明。
·11:34:51.216587:当前时间,精确到微秒。
·IP 10.0.0.12.ssh>10.0.0.1.56425:从主机10.0.0.12的SSH端口发送数据到10.0.0.1的56425端口,“>”代表数据流向。
·Flags[P.]:TCP包中的标志信息,S是SYN标志的缩写,F(FIN)、P(PUSH)、R(RST)、"."(没有标记)。
·seq:数据包中的数据的顺序号。
·ack:下次期望的顺序号。
·win:接收缓存的窗口大小。
·length:数据包长度。
范例10-83:监听指定主机的数据包。

[root@oldboy ~]# tcpdump -n  host 10.0.0.1  #<==使用-n选项不进行DNS解析,加快显示速度。监听指定主机的关键字为host,后面直接接主机名或IP地址即可。本行命令的作用是监听所有10.0.0.1的主机收到的和发出的数据包。
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
10:41:50.929114 IP 10.0.0.12.ssh > 10.0.0.1.56423: Flags [P.], seq 659941802:659942010, ack 4154903921, win 634, length 208
10:41:50.930970 IP 10.0.0.12.ssh > 10.0.0.1.56423: Flags [P.], seq 208:384, ack 1, win 634, length 176
10:41:50.931187 IP 10.0.0.1.56423 > 10.0.0.12.ssh: Flags [.], ack 384, win 255, length 0
10:41:50.931317 IP 10.0.0.12.ssh > 10.0.0.1.56423: Flags [P.], seq 384:640, ack 1, win 634, length 256
……
^C
736 packets captured
736 packets received by filter
0 packets dropped by kernel
[root@oldboy ~]# tcpdump -n src host 10.0.0.1  #<==只监听从10.0.0.1发出的数据包,即源地址为10.0.0.1,关键字为src(source,源地址)。
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
11:04:45.007219 IP 10.0.0.1.56423 > 10.0.0.12.ssh: Flags [.], ack 661112234, win 255, length 0
11:04:45.059696 IP 10.0.0.1.56423 > 10.0.0.12.ssh: Flags [.], ack 161, win 254, length 0
11:04:45.422776 IP 10.0.0.1.56423 > 10.0.0.12.ssh: Flags [.], ack 1169, win 256, length 0
11:04:45.474360 IP 10.0.0.1.56423 > 10.0.0.12.ssh: Flags [.], ack 1313, win 256, length 0
11:04:45.526768 IP 10.0.0.1.56423 > 10.0.0.12.ssh: Flags [.], ack 1457, win 255, length 0
11:04:45.577941 IP 10.0.0.1.56423 > 10.0.0.12.ssh: Flags [.], ack 1601, win 254, length 0
11:04:45.630143 IP 10.0.0.1.56423 > 10.0.0.12.ssh: Flags [.], ack 1745, win 254, length 0
11:04:45.680484 IP 10.0.0.1.56423 > 10.0.0.12.ssh: Flags [.], ack 1889, win 253, length 0
11:04:45.731739 IP 10.0.0.1.56423 > 10.0.0.12.ssh: Flags [.], ack 2033, win 253, length 0
……
^C
22 packets captured
22 packets received by filter
0 packets dropped by kernel
[root@oldboy ~]# tcpdump -n dst host 10.0.0.1 #<==只监听10.0.0.1收到的数据包,即目标地址为10.0.0.1,关键字为dst(destination,目的地)。
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
11:05:02.654070 IP 10.0.0.12.ssh > 10.0.0.1.56423: Flags [P.], seq 661117610:661117818, ack 4154935761, win 634, length 208
11:05:02.654279 IP 10.0.0.12.ssh > 10.0.0.1.56423: Flags [P.], seq 208:384, ack 1, win 634, length 176
11:05:02.655918 IP 10.0.0.12.ssh > 10.0.0.1.56423: Flags [P.], seq 384:544, ack 1, win 634, length 160
11:05:02.656932 IP 10.0.0.12.ssh > 10.0.0.1.56423: Flags [P.], seq 544:704, ack 1, win 634, length 160
11:05:02.657908 IP 10.0.0.12.ssh > 10.0.0.1.56423: Flags [P.], seq 704:864, ack 1, win 634, length 160
……
11:05:03.094028 IP 10.0.0.12.ssh > 10.0.0.1.56423: Flags [P.], seq 103392:103568, ack 129, win 634, length 176
^C
645 packets captured
645 packets received by filter
0 packets dropped by kernel
范例10-84:监听指定端口的数据包。


[root@oldboy ~]# tcpdump -nn port 22 #<==使用-n选项不进行DNS解析,但是其会将一些协议、端口进行转换,比如22端口转为ssh,读者可以对比查看范例10-4的输出结果。因此本例使用-nn选项。监听指定端口的关键字是port,后面接上端口号即可。
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
10:48:18.122973 IP 10.0.0.12.22 > 10.0.0.1.56423: Flags [P.], seq 660573578:660573786, ack 4154913361, win 634, length 208
10:48:18.123228 IP 10.0.0.12.22 > 10.0.0.1.56423: Flags [P.], seq 208:384, ack 1, win 634, length 176
10:48:18.123316 IP 10.0.0.1.56423 > 10.0.0.12.22: Flags [.], ack 384, win 254, length 0
10:48:18.124492 IP 10.0.0.12.22 > 10.0.0.1.56423: Flags [P.], seq 384:640, ack 1, win 634, length 256
10:48:18.125462 IP 10.0.0.12.22 > 10.0.0.1.56423: Flags [P.], seq 640:800, ack 1, win 634, length 160
10:48:18.125603 IP 10.0.0.1.56423 > 10.0.0.12.22: Flags [.], ack 800, win 253, length 0
^C
743 packets captured
744 packets received by filter
0 packets dropped by kernel
范例10-85:监听指定协议的数据包。

[root@oldboy ~]# tcpdump -n arp  #<==监听ARP数据包,因此表达式直接写arp即可。
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
16:48:08.075461 ARP, Request who-has 10.0.0.12 (00:0c:29:13:10:cf) tell 10.0.0.1, length 46
16:48:08.075490 ARP, Reply 10.0.0.12 is-at 00:0c:29:13:10:cf, length 28
^C
2 packets captured
2 packets received by filter
0 packets dropped by kernel
[root@oldboy ~]# tcpdump -n icmp  #<==监听icmp数据包(想要查看下面的监控数据,可以使用其他机器ping本机即可)
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
16:51:08.177903 IP 10.0.0.1 > 10.0.0.12: ICMP echo request, id 1, seq 11, length 40
16:51:08.177918 IP 10.0.0.12 > 10.0.0.1: ICMP echo reply, id 1, seq 11, length 40
16:51:09.180235 IP 10.0.0.1 > 10.0.0.12: ICMP echo request, id 1, seq 12, length 40
16:51:09.180249 IP 10.0.0.12 > 10.0.0.1: ICMP echo reply, id 1, seq 12, length 40
……
^C
8 packets captured
8 packets received by filter
0 packets dropped by kernel

11. Linux系统管理命令

lsof:查看进程打开的文件

lsof全名为list open files,也就是列举系统中已经被打开的文件,通过lsof命令,就可以根据文件找到对应的进程信息,也可以根据进程信息找到进程打开的文件。

image.png

范例11-1:显示使用文件的进程。

[root@oldboy ~]# lsof /var/log/messages
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
rsyslogd 1277 root    1w   REG    8,3     7323 795951 /var/log/messages
范例11-2:显示指定进程所打开的文件。

[root@oldboy ~]# lsof -c rsyslog  #<==使用-c选项显示指定进程所打开的文件。
COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF       NODE NAME
rsyslogd 1277 root  cwd    DIR            8,3     4096          2 /
rsyslogd 1277 root  rtd    DIR            8,3     4096          2 /
rsyslogd 1277 root  txt    REG                8,3   391360     527284 /sbin/rsyslogd
……
范例11-3:显示指定进程号所打开的文件。

[root@oldboy ~]# lsof -p 1277   #<==使用-p选项显示指定进程号所打开的文件。
COMMAND   PID USER   FD   TYPE   DEVICE SIZE/OFF       NODE NAME
rsyslogd 1277 root  cwd    DIR   8,3     4096          2 /
rsyslogd 1277 root  rtd    DIR   8,3     4096          2 /
rsyslogd 1277 root  txt    REG   8,3   391360     527284 /sbin/rsyslogd
rsyslogd 1277 root  mem     REG    8,3    27232    1046895 /lib64/rsyslog/imklog.so
……

 [root@oldboy ~]# lsof -i  #<==查看所有进程。
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd     1297 root    3u  IPv4  10428      0t0  TCP *:ssh (LISTEN)
sshd     1297 root    4u  IPv6  10430      0t0  TCP *:ssh (LISTEN)
sshd    11886 root    3r  IPv4  47229      0t0  TCP 10.0.0.12:ssh->10.0.0.1:54906 (ESTABLISHED)
sshd    12064 root    3r  IPv4  48871      0t0  TCP 10.0.0.12:ssh->10.0.0.1:62935 (ESTABLISHED)
[root@oldboy ~]# lsof -i tcp   #<==显示所有tcp网络连接的进程信息。
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd    1299 root    3u  IPv4  10445      0t0  TCP *:ssh (LISTEN)
sshd    1299 root    4u  IPv6  10447      0t0  TCP *:ssh (LISTEN)
sshd    1336 root    3r  IPv4  10585      0t0  TCP 10.0.0.12:ssh->10.0.0.1:62339 (ESTABLISHED)
[root@oldboy ~]# lsof -i :22  #<==显示端口为22的进程,这条命令很常用。
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd    1299 root    3u  IPv4  10445      0t0  TCP *:ssh (LISTEN)
sshd    1299 root    4u  IPv6  10447      0t0  TCP *:ssh (LISTEN)
sshd    1336 root    3r  IPv4  10585      0t0  TCP 10.0.0.12:ssh->10.0.0.1:62339 (ESTABLISHED)
[root@oldboy ~]# lsof -i tcp:22  #<==显示同时满足TCP和端口为22的进程。
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd    1299 root    3u  IPv4  10445      0t0  TCP *:ssh (LISTEN)
sshd    1299 root    4u  IPv6  10447      0t0  TCP *:ssh (LISTEN)
sshd    1336 root    3r  IPv4  10585      0t0  TCP 10.0.0.12:ssh->10.0.0.1:62339 (ESTABLISHED)
free:查看系统内存信息

image.png

范例11-8:查看系统内存。

[root@oldboy~]# free    #<==不加参数默认显示的是字节数,很难读懂。
             total       used       free     shared    buffers     cached
Mem:        486640     150828     335812        200      25736      38436
-/+ buffers/cache:      86656     399984 
Swap:      1048572          0    1048572 
[root@oldboy ~]# free -m  #<==使用-m选项,以MB为单位显示内存的使用情况。
             total       used       free     shared    buffers     cached
Mem:           475        147        327          0         25         37
-/+ buffers/cache:         84        390 
Swap:         1023          0       1023
[root@oldboy ~]# free -h   #<==使用-h选项,根据实际大小自动转换成KB、MB、GB单位,
                               显示内存的使用情况。
             total       used       free     shared    buffers     cached
Mem:          475M       147M       327M       200K        25M        37M
-/+ buffers/cache:        85M       390M 
Swap:         1.0G         0B       1.0G


针对上面的输出,有以下说明。
·Linux系统的特性是将不用的物理内存缓存起来,因此327MB不是系统的真实剩余内存。
·系统真正可用的内存为390MB。
·buffers为写入数据缓冲区。
·cache为读取数据的缓存区。
rpm:RPM包管理器

rpm命令的全称是Red Hat Package Manager(Red Hat包管理器),几乎所有的Linux发行版本都使用了这种形式的命令管理、安装、更新和卸载软件。

概括地说,rpm命令包含了五种基本功能(不包括创建rpm包):安装、卸载、升级、查询和验证。

image.png

yum:自动化RPM包管理工具

yum(Yellow dog Updater Modified)是多个Linux发行版的软件包管理器,例如Redhat RHEL、CentOS和Fedora。yum主要用于自动安装、升级rpm软件包,它能自动查找并解决rpm包之间的依赖关系。

image.png

image.png

范例11-54:安装httpd软件包。


[root@oldboy ~]# yum install httpd
Loaded plugins: fastestmirror, security
……
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.2.15-56.el6.centos.3 will be installed
--> Processing Dependency: httpd-tools = 2.2.15-56.el6.centos.3 for package: httpd-2.2.15-56.el6.centos.3.x86_64
--> Processing Dependency: apr-util-ldap for package: httpd-2.2.15-56.el6.centos.3.x86_64
--> Running transaction check
---> Package apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 will be installed
---> Package httpd-tools.x86_64 0:2.2.15-56.el6.centos.3 will be installed
--> Finished Dependency Resolution
……
Total download size: 928 k
Installed size: 3.1 M
Is this ok [y/N]:y  #<==如果执行命令不加-y选项,那么此处就需要输入y来确认安装,N表示不安装。
Downloading Packages:
(1/3): apr-util-ldap-1.3.9-3.el6_0.1.x86_64.rpm         |  15 kB  00:00
(2/3): httpd-2.2.15-56.el6.centos.3.x86_64.rpm          | 834 kB  00:00
(3/3): httpd-tools-2.2.15-56.el6.centos.3.x86_64.rpm    |  79 kB  00:00
……
Installed:
  httpd.x86_64 0:2.2.15-56.el6.centos.3
Dependency Installed:
  apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1  httpd-tools.x86_64 0:2.2.15-56.el6.centos.3
Complete!
范例11-55:常见yum命令的例子。

[root@oldboy ~]# yum list httpd   #<==检查httpd安装列表。
Loaded plugins: security
Installed Packages
httpd.x86_64            2.2.15-60.el6.centos.4
[root@oldboy ~]# yum search httpd   #<==搜索包含httpd字符串的软件包。
Loaded plugins: security
============= N/S Matched: httpd ============
iipsrv-httpd-fcgi.noarch : Apache HTTPD files for iipsrv
libmicrohttpd-devel.i686 : Development files for libmicrohttpd
libmicrohttpd-devel.x86_64 : Development files for libmicrohttpd
libmicrohttpd-doc.noarch : Documentation for libmicrohttpd
lighttpd-fastcgi.x86_64 : FastCGI module and spawning helper for lighttpd and PHP configuration
lighttpd-mod_authn_gssapi.x86_64 : Authentication module for lighttpd that uses GSSAPI
lighttpd-mod_authn_mysql.x86_64 : Authentication module for lighttpd that uses a MySQL database
lighttpd-mod_geoip.x86_64 : GeoIP module for lighttpd to use for location lookups
lighttpd-mod_mysql_vhost.x86_64 : Virtual host module for lighttpd that uses a MySQL database
httpd.x86_64 : Apache HTTP Server
httpd-devel.i686 : Development interfaces for the Apache HTTP server
httpd-devel.x86_64 : Development interfaces for the Apache HTTP server
httpd-itk.x86_64 : MPM Itk for Apache HTTP Server
httpd-manual.noarch : Documentation for the Apache HTTP server
httpd-tools.x86_64 : Tools for use with the Apache HTTP Server
libmicrohttpd.i686 : Lightweight library for embedding a webserver in applications
......
  Name and summary matches only, use "search all" for everything.
[root@oldboy ~]# yum grouplist   #<==查看已安装和未安装的包组。
Loaded plugins: security
Setting up Group Process
base/group_gz          | 226 kB     00:00     
epel/group_gz          | 150 kB     00:00     
Installed Groups:
   Additional Development
   Base
   Development tools
   Directory Client
......
   Security Tools
   Web Server
Available Groups:
   Backup Client
   Backup Server
   CIFS file server
   Client management tools
   Compatibility libraries
......
[root@oldboy ~]# yum groupinstall "SNMP Support" -y   #<==安装包组,从yum grouplist中查找。
...省略输出...

12. Linux系统常用内置命令

Linux里有一些特殊的命令,称为内置命令(直接内置在BASH解释器中),它们天生就与其他的普通命令不同,因为它们从系统启动成功的那一刻就已经在内存里安家了。当其他普通命令还在慢悠悠地从磁盘上读取程序文件加载到内存中时,内置命令已经早早地从内存中奔赴到CPU中,因此内置命令的执行效率要远远高于普通命令。当然这其中有些命令更为特殊,比如echo命令、pwd命令、kill命令等,它们既有内置命令版本,又有普通命令版本,两种格式的命令其用法是一样的。我们能在磁盘上找到它们的程序文件/bin/echo、/bin/pwd、/bin/kill。一般情况下,优先使用内置命令,除非你显式地执行/bin/echo这种全路径命令。

image.png

image.png

image.png