2024-03-20
sftpBefore you begin, ensure you have SSH access to your remote server. This usually involves having an SSH client installed (like OpenSSH, which is pre-installed on many systems) and knowing the server’s IP address or hostname, as well as your username and password (or SSH key).
The basic syntax for sftp is:
sftp [user@]hostnameLet’s connect to a server named myremote.com with username john:
sftp john@myremote.comYou’ll be prompted for your password. Once connected, you’ll see an sftp> prompt.
sftp CommandsHere are some key commands you’ll frequently use:
get filename: Downloads a file from the remote server to your local directory.
sftp> get remote_file.txtThis will download remote_file.txt to your current working directory.
get remote_file.txt local_file.txt: Downloads a file from the remote server, specifying a local filename.
sftp> get remote_file.txt my_local_copy.txtput filename: Uploads a file from your local directory to the remote server.
sftp> put my_local_file.txtput local_file.txt remote_file.txt: Uploads a file to the server, specifying a remote filename.
sftp> put my_local_file.txt their_file.txtlcd directory: Changes the local working directory.
sftp> lcd /home/john/documentscd directory: Changes the remote working directory.
sftp> cd /var/www/htmlpwd: Displays the current remote working directory.
sftp> pwdlpwd: Displays the current local working directory.
sftp> lpwdls: Lists the files and directories in the current remote directory.
sftp> lslls: Lists the files and directories in the current local directory.
sftp> llsmkdir directory_name: Creates a new directory on the remote server.
sftp> mkdir mynewdirectoryrmdir directory_name: Removes an empty directory on the remote server.
sftp> rmdir myolddirectoryrm filename: Deletes a file on the remote server. Use with caution!
sftp> rm file_to_delete.txtbye or exit: Closes the sftp session.
sftp excels in handling directories. While you can manually get and put individual files, you can also perform recursive transfers. However, direct recursive transfer isn’t a built-in command. You would need to use tools like rsync for this feature, which offers far more advanced options for synchronization and backup.
For instance, to download an entire directory recursively using rsync (which needs to be installed on both your local machine and the remote server), you might use:
rsync -avz john@myremote.com:/path/to/remote/directory /path/to/local/directoryThis command uses rsync to recursively copy (-a), archive mode (-v), compress (-z), from the remote server to your local machine. Remember to replace placeholders with your actual paths. This method is generally safer and more efficient for bulk file transfers than repeatedly using sftp get.
For enhanced security and convenience, consider configuring SSH keys. This eliminates the need to repeatedly enter your password. Setting up SSH keys is outside the scope of this immediate tutorial but it’s a highly recommended security practice.
Common errors might include connection issues (incorrect hostname/IP, network problems), permission errors (lack of read/write access on the remote server), or file-not-found errors. Always double-check your paths and permissions.