Your Search Bar For Shrewd Tips

How Do I Clear Known_hosts In Ssh Windows?


If SSH on Windows reports that a server’s host identification has changed, you may need to remove the old entry from your known_hosts file. This file stores the host keys SSH has previously accepted, helping protect your connections from impersonation. When a legitimate server is rebuilt, changes its SSH keys, or moves to a new address, the stored key may no longer match. You can clear the outdated entry without deleting every trusted server. This guide explains how to find and edit known_hosts in Windows, use ssh-keygen to remove a specific host, and check the new key safely.

What Is the SSH known_hosts File?

When you connect to an SSH server, your SSH client checks the server’s host key. On a first connection, SSH may ask whether you trust the key. If you accept it, the client saves the key in a file named known_hosts. On later connections, SSH compares the key it receives with the saved key. If they match, the connection can continue; if they do not, SSH displays a warning.

On Windows, the standard OpenSSH client usually looks for this file in your user profile’s .ssh directory:

C:\Users\YourUserName\.ssh\known_hosts

The equivalent PowerShell path is $env:USERPROFILE\.ssh\known_hosts. The file may not exist until you have made an SSH connection. It is a plain-text file, but entries can be hashed, so a saved server name may not be readable when you open it in a text editor.

Why Would You Need to Clear a known_hosts Entry?

A changed host key does not always indicate a problem. Common legitimate reasons include a server being reinstalled, an SSH host key being regenerated, a cloud machine receiving a replacement instance, or an administrator migrating a service to new hardware. You may also see a warning after changing a server’s IP address or connecting through a different hostname or port.

However, SSH’s warning is an important security check. A different host key can also mean that your connection is reaching an unexpected machine or that someone is attempting to intercept it. Before removing an entry, confirm the change with the server administrator or verify the new fingerprint through a trusted channel. Do not clear the warning simply to make it disappear if you cannot explain why the key changed.

Find Your known_hosts File in Windows

Open PowerShell and check whether the file exists with this command:

Test-Path "$env:USERPROFILE\.ssh\known_hosts"

If the command returns True, the file is present. To see its full path, run:

Write-Output "$env:USERPROFILE\.ssh\known_hosts"

You can also open the .ssh folder in File Explorer. Press Windows key + R, enter %USERPROFILE%\.ssh, and press Enter. If Windows says the location is unavailable, you may not have created the folder or made an SSH connection yet. A leading dot can make a folder or file less noticeable, but it does not change the path.

If you use more than one SSH client, check which one is producing the warning. Windows OpenSSH, Git for Windows, WSL, and some third-party clients may use separate configuration files or environments. Removing an entry from Windows’ known_hosts file will not necessarily change the file used by Git Bash or WSL.

Remove One Host with ssh-keygen

The safest and usually easiest way to clear a single outdated entry is the ssh-keygen -R command. It removes the matching host from known_hosts while leaving unrelated entries in place. Open PowerShell or Command Prompt and run:

ssh-keygen -R server.example.com

Replace server.example.com with the hostname you use when connecting. You can also use the server’s IP address if that is how you connect:

ssh-keygen -R 203.0.113.25

On a typical Windows OpenSSH installation, the command checks the default known_hosts file in your user profile. Its output should indicate whether a matching entry was found and removed. It may also create a backup file, often named known_hosts.old. Keep the backup temporarily in case you remove the wrong entry, but remember that it still contains the old host key.

If the command cannot find the file or you want to specify exactly which file to edit, include the -f option. In PowerShell, use:

ssh-keygen -R server.example.com -f "$env:USERPROFILE\.ssh\known_hosts"

The same command works in Command Prompt with the Windows environment variable syntax:

ssh-keygen -R server.example.com -f "%USERPROFILE%\.ssh\known_hosts"

Run the command using the same account and SSH environment that produced the warning. If you open a terminal as a different Windows user, that account has a different profile and may have a different known_hosts file.

Clear an Entry for a Nonstandard SSH Port

SSH entries for a nonstandard port are stored using a combined host-and-port format. For example, if the server is server.example.com and SSH listens on port 2222, remove the entry like this:

ssh-keygen -R "[server.example.com]:2222"

Keep the square brackets and quotation marks. The bracketed format tells SSH that the entry refers to a particular host on a particular port. If you connect to the same server on the default port and on a custom port, those can be separate entries, so remove the one that matches the connection in your warning.

