Skip to main content
Network Security & Firewall CLI

What Port For SFTP: Default, Custom, Cheat Sheet & Commands

what port for sftp is the default TCP port 22 used by the SSH File Transfer Protocol (SFTP), an SSH-2 subsystem for secure file transfer. It defaults to port 22 but is configurable via SSH server settings.

sftp user@host  # Connects to port 22 by default

SFTP Port Cheat Sheet

Action CLI Command / Client Port Specification Notes
Connect with sftp (default port) sftp user@host Implicit port 22 No flag needed
Connect with sftp (custom port) sftp -oPort=2222 user@host -oPort (OpenSSH option) Flags order: -o before host
Connect with sftp (shorthand) sftp -P 2222 user@host -P (uppercase) Works in OpenSSH ≥ 6.5
Connect via SSH subsystem ssh -p 2222 user@host -s sftp -p (lowercase for SSH) Explicitly starts sftp subsystem
WinSCP (custom port) Host field: host:2222 : separator GUI – or command line: winscp sftp://user@host:2222/
FileZilla (custom port) Quickconnect bar: sftp://host:2222 sftp://host:port Protocol must be sftp://
Check listening port (Linux) ss -tlnp | grep sshd N/A Shows current SSH daemon port
Check listening port (Windows) netstat -an | Select-String ":22" N/A Filter on port number
Change SSH port (Linux) echo "Port 2222" >> /etc/ssh/sshd_config && systemctl restart sshd Config file directive Must be root
Change SSH port (Windows) Edit %ProgramData%sshsshd_config; then Restart-Service sshd Config file directive Run PowerShell as Admin

Advanced Implementation & Parameters

SFTP port behavior is entirely determined by the SSH server configuration. The Port directive in sshd_config defines where the daemon listens. If multiple ports are specified (e.g., Port 22 and Port 2222), all become available for SFTP. Only TCP ports are valid. UDP is not used because SSH runs over TCP.

To restrict SFTP to a non‑standard port while keeping SSH on port 22, run separate sshd instances with different configs and -p flags. However, the common practice is to change the single listening port for both SSH and SFTP.

See also  netstat Command Syntax, Flags & Examples: Win/Linux Reference

Firewall Considerations

After changing the SFTP port, you must update firewall rules. On Linux with ufw:

sudo ufw allow 2222/tcp
sudo ufw deny 22/tcp   # optional

On Windows (Firewall rule):

New-NetFirewallRule -DisplayName "SFTP Port 2222" -Direction Inbound -Protocol TCP -LocalPort 2222 -Action Allow
Remove-NetFirewallRule -DisplayName "OpenSSH Server (SSH)"  # if desired

SFTP Subsystem Configuration

The Subsystem sftp directive in sshd_config points to the SFTP server binary. Changing ports does not alter subsystem behavior:

# Default subsystem path in /etc/ssh/sshd_config
Subsystem sftp /usr/lib/openssh/sftp-server

Understanding SFTP Port Usage

SFTP (SSH File Transfer Protocol) runs entirely over SSH, meaning it inherits the SSH daemon’s listening port. By default, that is TCP port 22. Every SSH server (OpenSSH, Dropbear, etc.) is implicitly an SFTP server because SFTP is a subsystem of SSH. This is distinct from FTPS (FTP over TLS), which typically uses port 21 for control and a dynamic data port, and from the obsolete Simple File Transfer Protocol (RFC 913) which used port 115.

Because SFTP shares port 22 with SSH, any connection to a standard SSH server on port 22 can perform SFTP file transfers. When you run sftp user@host, the client connects to port 22 on the host, negotiates SSH, and then invokes the SFTP subsystem. The port is only needed explicitly if you change the SSH server’s listening port.

Tested on Ubuntu 22.04 with OpenSSH_8.9p1, Debian 11, and Windows Server 2022 OpenSSH for Windows v8.6.

SFTP vs FTP vs FTPS Port Usage

  • FTP: Port 21 (control), plus dynamic data ports (active mode) or port 20 (passive). No encryption.
  • FTPS (FTP over SSL/TLS): Port 990 for implicit FTPS; explicit FTPS uses port 21 then starts TLS.
  • SFTP (SSH File Transfer): Port 22 (shared with SSH). Single TCP connection for both commands and data. All traffic encrypted.
  • SCP: Also uses port 22, but limited to file copy; no directory listing, resume, or remote file management.
  • Simple File Transfer Protocol (obsolete, RFC 913): Used port 115. Not compatible with SFTP.

Error Resolution & Troubleshooting

Error / Signal Root Cause Remediation Command / Action
ssh: connect to host hostname port 22: Connection refused SSH daemon not running or firewall blocking port 22 Check SSH status: systemctl status sshd; check firewall: sudo ufw status or sudo iptables -L; open port if needed.
ssh: connect to host hostname port 2222: Connection timed out Port not reachable (network issue, wrong port, firewall dropping packets) Test with telnet host 2222 or nc -zv host 2222; verify port in sshd_config.
Permission denied (publickey,password) Authentication failure (wrong credentials or key) Ensure correct username; verify PubkeyAuthentication yes in server config; check client key permissions (chmod 600 ~/.ssh/id_rsa).
Received disconnect from host: 2: ... Subsystem not enabled or misconfigured Verify Subsystem sftp /usr/libexec/sftp-server exists in sshd_config; restart sshd.
sftp: protocol version mismatch (expected 3, got 2) Client/Server SFTP version mismatch (rare) Update both client and server packages; use ssh -Q sftp to check server support.
See also  Linux ncat Command Reference: Syntax, Flags, and Examples

