Introduction
Secure File Transfer Protocol (SFTP) is an essential tool for securely transferring files between servers. This guide will walk you through various SFTP connection methods, including password authentication and SSH key authentication, on different ports.
Pre-requirements
- Access to a command-line interface (Terminal on Linux/macOS, PowerShell or Command Prompt on Windows)
- SFTP client (usually comes pre-installed on Linux and macOS, may need installation on Windows)
- Server hostname or IP address
- User credentials (username and password or SSH key)
- SSH key (for key-based authentication)
- Proper permissions to access the server
SFTP Connection Methods
1. Basic Password Authentication (Port 22)
Use this method when connecting with a password on the default SFTP port (22):
sftp username@hostname.example.com
Replace 'username' with your account username and 'hostname.example.com' with your server's hostname or IP address.
2. SSH Key Authentication (Port 22)
For enhanced security, use SSH key authentication:
sftp -i /path/to/your/private_key username@hostname.example.com
Replace '/path/to/your/private_key' with the actual path to your SSH private key file.
3. SSH Key Authentication (Custom Port)
If your server uses a non-standard port (e.g., 2222), use one of these commands:
sftp -P 2222 -i /path/to/your/private_key username@hostname.example.com
If the above doesn't work due to software version incompatibility, try:
sftp -oPort=2222 -i /path/to/your/private_key username@hostname.example.com
Navigating and Transferring Files
1. Getting Help
Once connected, use the help command to see available SFTP commands:
help
2. Transferring Files
To download a file from the remote server to your local machine:
get /remote/path/to/file.txt /local/path/to/destination/
To upload a file from your local machine to the remote server:
put /local/path/to/file.txt /remote/path/to/destination/
3. Exiting SFTP
To close the SFTP connection and return to your local shell:
exit
Best Practices
- Always use SSH key authentication when possible for enhanced security
- Regularly update your SSH keys and never share your private key
- Use strong, unique passwords if you must use password authentication
- Always verify the server's fingerprint when connecting for the first time
- Use aliases or SSH config files to simplify complex connection strings
- Consider using SFTP clients with graphical interfaces for easier file management
Common Issues and Solutions
- Connection refused: Ensure the SFTP service is running and the port is open on the server
- Permission denied: Verify your username, password, or SSH key permissions
- Host key verification failed: If you're sure the server is correct, update your known_hosts file
- Slow transfers: Check your network connection or try using compression (-C option)
- Can't connect with SSH key: Ensure the key is in the correct format and has the right permissions (typically 600)
Troubleshooting
If you're unable to connect to your account via SFTP, try these steps:
- Verify your credentials and server information
- Check if you can connect via SSH
- Ensure SFTP is enabled on the server
- Check your firewall settings
- If using cPanel and still encountering issues, open a support ticket with the technical support team
Conclusion
Mastering SFTP connections is crucial for secure and efficient file transfers. By understanding different authentication methods and troubleshooting common issues, you can ensure smooth and secure file management across your servers.