If you are unsure which port or hostname SSH is using, check your connection command, SSH configuration, or the details shown in the warning. For example, a connection such as ssh -p 2222 user@server.example.com uses the custom-port format.

Manually Edit or Delete known_hosts

You can remove an entry manually if ssh-keygen -R is unavailable or you need to inspect the file. First, make a backup. In PowerShell, run:

Copy-Item "$env:USERPROFILE\.ssh\known_hosts" "$env:USERPROFILE\.ssh\known_hosts.backup"

Then open the original file in Notepad:

notepad "$env:USERPROFILE\.ssh\known_hosts"

Find the entry that matches the host and delete only that line. Save the file and close Notepad. A line can contain a hostname, a key type, and a long key value. If entries are hashed, the hostname may look like random text rather than a readable server name. In that case, use ssh-keygen -R instead of trying to identify the line by sight.

You can delete the entire file if you deliberately want to remove all saved host keys. In PowerShell, the command is:

Remove-Item "$env:USERPROFILE\.ssh\known_hosts"

In Command Prompt, use:

del "%USERPROFILE%\.ssh\known_hosts"

Deleting the whole file affects every host recorded there, not just the one causing the warning. On future connections, SSH will treat each server as a first-time connection and ask you to verify its key again. For that reason, removing one host with ssh-keygen -R is preferable in most situations.

Connect Again and Verify the New Host Key

After removing the outdated entry, reconnect using the same command as before. For example:

ssh user@server.example.com

SSH may display a first-connection message with the server’s key fingerprint and ask whether you want to continue. Before accepting, compare the fingerprint with one supplied by the server administrator or obtained from another trusted source. A fingerprint shown only by the connection you are trying to verify is not independent confirmation.

If the fingerprint is correct, accept the prompt. SSH will save the new key in known_hosts. If the fingerprint is not the one you expected, stop and investigate rather than accepting it. Confirm that you have the right hostname, IP address, port, and account, and ask whether the server’s key was intentionally changed.

What If the Warning Still Appears?

If you cleared an entry but receive the same warning, check these common causes:

  • You removed a different name. SSH may have saved the host under an alias, an IP address, or a fully qualified domain name. Remove the exact name used in the connection.
  • The server uses a custom port. Remove the bracketed host-and-port entry, such as [server.example.com]:2222.
  • You edited the wrong file. Windows OpenSSH, Git for Windows, and WSL can use different known_hosts files. Check the client and terminal that display the warning.
  • An SSH configuration file changes the destination. Review %USERPROFILE%\.ssh\config for host aliases, alternate ports, or a UserKnownHostsFile setting that points to another file.
  • The key is changing repeatedly. The server may be behind a load balancer or may not be configured with consistent host keys. Ask the administrator to investigate instead of repeatedly deleting entries.
  • The warning is not from the standard OpenSSH client. A third-party SSH application may keep its own host-key database. Use that application’s settings or documentation to remove the saved key.

You can check which SSH executable PowerShell finds by running Get-Command ssh. This can help identify whether you are using Windows OpenSSH or another installation. If you have multiple clients, use the matching ssh-keygen command and file path for the client that generated the warning.

Conclusion

To clear a known host in SSH on Windows, remove the specific outdated entry with ssh-keygen -R hostname. For a custom port, include the bracketed hostname and port, such as ssh-keygen -R "[server.example.com]:2222". The standard Windows OpenSSH file is usually %USERPROFILE%\.ssh\known_hosts, and you can back it up or edit it with Notepad if needed. Verify why the host key changed and confirm the replacement fingerprint before accepting it. Removing only the affected entry keeps other saved SSH host keys intact while restoring a secure connection.


Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.

Shrewdnia

Shrewdnia

Shrewdnia is a destination for curious minds seeking clarity, knowledge, and informed perspectives. Through insightful articles and practical guides our passionate team explores a wide range of topics designed to help readers understand the world around them, make smarter decisions, and stay informed in an ever-changing landscape.


💡 Every question sparks discovery, and every perspective enriches the conversation. Share your thoughts and insights in the comments 👇

Back to blog

Leave a comment

JOIN THE SHREWDNIA COMMUNITY FORUM

What do you think?

Have an opinion, experience, or question about this topic? Join the Shrewdnia Forum and share your thoughts with other readers.

Join the Forum →