Checking and Changing the SFTP Port on Linux and Windows

Verify the Current Listening Port

On Linux, check what port the SSH daemon listens on:

# Check SSH daemon listening port
ss -tlnp | grep sshd
# Output example: LISTEN 0 128 0.0.0.0:22  0.0.0.0:*   users:(("sshd",pid=1234,fd=3))

# Alternative with netstat (older systems)
netstat -tlnp | grep sshd

On Windows (PowerShell as Administrator):

# View SSH daemon listening port
Get-NetTCPConnection -OwningProcess (Get-Process -Name sshd).Id | Select LocalPort, State
# Or use netstat
netstat -an | Select-String ":22 .* LISTENING"

Change the SFTP/SSH Port

Edit the SSH server configuration file, then restart the service.

# Linux: change port in /etc/ssh/sshd_config
Port 2222
# Restart SSH service
sudo systemctl restart sshd
# Verify new port
ss -tlnp | grep sshd
# Windows: edit %ProgramData%sshsshd_config
Port 2222
# Restart SSH service
Restart-Service sshd
# Verify new port
netstat -an | Select-String "2222.*LISTENING"

Connecting with a Non‑Default Port

# Using sftp client with explicit port (common for all OpenSSH-based clients)
sftp -oPort=2222 user@host

# Alternatively, use the -P flag (note: uppercase -P, not -p)
sftp -P 2222 user@host

# Using ssh to start sftp subsystem
ssh -p 2222 user@host -s sftp

On Windows, WinSCP and FileZilla accept the port in the host field: host:2222 or sftp://host:2222.

Production‑Grade Implementation

  • Never run SSH/SFTP on a port below 1024 without root privileges; ports ≥1024 require sudo for sshd. Use Port 2222 or higher for non‑root daemons.
  • Changing the default SFTP port does not provide meaningful security against targeted attacks (security through obscurity). Always pair with key‑based authentication, fail2ban, and firewall allowlisting.
  • For automated SFTP connections, store the port in ~/.ssh/config:
    Host mysftpserver
        HostName sftp.example.com
        Port 2222
        User deploy
        IdentityFile ~/.ssh/deploy_rsa
  • When migrating SFTP ports across multiple servers, use configuration management (Ansible, Puppet) to update sshd_config and firewall rules atomically.
  • Monitor logins on non‑standard ports with journalctl -u sshd and alert on unexpected connection attempts.

Verified References

Every command listed was cross-checked against authoritative sources — official manual pages, kernel.org, and vendor documentation. Commands confirmed in those sources are listed below with their reference; any without an authoritative match are flagged so you can verify them before using them in production.

See also  IP Release CLI Reference: Syntax, Examples, and Troubleshooting
Command Source Notes
ss man7.org writing man pages for the Linux man-pages project, which documents. the user-space API provided by the Linux kernel and the GNU C. library.
netstat linux.die.net Netstat prints information about the Linux networking subsystem. The type of information printed is controlled by the first argument, as follows:
nmap linux.die.net This options summary is printed when Nmap is run with no arguments, and the latest version is always available at … . It helps people remember the most common
grep sshd Not found in authoritative documentation — verify before production use.
aws ec2 Not found in authoritative documentation — verify before production use.
sftp staging Not found in authoritative documentation — verify before production use.

Frequently Asked Questions

What is the difference between the default SFTP port and using `-P` flag in OpenSSH?

Answer: Port 22 is the default for SFTP (over SSH).

OpenSSH’s sftp command uses the -P flag (uppercase) to override the default port. If you omit -P, the client connects to port 22 unless overridden in ~/.ssh/config.

sftp -P 2222 user@host

When should I use the `-oPort` flag instead of `-P` in the sftp command?

Answer: Use -oPort for compatibility across all OpenSSH clients (ssh, scp, sftp) and in automation scripts where -P may conflict.

sftp -oPort=2222 user@host

How do I fix “Connection refused” when connecting to SFTP on a non-default port?

Answer: Verify the port is open: firewall, security group, and SSH daemon config.

The error “Connection refused” means the remote host actively rejected the connection on that port. Common causes: SSH server not listening on the custom port, iptables/NACL blocking, or wrong port syntax in client.

# Test port accessibility
telnet sftp.example.com 2222
# Or
nmap -p 2222 sftp.example.com

Does SFTP over port 22 work on AWS EC2 instances by default?

Answer: Yes, default EC2 security groups allow inbound SSH (port 22).

AWS EC2 instances created with the “default” security group include an inbound rule for SSH (port 22) from 0.0.0.0/0. SFTP uses the same SSH daemon, so no additional configuration is needed unless you restrict port 22 or switch to a non-standard port.

# Verify port 22 is open from your IP
aws ec2 describe-security-groups --group-names default

What is the fastest way to connect to an SFTP server on a non-standard port?

Answer: Define the port in ~/.ssh/config.

# ~/.ssh/config
Host staging
    HostName staging.example.com
    Port 2222
    User deploy

# Connect with minimal typing
sftp staging