使用 gpg key 讓 git commit 更安全
起因
避免別人偽造自己的 git commit。通過每次 git commit 時輸入密碼,大幅降低被偽造的概率。此密碼即 gpg key 配置時設定的密碼。生成 gpg key 后,在 git config 和 Github setting 中配置 gpg key,使得以后每次 git commit 都是帶 gpg 簽名的。
Github 官方給出了比較詳細的文檔,基於此進行實踐,記錄如下,包括文檔中沒有提到的細節。
安裝 gpg
ubuntu 16.04:從 ppa 安裝新版 gpg
按 github 上配置 gpg key,需要新版 gpg,ubuntu16.04 的 apt 提供的用不了,太舊,需要換新的:
sudo add-apt-repository ppa:savoury1/gpg
sudo apt-get update
sudo apt install gpg
使用中發現, ppa 安裝的 gpg 會導致 add-ppa-repository 命令被卸載並且無法通過 apt 安裝(破壞了依賴關系),考慮源碼編譯安裝 gpg。
ubuntu 16.04:源碼編譯 gpg
cd ~/Downloads
INSTALL_PREFIX=/usr/local/gpg2
export PATH=$PATH:$INSTALL_PREFIX/bin
wget https://gnupg.org/ftp/gcrypt/libgpg-error/libgpg-error-1.41.tar.bz2
tar -xvf libgpg-error-1.41.tar.bz2
cd libgpg-error-1.41
./configure --prefix=$INSTALL_PREFIX
make -j8
sudo make install
cd ..
wget https://gnupg.org/ftp/gcrypt/libgcrypt/libgcrypt-1.8.7.tar.bz2
tar -xvf libgcrypt-1.8.7.tar.bz2
cd libgcrypt-1.8.7
./configure --prefix=$INSTALL_PREFIX
make -j8
sudo make install
cd ..
wget https://gnupg.org/ftp/gcrypt/libksba/libksba-1.5.0.tar.bz2
tar -xvf libksba-1.5.0.tar.bz2
cd libksba-1.5.0
./configure --prefix=$INSTALL_PREFIX
make -j8
sudo make install
cd ..
wget https://gnupg.org/ftp/gcrypt/libassuan/libassuan-2.5.4.tar.bz2
tar -xvf libassuan-2.5.4.tar.bz2
cd libassuan-2.5.4
./configure --prefix=$INSTALL_PREFIX
make -j8
sudo make install
cd ..
wget https://gnupg.org/ftp/gcrypt/ntbtls/ntbtls-0.2.0.tar.bz2
tar -xvf ntbtls-0.2.0.tar.bz2
cd ntbtls-0.2.0
./configure --prefix=$INSTALL_PREFIX
make -j8
sudo make install
cd ..
wget https://gnupg.org/ftp/gcrypt/npth/npth-1.6.tar.bz2
tar -xvf npth-1.6.tar.bz2
cd npth-1.6
./configure --prefix=$INSTALL_PREFIX
make -j8
sudo make install
cd ..
wget https://gnupg.org/ftp/gcrypt/gnupg/gnupg-2.2.27.tar.bz2
tar -xvf gnupg-2.2.27.tar.bz2
cd gnupg-2.2.27
./configure --prefix=$INSTALL_PREFIX
make -j8
sudo make install
cd ..
sudo apt-get install pinentry-curses
Windows
安裝 Gpg4win,會安裝 GnuPG 和 Gpg4win 兩個目錄。把 GnuPG/bin
放 PATH 里。
github 配置 gpg
按說應該 檢查 gpg key,我第一次創建,不用檢查。
gpg --full-generate-key
然后注意,選 key 長度的時候,默認值是 3072,需要修改為 4096。
查看 gpg 公鑰,
gpg --list-secret-keys --keyid-format LONG
/home/zz/.gnupg/pubring.kbx
---------------------------
sec rsa4096/ABCABCABC1234567 2021-01-15 [SC]
837F674FE2A5CC1B576BB8BA0961A0300B14A36D
uid [ 絕對 ] XX <XXXX@XX.com>
ssb rsa4096/36E9306FBCE179EF 2021-01-15 [E]
選擇ABCABCABC1234567
這個。
導出 gpg 公鑰
gpg --armor --export ABCABCABC1234567
粘貼到 Github 的 GPG 設置頁面 里的 gpg key。
zshrc 配置 gpg key
~/.zshrc 需要配置:
export GPG_TTY=$(tty)
然后 source ~/.zshrc
否則無法彈出gpg輸入密碼的文本GUI界面,導致帶簽名的commit失敗。
git 配置 gpg key
git config --global commit.gpgsign true
git config --global user.signingkey ABCABCABC1234567
重啟 gpg agent 服務(否則導出 gpg 私鑰會卡住沒反應,報錯說error receiving key from agent: timeout - skipped)
gpgconf --kill gpg-agent
導出 gpg 私鑰:
gpg --export-secret-key -a > secretkey.asc
拷貝 gpp 私鑰到另一台機器上:
scp secretkey.asc zz@172.X.Y.Z:/home/zz
在新機器上導入 gpg 私鑰
gpg --import secretkey.asc
其中后兩步也可以合並為:
gpg --export-secret-key -a | ssh 172.X.Y.Z gpg --import -
References
https://logistics.camber.moe/2020/GitRevised/
https://askubuntu.com/questions/32438/how-to-share-one-pgp-key-on-multiple-machines
https://omgdebugging.com/2019/08/04/install-latest-version-of-gnupg/