You can establish a secure SFTP connection between two servers directly from the Windows command line by using the built-in
sftp client, which relies on SSH protocols to encrypt data in transit. This method requires no third-party software installation and works seamlessly for transferring files or directories between remote hosts while maintaining strict security standards against eavesdropping and tampering.
Prerequisites and Environment Setup
Before initiating any transfer, ensure that both the source and destination servers have SSH services running and are accessible over the network. The Windows server acting as the client must have OpenSSH Client features enabled, which is standard in modern versions of Windows 10 and 11. You will need valid credentials for the destination server, including the username and either a password or an SSH key pair. It is also critical to verify that the firewall on the destination server allows inbound traffic on port 22, or whichever custom port your SSH daemon is configured to use. If you are connecting through a proxy or NAT device, ensure that the necessary ports are forwarded correctly to avoid timeout errors during the handshake phase.
Initiating the Connection via Command Prompt
Open
cmd.exe and navigate to the directory where you intend to store downloaded files or from which you wish to upload files. Type the following command structure to initiate the session:
sftp username@remote_host_ip
Replace
username with your account name on the target server and
remote_host_ip with the actual IP address or domain name. Upon first connection, you will be asked to accept the host key fingerprint. Type
yes to proceed. If you are using key-based authentication, ensure your private key is accessible via the
ssh-agent or specified via the
-i flag if invoking ssh directly, though the native sftp client often handles keys stored in
%USERPROFILE%\.ssh\id_rsa automatically. After entering your password or authenticating via key, you will see the
sftp> prompt, indicating a successful encrypted tunnel has been established.
Navigating the Remote File System
Once connected, the local file system commands like
dir or
ls apply only to your current Windows machine. To view files on the remote server, use the
ls command at the
sftp> prompt. This lists the contents of the remote user's home directory. To change directories on the remote server, use the
cd command followed by the path. For example,
cd /var/www/html moves you into the web root directory on the Linux server. Conversely, use
lcd (local change directory) to switch folders on your Windows machine without affecting the remote session state. Understanding this distinction prevents accidental uploads to unintended local paths.
Uploading and Downloading Files
To transfer files, use the
put command for uploading from your local machine to the remote server, and
get for downloading from the remote server to your local machine. The syntax is straightforward:
put local_file.txt uploads the file to the current remote directory. You can specify a different remote filename by appending it:
put local_file.txt remote_file.txt. For entire directories, use
put -r local_folder/ to recursively upload all contents. Similarly,
get -r remote_folder/ downloads a remote directory tree to your local machine. Always verify the integrity of large transfers by checking file sizes or checksums after completion.
Managing the Session and Troubleshooting
When finished, type
exit or press
Ctrl+D to close the SFTP session cleanly. If the connection hangs, check your network stability and ensure no intermediate firewalls are dropping idle packets. For more complex scenarios involving batch processing, you can create a script file containing multiple sftp commands and execute it using
sftp -b script.txt username@host. This automation is useful for scheduled backups or regular data synchronization tasks. For users preferring a graphical interface, tools like WinSCP offer similar functionality but may require additional configuration steps as detailed in our guide on
Using WinSCP or Other Programs (cmd) to Download from SFTP.
Frequently Asked Questions
Can I use SFTP if my server uses a non-standard SSH port?
Yes, you can specify a custom port by appending it to the hostname using an exclamation mark. The command format is
sftp -P port_number username@host. Note that capital
P is required for the port specification in the Windows client.
How do I resume a failed large file transfer?
The native Windows
sftp client does not support resuming broken transfers natively. You must restart the download or upload from the beginning. For resumable transfers, consider using
rsync over SSH or third-party tools that implement checkpointing capabilities.
What should I do if I get a "Permission Denied" error?
This error usually indicates incorrect username/password credentials or insufficient file system permissions on the remote server. Verify your login details first. If credentials are correct, check the ownership and permissions of the target directory on the remote server using
ls -l and adjust them via
chmod or
chown if you have sudo access.
Is SFTP faster than traditional FTP?
SFTP is generally slightly slower due to the overhead of encryption and decryption processes. However, the security benefits outweigh the minor performance penalty for most business applications. For extremely high-speed internal transfers where security is managed by network isolation, plain FTP might be considered, but it is strongly discouraged due to lack of encryption.
How can I automate SFTP connections in Windows Task Scheduler?
You can automate connections by creating a batch file that calls
sftp.exe with a batch input file containing your commands. Ensure that the task runs under a user account with appropriate permissions and that the script handles authentication securely, preferably using key-based login to avoid storing passwords in plain text within the script.