Connect to SFTP with a Private Key
Log in to an SFTP server with an SSH key instead of a password.
By the WinSCP Downloads Team · Last updated
Why use a key instead of a password?
An SSH key pair is a private key that stays on your PC and a public key you place on the server. The server lets you in only if you hold the matching private key, so there is no password to guess or steal, and scripts can log in without storing one.
How do you create a key pair?
- In the WinSCP Login dialog, choose Tools > Run PuTTYgen.
- Select the key type Ed25519 and click Generate, then move the mouse over the window until the key is created.
- Enter a key passphrase. It protects the key if someone copies the file.
- Click Save private key and store the .ppk file somewhere safe, for example C:\Users\you\.ssh\server.ppk.
- Copy the text in the box labelled Public key for pasting into OpenSSH authorized_keys file.
How do you add the public key to the server?
Log in to the server once with your password (in a terminal, or via Commands > Open Terminal in WinSCP) and add the public key as a single line to ~/.ssh/authorized_keys. The permissions matter: most SSH servers ignore the file if it can be written by other users.
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "ssh-ed25519 AAAA... your-key-comment" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keysHow do you tell WinSCP to use the key?
- In the Login dialog, select your site and click Edit.
- Click Advanced, then go to SSH > Authentication.
- Under Private key file, select your .ppk file and click OK.
- Clear the saved password if there is one, click Save, then Login.
- Enter the key passphrase when WinSCP asks for it.
If you select an OpenSSH private key instead of a .ppk file, WinSCP offers to convert it to the .ppk format for you.
How do you use the key in a script?
Pass the key with -privatekey and pin the server's host key with -hostkey, so the script never trusts an unknown server:
open sftp://deploy@example.com/ -privatekey="C:\keys\server.ppk" -hostkey="ssh-ed25519 255 SHA256:..."
put C:\site\* /var/www/
exitA key used by an unattended script usually has no passphrase, so keep that file readable only by the account that runs the script.
What if the server refuses the key?
- "Server refused our key": the public key is missing from ~/.ssh/authorized_keys of the user you log in as, or the line is broken across several lines.
- Permissions: ~/.ssh must be 700, authorized_keys 600, and your home folder must not be writable by other users.
- Wrong user: the key is only accepted for the account whose authorized_keys contains it.
- The server log (often /var/log/auth.log or the journal for sshd) states the exact reason.
To avoid typing the passphrase every time, load the key into Pageant: WinSCP uses keys from Pageant automatically.
Sources
This guide is written in our own words and checked against the official WinSCP documentation. For full details, see:
- Set up SSH public key authentication – winscp.net
- Using Public Keys for Authentication – winscp.net
- The Authentication Page (Advanced Site Settings dialog) – winscp.net
Related pages
- Convert an SSH Key for WinSCP – Convert OpenSSH private keys to PuTTY .ppk format, and back.
- Free SFTP Client for Windows – Using WinSCP as an SFTP client on Windows.
- Scripting – Automating transfers with WinSCP's scripting language.
- Troubleshooting – Fixes for common WinSCP connection problems.
Part of the WinSCP documentation. Download WinSCP for Windows.