直接把总结写在最前面吧!
gpg有1.x和2.x版本
root@david:~# gpg -h可以查看gpg版本是1.x还是2.x的
ubuntu16.04的gpg默认是1.x的ubuntu18.04和centos7.5的gpg默认是2.x的
在gpg1.x版本中使用gpg自动化加密的话使用如下命令
-c表示仅使用对称加密,--no-tty表示不产生任何输出,--passphrase-fd 0表示从表中输入中读取密码
root@server01:~# echo "YourPassword" | gpg -c --no-tty --passphrase-fd 0 --yes filename
在gpg1.x版本中试用gpg自动化解密使用如下命令:
root@server01:~# gpg --passphrase YourPassword xxx.gpg
在gpg2.x版本中使用自动化加密要额外加上--batch参数,不加则会报错,正确命令如下:
root@david:~ # echo "cX0BCoWt1+qq9ZeKYCXTtxMQeiI" | gpg -c --no-tty --batch --passphrase-fd 0 zabbix.tar.gz
在gpg2.x版本中使用自动化解密同样要加上--batch参数,不然会报错,正确命令如下
root@david:~ # gpg --batch --passphrase cX0BCoWt1+qq9ZeKYCXTtxMQeiI zabbix.tar.gz.gpg
看到公司的备份都是通过gpg加密的,命令如下:
echo $3 | gpg -c --no-tty --passphrase-fd 0 --yes $1
-c Encrypt with a symmetric cipher using a passphrase. The default symmetric cipher used is AES-128, but may be chosen with the --cipher-algo option. This command may be combined with --sign (for a signed and symmetrically encrypted message), --encrypt (for a message that may be decrypted via a secret key or a passphrase), or --sign and --encrypt together (for a signed message that may be decrypted via a secret key or a passphrase).
--no-tty Make sure that the TTY (terminal) is never used for any output. This option is needed in some cases because GnuPG sometimes prints warnings to the TTY even if --batch is used.
--passphrase string
Use string as the passphrase. This can only be used if only one passphrase is supplied. Obviously, this is of very questionable security on a multi-user system. Don't use this option if you can avoid it. Note that this passphrase is only used if the option --batch has also been given. This is different from GnuPG version 1.x.
ubuntu16.04上面的gpg是1.x版本,在ubuntu18.04以及centos7.5上是2.x版本
在ubuntu16.04上面,直接使用--passphrase-fd选项,在ubuntu118.04和centos7.x上,由于gpg版本是2.x的需要额外加上--batch选项
在gpg1.x版本上这个选项的解释如下:
--passphrase-fd n
Read the passphrase from file descriptor n. Only the first line will be read from file descriptor n. If you use 0 for n, the passphrase will be read from STDIN. This can only be used if only one passphrase is supplied.
--yes Assume "yes" on most questions.
所以,上述命令的意思是echo输出密码,管道符传给gpg命令,--passphrase-fd 0将echo的输出作为加密密码,--no-tty屏幕不输出任何提示信息,-c表示仅使用对称密码加密,--yes不解释了
在ubuntu16.04下面测试下:
加密:
root@server01:~/test# ls
Python-3.6.1.tar.xz #测试文件
root@server01:~/test# openssl rand -base64 20 #生成随机密码
XfvsQe2IqwnM+KcoNjqVqTCAP34=
root@server01:~/test# echo "XfvsQe2IqwnM+KcoNjqVqTCAP34" | gpg -c --no-tty --passphrase-fd 0 --yes Python-3.6.1.tar.xz #使用随机密码进行加密
root@server01:~/test# ls
Python-3.6.1.tar.xz Python-3.6.1.tar.xz.gpg
root@server01:~/test#
解密:
root@server01:~/test# ls
Python-3.6.1.tar.xz Python-3.6.1.tar.xz.gpg
root@server01:~/test# rm Python-3.6.1.tar.xz
root@server01:~/test# gpg Python-3.6.1.tar.xz.gpg
gpg: AES 加密过的数据
gpg: gpg-agent 在此次会话中无法使用
gpg: 以 1 个密码加密
root@server01:~/test# ls
Python-3.6.1.tar.xz Python-3.6.1.tar.xz.gpg
root@server01:~/test# tar xf Python-3.6.1.tar.xz
root@server01:~/test# ls
Python-3.6.1 Python-3.6.1.tar.xz Python-3.6.1.tar.xz.gpg
root@server01:~/test#
可以不用手动输入密码,使用--passphrase参数,更多选项可以man gpg:
root@server01:~/test# gpg --passphrase YourPassword xxx.gpg
root@server01:~/test# ls
Python-3.6.1.tar.xz.gpg
root@server01:~/test# gpg --passphrase XfvsQe2IqwnM+KcoNjqVqTCAP34 Python-3.6.1.tar.xz.gpg
gpg: AES 加密过的数据
gpg: gpg-agent 在此次会话中无法使用
gpg: 以 1 个密码加密
root@server01:~/test# ls
Python-3.6.1.tar.xz Python-3.6.1.tar.xz.gpg
root@server01:~/test# tar -xf Python-3.6.1.tar.xz
root@server01:~/test# ls
Python-3.6.1 Python-3.6.1.tar.xz Python-3.6.1.tar.xz.gpg
root@server01:~/test#
在ubuntu16.04上面加密的文件在ubuntu18.04也能解密,gpg版本不影响
在ubuntu18.04下面测试
由于ubuntu18.04和centos7.5上面的gpg是2.x版本的在使用--passphrase-fd命令时要额外使用--batch选项,不然会报如下错误:
root@david:~/test# echo "cX0BCoWt1+qq9ZeKYCXTtxMQeiI" | gpg -c --no-tty --passphrase-fd 0 zabbix.tar.gz
gpg: problem with the agent: 对设备不适当的 ioctl 操作
gpg: 生成密码的时候发生错误:操作已取消
gpg: symmetric encryption of 'zabbix.tar.gz' failed: 操作已取消
root@david:~/test# ls
zabbix.tar.gz
root@david:~/test# echo "cX0BCoWt1+qq9ZeKYCXTtxMQeiI" | gpg -c --no-tty --batch --passphrase-fd 0 zabbix.tar.gz
root@david:~/test# ls
zabbix.tar.gz zabbix.tar.gz.gpg
root@david:~/test#
解密都一样
不加参数需要手动输入密码
root@david:~/test# gpg xxx.gpg
加了参数直接解密
root@david:~/test# ls
zabbix.tar.gz.gpg
root@david:~/test# gpg --batch --passphrase cX0BCoWt1+qq9ZeKYCXTtxMQeiI zabbix.tar.gz.gpg
gpg: WARNING: no command supplied. Trying to guess what you mean ...
gpg: AES256 加密过的数据
gpg: 以 1 个密码加密
root@david:~/test# ls
zabbix.tar.gz zabbix.tar.gz.gpg