Type of GPG keys
The GPG key pair comprises two types of keys: Private and Public keys. You can use the public key for the data encryption, and that encrypted data will be decrypted using the Private key. As a Linux user, you can only share your Public key. For security reasons, you must keep your Private key secret. Anyone can use your public key to encrypt the data; however, the private key will be needed to decrypt it.
How to list GPG keys in Linux
The gpg command permits Linux users to list public keys and private keys using two different commands. For instance, the “–list-keys” option is added in the “gpg” command for listing the public GPG keys from the public key:
In the below-given output, “pub” represents Public key, “uid” indicates the “User ID”, and “sub” shows the Subkey:
The secret keyring stores the private GPG keys, and to list them on the Linux terminal, add the “–list-secret-keys” in the “gpg” command and execute it:
Here, “sec” represents Secret or Private key, “uid” is for the User ID, and “ssb” indicates the Secret Subkey:
Note: To delete the GPG keys of a specific user, note down its user ID “uid” and “Key ID” from the output of the above-given commands and then move to the next section.
How to delete GPG keys of a single user in Linux
There are two methods for deleting a user’s public and private GPG keys; either you can delete them using the user ID “uid” or their Key ID (Public ID). Also, remember that you have to delete the private key first and then move towards the public key deletion process, as GPG only permits you to delete the public key when its related private key is removed from the system.
Here is the syntax for deleting private GPG using the “uid”:
For instance, to delete the GPG keys of the user “Liza”, we will first delete its private key by specifying the “Liza” “uid” in the following command:
A confirmation dialog box will appear on your screen, click on the “Delete Key” button, and the private key of the specified user will be deleted within a few seconds:
Now, check out the syntax for deleting the public GPG of the user by using its “uid”:
To delete the private of the “Liza”, we will write out the following command in the terminal:
Enter “y” to confirm the deletion process, and you are all done!
Next, list out the public and private keys for the verification purpose:
Now, we will demonstrate the GPG key deletion method using a user’s Key ID. To delete a user’s private GPG key, we will follow the below-given syntax:
For instance, to delete the private GPG key of “john”, we will add its Key ID in the “gpg” command in the following way:
Click on the “Delete key” button to confirm that you want to delete the secret key of the mentioned user permanently:
The error-free output indicates that John’s private key is deleted. Now, to delete the public key, we will execute the “gpg –delete-key” command with its Key ID:
Check out the list of public and private keys to confirm whether the deletion operation worked or not:
How to delete GPG keys of multiple users in Linux
The “gpg” command also permits you to delete the GPG keys of multiple users at once, and it follows the order for the deletion in which the uids or the Key IDs are added.
Before moving towards, we will list out the private and public GPG keys and note down the uid, the private and Key ID of the “sharqa” and “fred” user:
Using the Linux terminal, you can delete the GPG keys of the multiple users by adding their uids in the gpg command:
For instance, we will execute the following command for deleting private keys of “sharqa” and “fred” with the help of their “uids”:
The gpg command will firstly delete the secret key of the user “sharqa,” and then it will remove the fred’s key from the secret keyring:
Lastly, we will delete the public keys as well:
Similarly, if you want to delete the private GPG key of multiple users by using their Key IDs, then have a look at the below-given syntax:
For instance, to delete the private GPG key of “sharqa” and “fred”, we will add their Key IDs in the following way:
Note that we have added the Key ID of the “sharqa” first; that’s why its private key is going to delete before fred’s:
Now, hit the “Delete Key” button for the confirmation:
At this point, the private key of the user “sharqa” is deleted, and the gpg command will move towards the deletion of fred’s key:
The above-given error-free output indicates that the private keys of both users are now deleted. In the next step, we will write the following command for deleting the sharqa’s and fred’s public keys from the public keyring:
Conclusion
As a Linux user, your GPG keyring comprises a long list after importing the keys of the other users to your system. You can delete the unnecessary GPG keys by using the “gpg” command. The “–delete-key” option is added in the “gpg” command for deleting the public key, whereas the “–delete-secret-key” option assists in the secret or private key deletion. This write-up demonstrated how to delete GPG keys of single and multiple users. Moreover, the procedures of using “uid” and the “Key ID” for deleting the private and public keys are also provided.