Quick take: sftp connects to a remote server over SSH and gives you an interactive prompt to browse directories, upload files with put, and download with get. Use sftp -P port user@host for non-standard ports.

What Is SFTP and How Does It Work?

sftp (Secure File Transfer Protocol) is an interactive file transfer program that runs over SSH. Unlike scp which is a one-shot copy tool, sftp opens a persistent session where you can browse the remote filesystem, manage files, and transfer in either direction — all with SSH encryption.

It is widely supported and the correct choice when you need to explore a remote server's directory structure before deciding what to transfer, or when transferring multiple files interactively.

Syntax and Connection

# Connect to a remote server
sftp user@hostname

# Connect to a non-standard SSH port (capital P)
sftp -P 2222 user@hostname

# Connect using a specific SSH key
sftp -i ~/.ssh/deploy_key user@hostname

# Connect using SSH config alias
sftp myserver

After connecting, you see a prompt: sftp>. The local and remote filesystems are both available. Commands prefixed with l (e.g. lls, lpwd) operate on the local side.

# Show current remote directory
sftp> pwd

# Show current local directory
sftp> lpwd

# List remote directory
sftp> ls -la

# List local directory
sftp> lls

# Change remote directory
sftp> cd /var/www/html

# Change local directory
sftp> lcd ~/Downloads

# Create a remote directory
sftp> mkdir uploads

# Remove a remote file
sftp> rm old_file.txt

Uploading Files

# Upload a single file to remote current directory
sftp> put local_file.txt

# Upload and rename on the remote side
sftp> put local_file.txt remote_name.txt

# Upload recursively (entire directory)
sftp> put -r ./my_project/

# Upload preserving timestamps and permissions
sftp> put -p config.conf

Downloading Files

# Download a single file to local current directory
sftp> get remote_file.txt

# Download and rename locally
sftp> get remote_file.txt local_copy.txt

# Download recursively (entire directory)
sftp> get -r /var/backups/weekly/

# Download preserving timestamps
sftp> get -p database_dump.sql

Common Options

OptionDescription
-P portSpecify SSH port (capital P)
-i identity_fileUse a specific private key
-rRecursive — transfer directories
-pPreserve file timestamps and permissions
-b batchfileRun commands from a file (batch mode)
-l limitLimit bandwidth in kbps
-CEnable compression for slow connections
-vVerbose — debug connection issues

sftp vs scp

scp is a one-shot tool: specify source and destination on the command line, and it copies then exits. It is faster for simple, known transfers because there is no session overhead.

sftp is interactive: connect once, then navigate and transfer multiple files across the session. Use sftp when you need to browse the remote server first, or when transferring many files in different locations.

For scripted, non-interactive transfers between well-known paths, scp is simpler. For anything that requires exploration or multiple operations, sftp wins.

Batch Mode and Automation

sftp can run commands from a file using -b, which enables basic automation:

# Create a batch command file
cat > upload_commands.txt <

Batch mode exits with an error code if any command fails, making it suitable for use in shell scripts. For more complex automation over SSH, consider rsync which handles incremental sync and is more script-friendly.

Using SSH Config with sftp

If you use an SSH config file (~/.ssh/config), sftp respects it automatically — connect with a short alias instead of specifying host, user, port, and key every time:

# ~/.ssh/config
Host webserver
    HostName 192.168.1.50
    User deploy
    Port 2222
    IdentityFile ~/.ssh/deploy_rsa
    ServerAliveInterval 60
# Connect with just:
sftp webserver

# Batch mode also works:
sftp -b commands.txt webserver

Full Batch Mode Script Example

Batch mode enables automated, non-interactive file transfers from a cron job or deployment script. Create a file of sftp commands and pass it with -b:

#!/bin/bash
# deploy_assets.sh — Upload build artifacts to web server

BATCH_FILE=$(mktemp)
cat > "$BATCH_FILE" <<'EOF'
cd /var/www/html
lcd ./dist
put -r .
chmod 644 index.html
chmod 755 assets
bye
EOF

sftp -b "$BATCH_FILE" deploy@webserver
EXIT_CODE=$?
rm -f "$BATCH_FILE"

if [ $EXIT_CODE -eq 0 ]; then
    echo "Deploy successful"
else
    echo "Deploy failed with exit code $EXIT_CODE" >&2
    exit 1
fi

sftp vs rsync vs scp — When to Use Each

ToolBest forIncremental sync
sftpInteractive browsing and manual transfers; SFTP-only hosts (cloud storage)No
scpQuick one-shot file copies in scripts when path is knownNo
rsyncBackups and deployments — only transfers changed bytesYes

For deployments where only changed files need to be pushed, rsync is significantly faster because it transfers only deltas. sftp wins when you need an interactive session to explore the remote server first, or when connecting to a server that only exposes the SFTP subsystem (common with managed cloud storage and SFTP hosting providers).

Common sftp Errors and Fixes

ErrorCauseFix
Connection refusedSSH not running or wrong portUse -P port; check systemctl status sshd on server
Permission denied (publickey)Wrong key or not in authorized_keysVerify key with ssh-copy-id; debug with sftp -v
Broken pipe / connection resetIdle timeout by firewall or serverAdd ServerAliveInterval 60 to ~/.ssh/config
Permission denied on uploadRemote user lacks write permissionCheck remote dir with ls -la inside sftp session
No sftp subsystemSFTP subsystem not configured on serverAdd Subsystem sftp /usr/lib/openssh/sftp-server to server's /etc/ssh/sshd_config

Reference: sftp man page — man7.org. Tested on Ubuntu 22.04 LTS with OpenSSH 8.9.

Final Thoughts

sftp is the right tool when you need an interactive file transfer session over SSH — browsing directories, uploading, downloading, and managing files without writing a full rsync command. Learn the handful of interactive commands (ls, cd, put, get) and you will handle the majority of remote file transfer scenarios with one tool.

FAQ: sftp Command in Linux

How do I upload a file with sftp?+

Connect with sftp user@host, navigate to the target directory with cd, then use put localfile.txt to upload. Use put -r directory/ to upload a whole folder recursively.

How do I download a file with sftp?+

Connect to the server, navigate with cd to the remote directory, then run get remotefile.txt to download it to your current local directory. Use get -r folder/ for recursive download.

What is the difference between sftp and scp?+

scp is a simple one-shot copy command. sftp is interactive — you connect and get a prompt where you can browse directories, upload, download, and manage files. sftp is more flexible for browsing unknown directory structures.

How do I use sftp with a non-standard SSH port?+

Use sftp -P 2222 user@host where 2222 is your custom port. Note the capital P — sftp uses -P unlike ssh which uses lowercase -p.

How do I use sftp with an SSH key?+

Run sftp -i ~/.ssh/id_rsa user@host to specify a key file. If your key is already loaded in ssh-agent or listed in ~/.ssh/config, sftp picks it up automatically.

Need help with Linux servers or infrastructure?

Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.

Hire Me for Support