Linux md5sum 的用法

   MD5 算法常常被用来验证网络文件传输的完整性,防止文件被篡改。MD5 全称是报文摘要算法,此算法对任意长度

的信息逐位计算,产生一个二进制长度为 128 位(十六进制长度 32 位)的报文摘要,不同的文件产生相同的报文摘要的可

能性非常小。

    在 Linux 上,md5sum 是用来计算和校验文件报文摘要的工具程序。一般来说,安装了 Linux 后,就会有 md5sum 这

个工具,直接在命令行终端直接运行。

    1.使用 md5sum 来产生报文摘要命令如下:

md5sum file > file.md5 
或
md5sum file >> file.md5

  也可以把多个文件的报文摘要输出到一个文件中,这需要使用通配符 * ,比如某目录下有几个 iso 文件,要把这几个iso

文件的报文摘要输出到 iso.md5 文件中,命令如下:

md5sum *.iso > iso.md5

  2.使用 md5 报文摘要验证文件

           2.1 把下载的文件 file 和 该文件的 file.md5 报文摘要放在同一个目录下,然后用如下命令进行验证:

md5sum -c file.md5

     2.2 使用 md5sum file 命令会输出一个 md5 的报文摘要,然后把这个报文摘要直接与 file.md5 中的内容比较。

        示例如下:

[root@VM_81_181_centos ~]# mkdir test
[root@VM_81_181_centos ~]# cd test/
[root@VM_81_181_centos test]# touch f1.txt f2.txt
[root@VM_81_181_centos test]# ls
f1.txt  f2.txt
[root@VM_81_181_centos test]# md5sum *.txt > md5sumtest.md5
[root@VM_81_181_centos test]# cat md5sumtest.md5 
d41d8cd98f00b204e9800998ecf8427e  f1.txt
d41d8cd98f00b204e9800998ecf8427e  f2.txt
[root@VM_81_181_centos test]# echo "hahaha" > f1.txt 
[root@VM_81_181_centos test]# md5sum -c md5sumtest.md5 
f1.txt: FAILED
f2.txt: OK
md5sum: WARNING: 1 computed checksum did NOT match
[root@VM_81_181_centos test]# echo "hahaha" > f2.txt 
[root@VM_81_181_centos test]# md5sum -c md5sumtest.md5 
f1.txt: FAILED
f2.txt: FAILED
md5sum: WARNING: 2 computed checksums did NOT match
[root@VM_81_181_centos test]# cat /dev/null > f1.txt 
[root@VM_81_181_centos test]# md5sum -c md5sumtest.md5 
f1.txt: OK
f2.txt: FAILED
md5sum: WARNING: 1 computed checksum did NOT match
[root@VM_81_181_centos test]# cat /dev/null > f2.txt 
[root@VM_81_181_centos test]# md5sum -c md5sumtest.md5 
f1.txt: OK
f2.txt: OK
[root@VM_81_181_centos test]# 

  生成一个文件的 md5 值

[root@VM_81_181_centos test]# md5sum f1.txt 
d41d8cd98f00b204e9800998ecf8427e  f1.txt

  

猜你喜欢

转载自www.cnblogs.com/leeyongbard/p/10281580.html