一、MD5摘要算法
MD5即Message-Digest Algorithm 5(信息-摘要算法5),用于确保信息传输完整一致。如果一个文件的内容不改变,则该文件的MD5值也不会改变,(即使文件名改变,该MD5值也不会改变,MD5主要是对文件内容的摘要),通常用于在传输文件的时候校验文件的完整性。
二、md5sum命令详解
[root@master01 ~]# md5sum --help
Usage: md5sum [OPTION]... [FILE]...
Print or check MD5 (128-bit) checksums.
With no FILE, or when FILE is -, read standard input.
-b, --binary read in binary mode
-c, --check read MD5 sums from the FILEs and check them
--tag create a BSD-style checksum
-t, --text read in text mode (default)
Note: There is no difference between binary and text mode option on GNU system.
The following four options are useful only when verifying checksums:
--quiet don't print OK for each successfully verified file
--status don't output anything, status code shows success
--strict exit non-zero for improperly formatted checksum lines
-w, --warn warn about improperly formatted checksum lines
--help display this help and exit
--version output version information and exit
The sums are computed as described in RFC 1321. When checking, the input
should be a former output of this program. The default mode is to print
a line with checksum, a character indicating input mode ('*' for binary,
space for text), and name for each FILE.
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'md5sum invocation'
[root@master01 ~]#
md5sum 文件名 生成该文件的md5摘要值
[root@master01 ~]# md5sum erlang-25.3.2.1-1.el7.x86_64.rpm
7737bff77fa2783323220fbacba58495 erlang-25.3.2.1-1.el7.x86_64.rpm
[root@master01 ~]#
md5sum -b 文件名 以二进制的方式读取文件并生成该文件的md5摘要值
md5sum -t 文件名 以字符流的方式读取文件并生成该文件的md5摘要值
[root@master01 ~]# md5sum erlang-25.3.2.1-1.el7.x86_64.rpm
7737bff77fa2783323220fbacba58495 erlang-25.3.2.1-1.el7.x86_64.rpm
[root@master01 ~]# md5sum -b erlang-25.3.2.1-1.el7.x86_64.rpm
7737bff77fa2783323220fbacba58495 *erlang-25.3.2.1-1.el7.x86_64.rpm
[root@master01 ~]# md5sum -t erlang-25.3.2.1-1.el7.x86_64.rpm
7737bff77fa2783323220fbacba58495 erlang-25.3.2.1-1.el7.x86_64.rpm
[root@master01 ~]#
在类UNIX操作系统上,通常两者是没有区别的
md5sum -c check.md5 读取一个检查文件,并校验MD5值
[root@master01 ~]# md5sum erlang-25.3.2.1-1.el7.x86_64.rpm > erlang.md5
[root@master01 ~]# cat erlang.md5
7737bff77fa2783323220fbacba58495 erlang-25.3.2.1-1.el7.x86_64.rpm
[root@master01 ~]# md5sum -c erlang.md5
erlang-25.3.2.1-1.el7.x86_64.rpm: OK
修改检查文件后,再检查会报错
[root@master01 ~]# vim erlang.md5
[root@master01 ~]# cat erlang.md5
6637bff77fa2783323220fbacba58495 erlang-25.3.2.1-1.el7.x86_64.rpm
[root@master01 ~]# md5sum -c erlang.md5
erlang-25.3.2.1-1.el7.x86_64.rpm: FAILED
md5sum: WARNING: 1 computed checksum did NOT match
[root@master01 ~]#
三、其他选项
–quiet
[root@master01 ~]# md5sum -c --quiet erlang.md5
[root@master01 ~]# md5sum -c --quiet erlang.md5.error
erlang-25.3.2.1-1.el7.x86_64.rpm: FAILED
md5sum: WARNING: 1 computed checksum did NOT match