SFTP (SSH File Transfer Protocol) moves files over an encrypted SSH connection on a single port, usually TCP 22. FTP is the older File Transfer Protocol: it uses a control connection on port 21 plus separate data connections, and it sends usernames, passwords and files in plain text unless you add TLS, which turns it into FTPS.
For almost every new project, choose SFTP. It's encrypted by default, it passes through firewalls on one port, and it's built into OpenSSH, so it's already on most Linux servers and available on Windows through the OpenSSH Server feature. Run an FTP server, preferably FTPS, only when a legacy system or a partner insists on it.
FTP, FTPS and SFTP are not three flavours of one thing
The names sound alike, but SFTP is not "FTP with security". It's a completely different protocol that runs inside SSH. FTPS is the one that's actually FTP with TLS.
Feature | FTP | FTPS | SFTP |
|---|
Underlying protocol | FTP | FTP over TLS | SSH |
Default ports | 21 (control) + 20 or a passive data range | 21 with AUTH TLS (explicit) or 990 (implicit), plus a passive data range | 22 only |
Encryption | None. Credentials and data in clear text | TLS for control and data | SSH encryption for everything |
Authentication | Username and password | Password, optionally client certificates | Password and/or SSH keys |
Firewall behavior | Needs a passive port range; NAT can break it | Harder: firewalls can't inspect the encrypted control channel, so the passive range must be opened explicitly | Easy: one TCP connection |
Typical use today | Legacy only, never over the internet | Partners or devices that require FTP semantics | Default choice for secure file transfer |
The firewall row is where most FTP pain comes from. FTP opens a new connection for every directory listing and file transfer. In passive mode, which almost every client uses, the server tells the client which port to connect to for data:
Client → Server:21 USER / PASS / PASV (control)
Server → Client "connect to me on port 40017"
Client → Server:40017 file data (data)
So the server's firewall has to allow port 21 and a range of passive ports. With plain FTP, some firewalls read the control channel and open the data port automatically. With FTPS the control channel is encrypted, so they can't, and you have to configure the passive range on both the server and the firewall. SFTP has none of this.
An SFTP server on Linux with OpenSSH
Most Linux distributions already ship OpenSSH. The setup below creates an SFTP-only group whose users are locked into their own directory (a chroot jail), can't get a shell, and log in with SSH keys.
Install OpenSSH Server
# Debian / Ubuntu
sudo apt update && sudo apt install -y openssh-server
# RHEL / Rocky / AlmaLinux / Fedora
sudo dnf install -y openssh-server
sudo systemctl enable --now sshd
Create the group, the user and the jail
sudo groupadd sftpusers
sudo useradd -M -g sftpusers -s /usr/sbin/nologin partner1
# The chroot directory must be owned by root and not writable by anyone else
sudo mkdir -p /srv/sftp/partner1/uploads
sudo chown root:root /srv/sftp/partner1
sudo chmod 755 /srv/sftp/partner1
# Give the user a writable folder inside the jail
sudo chown partner1:sftpusers /srv/sftp/partner1/uploads
sudo chmod 750 /srv/sftp/partner1/uploads
Get the ownership right, because it's the most common stumbling block. If the chroot directory is writable by the user, OpenSSH refuses the login and logs a "bad ownership or modes for chroot directory" error. That's why the user writes into uploads, not into the jail root.
Set up key authentication
On the client, generate a key pair. Ed25519 keys are short and strong.
ssh-keygen -t ed25519 -C "partner1" -f ~/.ssh/partner1_ed25519
On the server, store authorized keys outside the jail, in a root-owned location, so the user can't modify them:
sudo mkdir -p /etc/ssh/authorized_keys
sudo cp partner1_ed25519.pub /etc/ssh/authorized_keys/partner1
sudo chown root:root /etc/ssh/authorized_keys/partner1
sudo chmod 644 /etc/ssh/authorized_keys/partner1
Configure sshd
Edit /etc/ssh/sshd_config. Make sure the SFTP subsystem uses the built-in server, then add a Match block at the end of the file:
Subsystem sftp internal-sftp
Match Group sftpusers
ChrootDirectory /srv/sftp/%u
ForceCommand internal-sftp
AuthorizedKeysFile /etc/ssh/authorized_keys/%u
PasswordAuthentication no
AllowTcpForwarding no
X11Forwarding no
PermitTunnel no
If the file already has a Subsystem sftp /usr/lib/openssh/sftp-server line, replace it rather than adding a second one. internal-sftp runs inside sshd, so the jail doesn't need any binaries or libraries copied into it. Every directive here is documented in the sshd_config manual.
Test and restart
sudo sshd -t # validate the configuration
sudo systemctl restart ssh # Debian/Ubuntu (the unit is "sshd" on RHEL)
# From the client
sftp -i ~/.ssh/partner1_ed25519 [email protected]
sftp> cd uploads
sftp> put report.csv
Keep your current SSH session open while you test, so a mistake doesn't lock you out of the server. For a fuller enterprise walkthrough, see Building a Secure SFTP Server with Chroot Jail on Linux.
An SFTP server on Windows with OpenSSH Server
Windows includes Microsoft's port of OpenSSH, and SFTP comes with it. Microsoft's OpenSSH getting-started guide lists Windows Server 2019 or Windows 10 build 1809 as the minimum. From Windows Server 2025, OpenSSH is installed by default and you only need to enable the sshd service.
# Run in an elevated PowerShell session
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Start-Service sshd
Set-Service -Name sshd -StartupType 'Automatic'
# Setup normally creates this rule; verify it exists
Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP"
The server reads %ProgramData%\ssh\sshd_config. On Windows, ChrootDirectory is supported for SFTP sessions only, so always pair it with ForceCommand internal-sftp. Group names must be lowercase.
Match Group sftpusers
ChrootDirectory D:\SFTP\%u
ForceCommand internal-sftp
AllowTcpForwarding no
PasswordAuthentication no
New-LocalGroup -Name "sftpusers"
$pw = Read-Host -AsSecureString "Password for partner1"
New-LocalUser -Name "partner1" -Password $pw
Add-LocalGroupMember -Group "sftpusers" -Member "partner1"
New-Item -ItemType Directory -Path "D:\SFTP\partner1" -Force
Restart-Service sshd
Keys work slightly differently on Windows. For standard users, the public key goes in C:\Users\<user>\.ssh\authorized_keys by default. That profile folder is only created at the user's first sign-in, so for SFTP-only accounts I'd set AuthorizedKeysFile to an absolute, admin-controlled path inside the Match block instead.
Members of the Administrators group are different again: the server uses %ProgramData%\ssh\administrators_authorized_keys, and that file must grant access only to SYSTEM and Administrators.
icacls.exe "C:\ProgramData\ssh\administrators_authorized_keys" /inheritance:r /grant "Administrators:F" /grant "SYSTEM:F"
Windows OpenSSH supports only password and publickey authentication. It reads the configuration file only at startup, so restart sshd after every change.
When you have to run FTP
If a partner or device genuinely needs FTP, run FTPS with TLS required and a fixed passive port range. Never expose plain FTP to the internet.
IIS FTP on Windows
On Windows, use the FTP Server role in IIS:
# Windows Server
Install-WindowsFeature Web-Ftp-Server -IncludeManagementTools
# Windows 10/11
Enable-WindowsOptionalFeature -Online -FeatureName IIS-FTPServer, IIS-FTPSvc, IIS-ManagementConsole
- Open IIS Manager, right-click Sites and choose Add FTP Site.
- Set a name and a physical path, such as
D:\FTP. - Bind to port 21, choose an SSL certificate, and select Require SSL so the site is FTPS only.
- Under authentication, enable Basic and disable Anonymous.
- Add an authorization rule granting Read or Read/Write to a specific Windows group.
- At the server level, open FTP Firewall Support. Set a Data Channel Port Range (for example 50000-50100) and, if the server is behind NAT, its external IP.
- Restart the FTP service with
Restart-Service ftpsvc, and open port 21 plus the passive range in Windows Firewall and any network firewall. - Enable FTP User Isolation if each user should see only their own folder.
vsftpd on Linux
On Linux, vsftpd ("very secure FTP daemon") is the most common FTP server.
sudo apt install -y vsftpd # or: sudo dnf install -y vsftpd
sudo cp /etc/vsftpd.conf /etc/vsftpd.conf.bak
Here's a hardened /etc/vsftpd.conf with TLS and a fixed passive range. On RHEL-based systems the file is /etc/vsftpd/vsftpd.conf.
anonymous_enable=NO
local_enable=YES
write_enable=YES
chroot_local_user=YES
allow_writeable_chroot=NO
pasv_enable=YES
pasv_min_port=40000
pasv_max_port=40100
# pasv_address=203.0.113.25 # public IP if behind NAT
ssl_enable=YES
rsa_cert_file=/etc/ssl/certs/ftp.example.com.pem
rsa_private_key_file=/etc/ssl/private/ftp.example.com.key
force_local_logins_ssl=YES
force_local_data_ssl=YES
sudo systemctl restart vsftpd
sudo ufw allow 21/tcp
sudo ufw allow 40000:40100/tcp
With chroot_local_user=YES and allow_writeable_chroot=NO, the user's home directory must not be writable. Give them a writable subfolder, exactly as with the SFTP jail.
Connecting with WinSCP, FileZilla and the command line
Client | Platforms | Protocols | Good for |
|---|
WinSCP | Windows | SFTP, SCP, FTP/FTPS, WebDAV, S3 | GUI use, scripting, and a .NET assembly for automation |
FileZilla | Windows, macOS, Linux | SFTP, FTP/FTPS | Cross-platform GUI; decline any optional bundled offers in the installer |
OpenSSH sftp / scp | Linux, macOS, Windows 10+ | SFTP | Scripts and quick transfers |
curl
| All | FTP, FTPS, SFTP (if built with libssh2) | One-line uploads in pipelines |
lftp
| Linux, macOS | FTP/FTPS, SFTP | Mirroring folders and retries |
For scripted transfers, the OpenSSH client in batch mode and curl cover most needs:
# Batch SFTP upload, non-interactive
sftp -i ~/.ssh/partner1_ed25519 -b - [email protected] <<'EOF'
cd uploads
put daily-report.csv
bye
EOF
# FTPS upload with curl (explicit TLS)
curl --ssl-reqd -T daily-report.csv -u user:pass ftp://ftp.example.com/uploads/
SFTP from C# with SSH.NET
SSH.NET is the most widely used open-source SSH and SFTP library for .NET. As of October 2026 the latest NuGet release is 2026.0.0, targeting .NET Framework 4.6.2+, .NET Standard 2.0 and .NET 8+. Recent versions include async methods such as ConnectAsync, UploadFileAsync, DownloadFileAsync and ListDirectoryAsync. Commercial libraries exist if you need vendor support, but SSH.NET is where I'd start.
dotnet add package SSH.NET
This example authenticates with a key, verifies the server's host key, then uploads, lists and downloads:
using Renci.SshNet;
const string host = "sftp.example.com";
const string user = "partner1";
// SHA-256 host key fingerprint, as printed by: ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub
// (copy the part after "SHA256:")
const string expectedFingerprint = "REPLACE_WITH_SERVER_FINGERPRINT";
using var key = new PrivateKeyFile(@"C:\keys\partner1_ed25519");
var connectionInfo = new ConnectionInfo(host, 22, user,
new PrivateKeyAuthenticationMethod(user, key));
using var client = new SftpClient(connectionInfo);
// Never skip host key verification in production
client.HostKeyReceived += (sender, e) =>
{
e.CanTrust = e.FingerPrintSHA256 == expectedFingerprint;
};
await client.ConnectAsync(CancellationToken.None);
// Upload
await using (var input = File.OpenRead(@"C:\data\daily-report.csv"))
{
await client.UploadFileAsync(input, "/uploads/daily-report.csv");
}
// List
await foreach (var file in client.ListDirectoryAsync("/uploads", CancellationToken.None))
{
if (!file.IsDirectory)
Console.WriteLine($"{file.Name} {file.Length:N0} bytes");
}
// Download
await using (var output = File.Create(@"C:\data\ack.csv"))
{
await client.DownloadFileAsync("/uploads/ack.csv", output);
}
client.Disconnect();
Before this goes to production, load the private key and fingerprint from a secret store such as Azure Key Vault rather than source code, and wrap transfers in a retry policy for transient network failures. It's also worth uploading to a temporary name and renaming with RenameFileAsync when the transfer completes, so the receiving system never picks up a half-written file.
Plain FTP and FTPS in .NET
FtpWebRequest belongs to the WebRequest family that has been obsolete (warning SYSLIB0014) since .NET 6. For FTP or FTPS, the open-source FluentFTP library is the usual replacement:
using FluentFTP;
await using var ftp = new AsyncFtpClient("ftp.example.com", "user", "pass");
ftp.Config.EncryptionMode = FtpEncryptionMode.Explicit; // FTPS
await ftp.AutoConnect();
await ftp.UploadFile(@"C:\data\daily-report.csv", "/uploads/daily-report.csv");
For a fuller walkthrough of the FTP side, see Upload Files to FTP Server in .NET Core.
Skipping the server: managed SFTP on AWS and Azure
If you'd rather not patch and monitor a server at all, both major clouds offer managed SFTP endpoints that write straight into object storage.
Feature | AWS Transfer Family | Azure Blob Storage SFTP |
|---|
Protocols | SFTP, FTPS, FTP (FTP only inside a VPC), AS2 | SFTP |
Storage backend | Amazon S3 or Amazon EFS | Blob Storage with hierarchical namespace enabled |
Identities | Service-managed users, or a custom identity provider | Local users (password or SSH keys); Microsoft Entra ID also supported |
Pricing model | Per hour per enabled protocol, plus per GB transferred | Hourly charge while SFTP is enabled, plus normal storage and transaction costs |
AWS's pricing page lists $0.30 per hour per enabled protocol and $0.04 per GB uploaded or downloaded; check it for your region. Microsoft's Azure Blob SFTP documentation says SFTP requires hierarchical namespace, allows up to 50,000 local users per storage account and 10 public keys per user, and recommends enabling SFTP only while you're using it, to avoid the hourly charge. No VM is involved: you enable SFTP on the storage account, create local users, and connect with any compatible client on port 22.
Two common errors and how to fix them
FTP logs in, but directory listings fail
That's the classic passive mode problem. The control connection on port 21 works, but the data connection to the passive port range is blocked by a firewall or NAT. Configure a fixed passive range on the server, open it in every firewall along the way, and set the server's public IP for passive replies (pasv_address in vsftpd, or the external IP in IIS FTP Firewall Support).
A chrooted SFTP user is disconnected immediately
Usually the chroot directory, or one of its parents, isn't owned by root or is writable by the user or group. Make the chroot directory root:root with mode 755 and give the user a writable subfolder. The SSH log (journalctl -u ssh or /var/log/auth.log) shows the exact reason.
Hardening checklist
Most of the security work is already in the configurations above, but it's worth checking each server against this list.
- Prefer SFTP and disable plain FTP. If FTP must run, require TLS for both logins and data.
- Use SSH keys and disable passwords for automated partners. Rotate keys, and remove them when a partner leaves.
- Chroot every user and give each one only the folder they need.
- Deny shell access with
ForceCommand internal-sftp and a nologin shell. - Restrict by IP with firewall rules or
AllowUsers [email protected]/24 when partners have fixed addresses. - Block brute force with fail2ban or your firewall's rate limiting. A non-default port reduces log noise, but it isn't security.
- Remove legacy algorithms such as
ssh-rsa (SHA-1) signatures and old Diffie-Hellman groups. - Verify host keys in every client and script, and never accept any key automatically.
- Log and monitor logins and transfers, and alert on unusual volume.
- Scan uploads for malware before downstream systems process them, and keep the landing folder separate from processing folders.
- Patch OpenSSH, vsftpd and IIS regularly.
If the same Linux box also hosts your application, Securing Your Self-Hosted ASP.NET Core & SQL Server Stack on Ubuntu covers firewall and SSH hardening in more depth.
Join the conversation! Your thoughts help the community grow.