Open In App

Removing an SSH Public Key for user

Last Updated : 10 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Secure Shell (SSH) keys are a fundamental part of modern authentication and secure communication between computers over a network. SSH keys provide a secure way to access remote servers and perform various administrative tasks. However, managing SSH keys is essential to maintain the security of your systems. In some cases, you may need to delete an SSH key for a user. This article will walk you through the process of deleting SSH key pairs, covering every aspect of it.

Understanding SSH Keys

Before diving into the deletion process, let’s briefly understand what SSH keys are and how they work.

SSH keys come in pairs: a public key and a private key. The public key is placed on remote servers you want to access securely, while the private key is kept on your local machine. When you attempt to connect to a remote server, the server checks whether your public key matches the private key you provide for authentication.

Reasons to Delete SSH Keys

There are various reasons why you might need to delete SSH keys for a user:

  1. Security Concerns: If you suspect that a user’s private key has been compromised or is no longer secure, it’s crucial to delete it to prevent unauthorized access.
  2. User Departure: When an employee or team member leaves your organization, you should remove their SSH keys to ensure they no longer have access to your servers.
  3. Key Rotation: Periodically, it’s a good security practice to rotate SSH keys. Deleting old keys is part of this process.

Now that we understand why we might need to delete SSH keys, let’s delve into the step-by-step process of doing so.

Deleting SSH Keys

The process of deleting SSH keys involves locating and removing the public key file from the remote server. Here’s how to do it:

Step 1: Log into the Remote Server

You need to have administrative access to the remote server to delete SSH keys for a user. Log in using SSH with an account that has the necessary permissions.

ssh username@remote_server_ip

Step 2: Locate the SSH Directory

SSH key files are typically stored in the user’s home directory within a hidden folder called .ssh. Navigate to this directory.

cd ~/.ssh

Step 3: Identify the Key to Delete

Inside the .ssh directory, you’ll find files with names like id_rsa.pub, id_ecdsa.pub, or id_ed25519.pub. These files correspond to the public keys associated with the user. Identify the key you want to delete.

Step 4: Delete the Key

Use a text editor or command-line tools like rm to delete the public key file associated with the user.

rm id_rsa.pub

Step 5: Verify Deletion

To confirm that the key has been deleted, attempt to list the contents of the .ssh directory again.

ls -l

The deleted key should no longer appear in the directory listing.

Step 6: Restart SSH Service (Optional)

In some cases, it may be necessary to restart the SSH service for the changes to take effect.

sudo service ssh restart

Examples

Deleting an SSH Key for User “shivansh” on a Linux Server

Let’s say you want to delete an SSH key for a user named “shivansh” on a Linux server with the IP address “192.168.1.47.”

Log into the remote server using SSH:

ssh shivansh@192.168.1.47

Navigate to the .ssh directory:

cd ~/.ssh

Identify and delete the SSH key:

If, for example, you want to delete the `id_rsa.pub` key for John, you would run:

rm id_rsa.pub

Verify deletion:

To make sure the key has been removed, you can list the contents of the `.ssh` directory again:

ls -l

The id_rsa.pub file should no longer be present in the directory listing.

Deleting an SSH Key for User “rishabh” on a Windows Server

Deleting an SSH key on a Windows server is similar to the process on a Linux server, with slight differences in commands.

Log into the remote Windows server using SSH:

You may use an SSH client like PuTTY to connect to a Windows server.

Navigate to the `.ssh` directory:

Windows typically stores SSH keys in the user’s home directory under the .ssh folder, just like in Linux.

cd C:\Users\rishabh\.ssh

Identify and delete the SSH key:

To delete an SSH key, for example, id_rsa.pub for rishabh, you can use the del command:

del id_rsa.pub

Verify deletion:

Confirm that the key has been deleted by listing the contents of the .ssh directory:

dir

The id_rsa.pub file should no longer be listed in the directory.

Frequently Asked Questions

Q1.What Are SSH Keys, and Why Are They Important?

Anwer:

SSH keys consist of a public and a private key and are essential for secure communication and authentication between computers over a network. They play a crucial role in accessing remote servers securely and performing administrative tasks. This article explains their significance.

Q2.What Are the Common Reasons for Deleting SSH Keys?

Answer:

There are various reasons for deleting SSH keys, including security concerns, employee departures, and key rotation. This article outlines these reasons in detail and highlights the importance of managing SSH keys for security.

Q3.How Can I Delete an SSH Key for a User on a Remote Server?

Answer:

The article provides a step-by-step guide on how to delete an SSH key for a user on a remote server. It covers the process, from logging into the server to verifying the deletion. The instructions are suitable for both Linux and Windows servers.

Q4.What Precautions Should I Take When Deleting SSH Keys?

Answer:

It’s crucial to exercise caution when deleting SSH keys to prevent accidental removal of important access credentials. The article does not go into specific precautions but emphasizes the importance of understanding the process before proceeding.

Q5.Do I Need to Restart the SSH Service After Deleting an SSH Key?

Answer:

While not always necessary, the article mentions that in some cases, you may need to restart the SSH service for the changes to take effect after deleting an SSH key. It provides a command for restarting the SSH service as an optional step.

Conclusion

Managing SSH keys is a critical aspect of maintaining the security of your servers and systems. Knowing how to delete SSH keys for a user is essential when dealing with security concerns, employee departures, or key rotation. By following the steps outlined in this guide, you can confidently remove SSH keys from a remote server, ensuring that only authorized individuals have access to your systems. Remember to exercise caution when deleting keys to avoid accidental removal of important access credentials.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads