How to Add and Delete SSH Keys?
Introduction
To add an SSH key to your server, run ssh-copy-id -i ~/.ssh/id_rsa.pub user@server. To remove or delete one, open ~/.ssh/authorized_keys on the server (or the ~/.ssh/ folder on Mac/Windows), delete the key, and save. Both changes take effect immediately and don’t require a server reinstall — and the steps work whether you’re on Mac, Windows, or Linux.
If you’ve used a hosting provider’s web panel to change SSH keys before, you may have noticed it triggers a full reinstall and wipes your data. The manual method below avoids that entirely.
This guide covers both methods step by step, plus an alternative approach that works even without ssh-copy-id installed.
Learn the step-by-step process in our guide on How Do I Generate SSH Keys?
Prerequisites
- Before you get started, make sure the following requirements are met:
- You have an SSH key in OpenSSH format. If you don’t already have one, refer to our guide to creating an SSH key.
- Your server is running Linux or BSD.
- You’re working from a Linux, Mac, or Windows workstation.
How to Add an SSH Key to Your Server?
Use the ssh-copy-id command from a Linux or macOS system to deploy a new SSH key to the server.
From a Linux or Mac Workstation
Most Linux systems come with the ssh-copy-id utility pre-installed. On macOS, you can install it using Homebrew, MacPorts, or by downloading it manually with curl.
Use the following command to copy your SSH public key to the server:
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
Replace ~/.ssh/id_rsa.pub with the actual path to your public key if it’s different.
Replace [email protected] with your server’s username and IP address or domain.
When you run the command, you’ll see output similar to this:
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/your_user/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys [email protected]'s password:
After entering your password, the utility installs the key and confirms:
Number of key(s) added: 1 Now try logging into the machine, with: "ssh '[email protected]' " and check to make sure that only the key(s) you wanted were added.
From a Windows Workstation
Windows users can use a one-liner in PowerShell to copy their public key to the server. This command pipes your key into the remote server’s authorized_keys file:
type $env:USERPROFILE\.ssh\id_rsa.pub | ssh [email protected] "cat >> ~/.ssh/authorized_keys"
Make sure to adjust the file path if your key is stored under a different name.
Replace [email protected] with your actual server credentials.
Alternate Method (Works From Any Workstation)
If ssh-copy-id isn’t available or you prefer doing things manually:
SSH into your server:
ssh [email protected]
Open the authorized_keys file using a text editor:
nano ~/.ssh/authorized_keys
Copy your public key from your local machine and paste it into the file.
Save the file and exit the editor. Your new SSH key is now active.
How to Remove an SSH Key?
There’s no automated way to remove SSH keys; you’ll need to do it manually. Here’s how:
SSH into your server:
ssh [email protected]
Open the authorized_keys file:
nano ~/.ssh/authorized_keys
Locate the line containing the public key you want to remove and delete it.
Save and close the file. That key will no longer be able to access your server.
Follow this guide to Set Up Access to Private Repositories Using Git quickly and securely.
Conclusion
Manually changing SSH keys on a running server is a smart and secure way to manage access without risking data loss. Avoiding automated tools that reinstall your server helps you maintain uptime and full control over your configuration.
Whether you’re rotating keys for security reasons, granting access to a team member, or just cleaning up unused credentials, these simple steps make it easy to manage SSH access from any workstation. Always remember to test your new key before removing the old one to prevent accidental lockouts.
FAQs
How to remove SSH key in Linux?
Open the ~/.ssh/authorized_keys file, delete the key which you will not need and save the file. Access will no longer be available to the key.
How to delete SSH keys on Mac?
Go to the ~/.ssh/ folder, find the key files you want to remove, and delete them. Make sure you delete the private and public key where necessary.
How to delete SSH key in Windows?
To remove the necessary SSH key on the system, open the .ssh folder (which is normally in the user profile), find the key files, and delete them.
How to clear SSH keys on Mac?
To clear all SSH keys on a Mac, open the ~/.ssh/ folder in Finder or Terminal and delete the key files you no longer need – typically id_rsa, id_rsa.pub, or similarly named key pairs. Removing a key here only affects your local machine; you’ll also need to remove it from any server’s authorized_keys file to fully revoke access.
What’s the difference between removing and deleting an SSH key?
Removing an SSH key from a server means deleting its entry from the authorized_keys file, which revokes its access. Deleting an SSH key means removing the actual key file (public and/or private) from your local machine or server, which permanently destroys the key itself.
How do I copy an SSH key to a Mac?
Use the ssh-copy-id command to copy your public key to a server from a Mac. If it’s not already installed, add it with brew install ssh-copy-id, then run ssh-copy-id -i ~/.ssh/id_rsa.pub user@server.