Guide
How to use SFTP: commands and file transfers over SSH
SFTP (the SSH File Transfer Protocol) copies files over an SSH connection, on port 22 with the same sign-in. Run sftp user@server, then use ls and cd to find your way, get to download and put to upload. Type exit when you're done.
Linux, macOS and Windows 10 and 11 all have the sftp command. Here's how to use it, every common command in one table, how it compares with scp and rsync, and how to do the same by dragging files between two panes.
- sftp commands
- scp and rsync compared
- Two-pane SFTP app
Updated · OpenSSH on Linux, macOS and Windows 10 and 11
What SFTP is (and isn't)
SFTP is part of SSH. It uses the SSH connection, so it's encrypted, it runs on the SSH port (22 unless the server uses another SSH port), and it signs in with the same accounts, passwords and keys. Any server you can reach with ssh almost always takes sftp too: the SSH server on Linux, macOS and Windows includes it.
Despite the name, it isn't FTP. FTP is an older, unencrypted protocol on port 21, and FTPS is FTP with TLS added. If you have the choice, use SFTP. Don't have an SSH server yet? Enable SSH on Ubuntu or Debian first.
Step-by-step: transfer files with sftp
Step 1: Connect
In a terminal (or PowerShell on Windows), start in the folder where you want downloads to land, then connect:
sftp admin@server.example.com sftp -P 2222 admin@server.example.com # another port: capital P sftp -i ~/.ssh/id_ed25519 admin@server.example.com # a specific key
The first time, check the server's key fingerprint before you type
yes, as with ssh. The prompt changes tosftp>. An old NAS or device that answers with "Unable to negotiate" needs the fix for no matching host key type found.Step 2: Find the folder on the server
pwdshows where you are,ls -lwhat's there, andcd /var/www/sitemoves you.Step 3: Pick the folder on your computer
The same commands with an
lin front work on your side:lpwd,llsandlcd ~/Downloads.Step 4: Upload with put
put report.pdfuploads one file,put -r photosa whole folder (with older sftp versions, make the folder on the server first withmkdir photos).Step 5: Download with get
get backup.tar.gzdownloads a file,get -r logsa folder. If a big download is cut off, reconnect andreget backup.tar.gzcarries on where it stopped.Step 6: Close the session
Type
exit.
SFTP commands
Everything you'll use day to day. Commands without an l work on the server; with an l in front, on your computer.
| Command | What it does |
|---|---|
ls, ls -l | List the current folder on the server (-l with sizes and dates). |
lls | List the current folder on your computer. |
cd /var/www | Change folder on the server. |
lcd ~/Downloads | Change folder on your computer. |
pwd, lpwd | Show where you are on the server, and on your computer. |
get file.txt | Download a file to the current local folder. |
get -r folder | Download a whole folder. |
put file.txt | Upload a file to the current server folder. |
put -r folder | Upload a whole folder. |
get *.log | Download every file that matches a pattern (put works the same way). |
reget file.iso, reput file.iso | Resume a download or upload that was cut off. |
mkdir name, lmkdir name | Make a folder on the server, or on your computer. |
rm file, rmdir folder | Delete a file, or an empty folder, on the server. |
rename old new | Rename or move a file on the server. |
chmod 644 file | Change a file's permissions on the server. |
df -h | Free space on the server's disk (if the server supports it). |
!command | Run a command on your own computer without leaving sftp. |
help (or ?) | List every command. |
exit (or bye, quit) | Close the session. |
One-line copies and scripts
You don't always need the interactive prompt:
# download one file to the current folder sftp admin@server.example.com:/var/log/syslog # run a list of commands from a file sftp -b upload.txt admin@server.example.com
A batch file is just sftp commands, one per line (for example cd /var/www/site then put -r dist/*). Batch mode can't ask for a password, so set up key sign-in first. A server you've saved in your SSH config file works by its short name: sftp web.
Other ways to copy files over SSH: scp and rsync
| Tool | Best for | Good to know |
|---|---|---|
| sftp | Looking around and picking files; several transfers in one session. | Resumes with reget and reput. Scriptable with -b. |
| scp | One quick copy you already know the path of. | Since OpenSSH 9.0 it uses the SFTP protocol underneath. -r for folders, -P for the port. |
| rsync | Folders you copy again and again, backups, big trees. | Only sends what changed. Needs rsync installed on both ends. |
scp report.pdf admin@server.example.com:/tmp/ scp admin@server.example.com:/var/log/syslog ./ scp -r photos admin@server.example.com:backup/ rsync -avz --progress site/ admin@server.example.com:/var/www/site/
With rsync, the slash at the end of site/ matters: it copies what's inside the folder rather than the folder itself. Copying between two servers rather than to your own computer? See how to copy files between two servers.
Transfer files with an SFTP app instead
Commands are quick once you know them, but for browsing, dragging is easier. The RemoteAccessApp SSH and SFTP client has a free two-pane file manager in its Console for Windows and Linux:
- Open a server's files: add the server on Connections as an SFTP host, or click Files on a saved SSH host or above its terminal.
- Two panes: This PC on one side, the server on the other. Drag files between them, or select them and click Upload or Download.
- No surprises: you're asked Replace existing items? before anything is overwritten, and the Transfers list shows each file, with the reason if one fails.
- The same server key check and password or key sign-in as the SSH terminal. FTP and FTPS servers open in the same file manager.
It's free for up to 5 saved hosts: download RemoteAccessApp. The help article on copying files with SFTP and FTP covers every button.
Give someone SFTP-only access
To let a user upload files without a shell, lock their account to SFTP in a folder of its own. Add this at the end of /etc/ssh/sshd_config:
Match User uploader
ForceCommand internal-sftp
ChrootDirectory /srv/sftp/uploader
AllowTcpForwarding no
X11Forwarding noThe folder they're locked into must belong to root and not be writable by anyone else, so give them a folder inside it to write to:
sudo mkdir -p /srv/sftp/uploader/files sudo chown root:root /srv/sftp/uploader sudo chmod 755 /srv/sftp/uploader sudo chown uploader:uploader /srv/sftp/uploader/files sudo sshd -t && sudo systemctl restart ssh # sshd on Fedora and RHEL
Test it
sftp uploader@server should land in / with only the files folder, and ssh uploader@server should refuse a shell.Questions, answered
Related
- SSH & SFTP clientTerminal tabs, an SFTP and FTP file manager, key sign-in and one command to every server.Learn more
- Copy files between serversA step-by-step server-to-server copy over SFTP or FTP, with nothing saved on your own disk.Read the guide
- SSH port 22SSH and SFTP use port 22. Connect on another port with ssh -p, change the port on a Linux or Windows server, and check it's open.Read the guide
- Passwordless SSH with keysMake an SSH key, copy it to the server with ssh-copy-id (or one PowerShell line on Windows), get the permissions right and sign in without a password.Read the guide
- SSH to a Linux serverConnect to a Linux server from Windows or another Linux machine, check its key, and copy files.Read the guide
- Server-to-server transfersCopy files between two servers, or to many at once, streamed with no temporary copy, with resume and verify.Learn more
