md5sum 命令常用来获取文件 MD5值,验证文件完整性,防止文件被篡改或数据不完整,也可用户计算文本的 MD5 值。
md5sum 采用 MD5 报文摘要算法计算文件内容的摘要值(MD5值),与文件名无关。理论上来说当文件内容相同时 MD5 值也是相同的,但存在极小的概率不同文件会有相同 MD5 值,因为 MD5 值只是一个 128 位的二进制数据。
命令概览:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@centos uphie]# md5sum --help
用法:md5sum [选项]... [文件]...
显示或检查 MD5(128-bit) 校验和。
若没有文件选项,或者文件处为"-",则从标准输入读取。
-b, --binary 以二进制模式读取
-c, --check 从文件中读取MD5 的校验值并予以检查
--tag create a BSD-style checksum
-t, --text 以纯文本模式读取(默认)
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 显示此帮助信息并退出
--version 显示版本信息并退出
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/>
请向<http://translationproject.org/team/zh_CN.html> 报告md5sum 的翻译错误
要获取完整文档,请运行:info coreutils 'md5sum invocation'
获取文件 MD5 值
1
2
[root@centos uphie]# md5sum animal.txt
533a89f0cc5975b4684613f1356c199b animal.txt
也可同时获取多个文件的 MD5 值:
1
2
3
4
[root@centos uphie]# md5sum animal.txt fruit.txt phones.txt
533a89f0cc5975b4684613f1356c199b animal.txt
ecae070ded2cf06a912c05ca18862a62 fruit.txt
5c006136605e6964aa77dd3ec9b4a0b9 phones.txt
获取文本 MD5 值
1
2
[root@centos uphie]# echo uphie.studio | md5sum
f90026fa98af166afa276608debf6685 -
由于没有文件名,所以文件名处用 -
进行了代替。
选项
-b/–binary
以二进制的模式读取文件内容。
1
2
3
4
[root@centos uphie]# md5sum animal.txt
533a89f0cc5975b4684613f1356c199b animal.txt
[root@centos uphie]# md5sum -b animal.txt
533a89f0cc5975b4684613f1356c199b *animal.txt
-t/–text
以文本的模式读取文件内容,也是读取的默认模式。
1
2
3
4
[root@centos uphie]# md5sum animal.txt
533a89f0cc5975b4684613f1356c199b animal.txt
[root@centos uphie]# md5sum -t animal.txt
533a89f0cc5975b4684613f1356c199b animal.txt
-c/–check
指定一个记录 MD5 值和文件名的文件,校验指定的文件和 MD5 值是否匹配,文件内容格式需为:
1
md5val filename
也可多行:
1
2
md5val1 filename1
md5val2 filename2
校验示例:
1
2
3
[root@centos uphie]# echo "533a89f0cc5975b4684613f1356c199b animal.txt" > animal.txt.md5
[root@centos uphie]# md5sum -c animal.txt.md5
animal.txt: 确定
我们修改下 MD5 值:
1
2
3
4
[root@centos uphie]# echo "aa3a89f0cc5975b4684613f1356c199b animal.txt" > animal.txt.md5
[root@centos uphie]# md5sum -c animal.txt.md5
animal.txt: 失败
md5sum: 警告:1 个校验和不匹配