linux中校验文件完整性(md5,sha1)
经常在Linux下下载软件的人,一定会有一个良好的习惯:校验文件的hash,以确定文件的完整性甚至是安全性。我配置环境的时候也恰好用到了,笔者的是一个lubuntu的机子。这里我大致做个记录。(不了解hash的请自行查阅相关资料)
MD5校验:
magic@luna:~/lnmp$ md5sum lnmp1.3-full.tar.gz a5aa55cd177cd9b9176ad697c12e45c0 lnmp1.3-full.tar.gz
或者我们可以保存到一个文件中:
magic@luna:~/lnmp$ md5sum lnmp1.3-full.tar.gz > md5-hash.txt
然后:
magic@luna:~/lnmp$ cat md5-hash.txt a5aa55cd177cd9b9176ad697c12e45c0 lnmp1.3-full.tar.gz
还可以根据已经得到的hash来确认文件:
magic@luna:~/lnmp$ md5sum -c md5-hash.txt lnmp1.3-full.tar.gz: 确定 magic@luna:~/lnmp$
SHA1:
这个类似于MD5校验方法:
magic@luna:~/lnmp$ sha1sum lnmp1.3-full.tar.gz ee7ec6e8be0b3b5a481df903427261236f9bb057 lnmp1.3-full.tar.gz
或者保存到文件中再去查看:
magic@luna:~/lnmp$ sha1sum lnmp1.3-full.tar.gz > sha1-hash.txt magic@luna:~/lnmp$ cat sha1-hash.txt ee7ec6e8be0b3b5a481df903427261236f9bb057 lnmp1.3-full.tar.gz
已知hash情况下的校验(通常这个情况还挺多):
magic@luna:~/lnmp$ sha1sum -c sha1-hash.txt lnmp1.3-full.tar.gz: 确定 magic@luna:~/lnmp$
注意事项:在已知hash数值情况下对文件进行校验的时候要注意,一定要让系统能够找到要校验的文件。否则就没法进行校验了。具体的使用说明,可以通过md5(sha1)sum --help来查看:
magic@luna:~/lnmp$ sha1sum --help Usage: sha1sum [OPTION]... [FILE]... Print or check SHA1 (160-bit) checksums. 如果没有指定文件,或者文件为"-",则从标准输入读取。 -b, --binary read in binary mode -c, --check 从文件中读取SHA1 的校验值并予以检查 --tag create a BSD-style checksum -t, --text 以纯文本模式读取(默认) The following five options are useful only when verifying checksums: --ignore-missing don't fail or report status for missing files --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 FIPS-180-1. When checking, the input should be a former output of this program. The default mode is to print a line with checksum, a space, a character indicating input mode ('*' for binary, ' ' for text or where binary is insignificant), and name for each FILE. GNU coreutils online help: <http://www.gnu.org/software/coreutils/> 请向<http://translationproject.org/team/zh_CN.html> 报告sha1sum 的翻译错误 Full documentation at: <http://www.gnu.org/software/coreutils/sha1sum> or available locally via: info '(coreutils) sha1sum invocation'
ps:转载请注明文章出处。