Enhancing Skills

sftp: Secure File Transfer Protocol, for transferring files between hosts

Command: sftp

Used to securely transfer files between hosts over a network. It provides a secure alternative to traditional FTP by using SSH for data transfer, ensuring encryption and secure authentication.


Sample Command and Output:

$ sftp user@remote_host
Connecting to remote_host...
user@remote_host's password: 
sftp> ls
file1.txt  file2.txt  directory/
sftp> get file1.txt
Fetching file1.txt to file1.txt
sftp> put localfile.txt
Uploading localfile.txt to /path/to/remote/directory/localfile.txt
sftp> exit

Description:

  • sftp user@remote_host: Connects to the remote host using SFTP with the specified username (user). You will be prompted for the password of the remote user account.
  • ls: Lists files and directories in the current remote directory.
  • get file1.txt: Downloads the file file1.txt from the remote server to the local machine.
  • put localfile.txt: Uploads the file localfile.txt from the local machine to the remote server.
  • exit: Closes the SFTP session.

Additional Commands and Sample Outputs:

  • sftp -oPort=2222 user@remote_host: Connect to the remote host on a different port (e.g., port 2222). Sample Command and Output:
  $ sftp -oPort=2222 user@remote_host
  Connecting to remote_host port 2222...
  user@remote_host's password: 

Description:

  • sftp -oPort=2222 user@remote_host: Specifies a port number for the connection if the remote server is listening on a port other than the default port 22.
  • sftp user@remote_host <<EOF: Use a here-document to automate file transfers. Sample Command and Output:
  $ sftp user@remote_host <<EOF
  get remote_file.txt
  put local_file.txt
  bye
  EOF

Description:

  • sftp user@remote_host <<EOF ... EOF: Automates SFTP commands by feeding them from a script or command-line input. This example downloads remote_file.txt and uploads local_file.txt, then exits.
  • sftp -b batchfile.txt user@remote_host: Use a batch file for non-interactive file transfers. Sample Command and Output:
  $ sftp -b batchfile.txt user@remote_host

Description:

  • sftp -b batchfile.txt user@remote_host: Executes a series of SFTP commands listed in batchfile.txt. This is useful for scripting and automating file transfers.

Note: Ensure that SSH is configured properly on both the local and remote hosts to use SFTP.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.