2024-03-20
sftp
Before 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@]hostname
Let’s connect to a server named myremote.com
with username john
:
sftp john@myremote.com
You’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.txt
This 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.txt
put filename
: Uploads a file from your local directory to the remote server.
sftp> put my_local_file.txt
put local_file.txt remote_file.txt
: Uploads a file to the server, specifying a remote filename.
sftp> put my_local_file.txt their_file.txt
lcd directory
: Changes the local working directory.
sftp> lcd /home/john/documents
cd directory
: Changes the remote working directory.
sftp> cd /var/www/html
pwd
: Displays the current remote working directory.
sftp> pwd
lpwd
: Displays the current local working directory.
sftp> lpwd
ls
: Lists the files and directories in the current remote directory.
sftp> ls
lls
: Lists the files and directories in the current local directory.
sftp> lls
mkdir directory_name
: Creates a new directory on the remote server.
sftp> mkdir mynewdirectory
rmdir directory_name
: Removes an empty directory on the remote server.
sftp> rmdir myolddirectory
rm filename
: Deletes a file on the remote server. Use with caution!
sftp> rm file_to_delete.txt
bye
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/directory
This 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.