sftp Command
What is Linux sftp Command?
sftp (SSH File Transfer Protocol) is an interactive command-line program for securely transferring files over SSH connections. Unlike scp which is designed for simple file copying, sftp provides a full interactive session with file management capabilities similar to traditional FTP but with SSH encryption. sftp runs over the SSH protocol and provides all the functionality of FTP with the security benefits of SSH. It allows you to navigate directories, list files, and perform various file operations on both local and remote systems within a single secure session. From the man page: > sftp is a file transfer program, similar to ftp, which performs all operations over an encrypted ssh transport. It may also use many features of ssh, such as public key authentication and compression. ## Key Features
Secure transfer: All data is encrypted using SSH- Interactive session: Provides a command-line interface for file operations - Full file management: Create, delete, rename files and directories - Resume capability: Can resume interrupted transfers - Batch operations: Supports scripted operations - Cross-platform: Available on all Unix-like systems ## sftp Syntax
sftp [options] [user@]hostname[:directory]
Basic connection patterns: -sftp user@remote-server
-sftp -P 2222 user@remote-server
-sftp user@remote-server:/remote/directory
## Connecting to sftp ### Basic Connection Connect to a remote server:sftp [email protected]
### Connect with Custom Port Use a non-standard SSH port:sftp -P 2222 [email protected]
### Connect with SSH Key Use a specific private key:sftp -i ~/.ssh/my-key [email protected]
### Connect to Specific Directory Start in a specific remote directory:sftp [email protected]:/home/user/documents
## Essential sftp Commands ### Navigation Commands #### Remote Navigation -pwd
- Show current remote directory -ls
- List remote directory contents -ls -la
- List with detailed information -cd directory
- Change remote directory -cd ..
- Go to parent directory #### Local Navigation -lpwd
- Show current local directory -lls
- List local directory contents -lls -la
- List local files with details -lcd directory
- Change local directory ### File Transfer Commands #### Download Files (Remote to Local) Download a single file:sftp> get filename.txt
Download with custom local name:sftp> get remote-file.txt local-file.txt
Download to specific local directory:sftp> get filename.txt /local/path/
Download multiple files:sftp> mget *.txt sftp> mget file1.txt file2.txt file3.txt
#### Upload Files (Local to Remote) Upload a single file:sftp> put localfile.txt
Upload with custom remote name:sftp> put local.txt remote.txt
Upload to specific remote directory:sftp> put localfile.txt /remote/path/
Upload multiple files:sftp> mput *.log sftp> mput file1.txt file2.txt file3.txt
### Directory Operations #### Create and Remove Directories Create remote directory:sftp> mkdir newdirectory
Create local directory:sftp> lmkdir newlocaldirectory
Remove remote directory:sftp> rmdir directory
Remove local directory:sftp> lrmdir localdirectory
#### File and Directory Management Remove remote file:sftp> rm filename.txt
Rename remote file:sftp> rename oldname.txt newname.txt
View file contents:sftp> more filename.txt
Get detailed file information:sftp> stat filename.txt
## Advanced sftp Examples ### Resume Interrupted Transfer If a transfer is interrupted, sftp can resume from where it left off:sftp> reget large-file.zip
### Set Transfer Mode Set binary transfer mode (for non-text files):sftp> binary
Set ASCII transfer mode (for text files):sftp> ascii
### Change File Permissions Change permissions on remote file:sftp> chmod 755 script.sh sftp> chmod 644 document.txt
### Symbolic Links Create symbolic link:sftp> symlink target linkname
Read symbolic link:sftp> readlink linkname
## Batch Operations and Scripting ### Using sftp with Batch File Create a batch filecommands.txt
:cd /remote/directory put localfile.txt get remotefile.txt quit
Run sftp with batch file:sftp -b commands.txt user@remote-server
### One-liner Commands Execute single command:echo "put file.txt" | sftp user@remote-server
Multiple commands:echo -e "cd /uploads\nput *.txt\nquit" | sftp user@remote-server
## Interactive Session Examples ### Complete File Transfer Session$ sftp user@remote-server Connected to remote-server. sftp> pwd Remote working directory: /home/user sftp> lpwd Local working directory: /home/localuser sftp> ls file1.txt file2.txt directory1 sftp> cd directory1 sftp> put localfile.txt Uploading localfile.txt to /home/user/directory1/localfile.txt localfile.txt 100% 1024 1.0KB/s 00:01 sftp> get remotefile.txt Fetching /home/user/directory1/remotefile.txt to remotefile.txt remotefile.txt 100% 2048 2.0KB/s 00:01 sftp> quit
### Working with Wildcards Upload all text files:sftp> mput *.txt
Download all log files:sftp> mget *.log
Upload files matching pattern:sftp> mput backup_*
## Practical Use Cases ### Website Maintenance Connect and upload website files:sftp [email protected] sftp> cd /var/www/html sftp> put -r website-updates/* sftp> chmod 755 scripts/*.cgi sftp> quit
### Database Backup Management Download database backups:sftp dbadmin@backup-server sftp> cd /backups/mysql sftp> ls -la sftp> get daily-backup-$(date +%Y%m%d).sql sftp> quit
### Log File Collection Collect logs from multiple directories:sftp admin@log-server sftp> cd /var/log sftp> mget *.log sftp> cd ../nginx sftp> mget access.log error.log sftp> quit
### Configuration Management Deploy configuration files:sftp config@production-server sftp> cd /etc/nginx/sites-available sftp> put mysite.conf sftp> cd /etc/ssl/certs sftp> put certificate.pem sftp> quit
## Security and Authentication ### SSH Key Authentication Generate SSH key pair:ssh-keygen -t rsa -b 4096 -C "[email protected]"
Copy public key to server:ssh-copy-id user@remote-server
Connect using key:sftp -i ~/.ssh/id_rsa user@remote-server
### Using SSH Config Create~/.ssh/config
:Host myserver HostName remote-server.com User myuser Port 2222 IdentityFile ~/.ssh/myserver-key
Connect using config:sftp myserver
## Performance and Optimization ### Compression Enable compression for slow connections:sftp -C user@remote-server
### Connection Options Set connection timeout:sftp -o ConnectTimeout=30 user@remote-server
Disable host key checking (not recommended for production):sftp -o StrictHostKeyChecking=no user@remote-server
### Large File Handling For large files, use binary mode and show progress:sftp> binary sftp> progress sftp> get large-file.zip
## Troubleshooting ### Common Error Messages “Permission denied (publickey)” - Check SSH key authentication - Verify public key is installed on server - Test SSH connection first:ssh user@server
“Connection refused” - Verify SSH service is running - Check firewall settings - Confirm correct hostname and port “No such file or directory” - Check file paths and names - Usels
to verify files exist - Remember paths are case-sensitive ### Debugging Connection Issues Enable verbose mode:sftp -v user@remote-server
Test SSH connectivity:ssh -T user@remote-server
Check SSH configuration:ssh -F ~/.ssh/config user@remote-server
## sftp vs Other Tools ### sftp vs scp - sftp: Interactive session, file management, resume capability - scp: Command-line, good for scripts, one-time transfers ### sftp vs ftp - sftp: Encrypted, secure, uses SSH - ftp: Unencrypted, legacy protocol, security risks ### sftp vs rsync - sftp: Interactive, manual operations, good for selective transfers - rsync: Automated, incremental, better for synchronization ## Command Reference ### Essential Commands Summary | Command | Description | |———|————-| |ls
| List remote files | |lls
| List local files | |pwd
| Show remote directory | |lpwd
| Show local directory | |cd
| Change remote directory | |lcd
| Change local directory | |get
| Download file | |put
| Upload file | |mget
| Download multiple files | |mput
| Upload multiple files | |mkdir
| Create remote directory | |rmdir
| Remove remote directory | |rm
| Remove remote file | |rename
| Rename remote file | |chmod
| Change permissions | |quit
| Exit sftp session | ## sftp Command Manual / Help We can useman
andinfo
command to see the manual page of sftp command. sftp command also have--help
option to show list of options. To open man page for sftp command we can use command below. To exit man or info page you can pressq
.man sftp
To open info page for sftp command we can use command below.info sftp
To open help page from sftp command we can run command below.sftp --help
You can also get help within an sftp session:sftp> help sftp> ?
## References You can find more information about sftp from the following links:
Related Linux Commands
You can read tutorials of related Linux commands below:
- scp Command- rsync Command * wget Command ## Summary In this comprehensive tutorial, we’ve covered the essential aspects of using sftp for secure interactive file transfer. sftp provides a powerful and secure way to manage files on remote systems with full SSH encryption. Key takeaways:- sftp provides an interactive session for secure file transfer over SSH - Supports both upload and download operations with resume capability - Offers full file management including navigation, permissions, and directory operations - Can be used interactively or with batch scripts for automation - More feature-rich than scp but requires interactive session management - Essential for system administrators who need secure file management capabilities Visit our Linux Commands guide to learn more about using command line interface in Linux.