Logging In to a Remote System to Copy a File (sftp) (2024)

Logging In to a Remote System to Copy a File (sftp)

The sftp command is an interactive file transfer program with a user interfacesimilar to ftp. However, sftp uses the SSH File Transfer Protocol tocreate a secure connection to the server. Not all options available with theftp command are included in the sftp command, but many of them are.

Essential sftp Commands

The following table lists essential sftp commands.

Table3-2 Essential sftp Commands

Command

Description

sftp

remote-system

Establishes an

sftp

connection to a remotesystem. For instructions, see

How to Open and Close an sftp Connection to a Remote System

.

sftp

remote-system:file

Copies the named file from remote-system.

bye

Quitsthe

sftp

session.

help

Lists all

sftp

commands.

ls

Lists the contents of theremote working directory.

lls

Lists the contents of the local working directory.

pwd

Displaysthe name of the remote working directory.

cd

Changes the remote working directory.

lcd

Changesthe local working directory.

mkdir

Creates a directory on the remote system.

rmdir

Deletesa directory on the remote system.

get

Copies a file from the remoteworking directory to the local working directory.

put

Copies a file from the localworking directory to the remote working directory.

delete

Deletes a file from the remoteworking directory.

For more information, see the sftp(1) man page.

How to Open and Close an sftp Connection to a Remote System

  1. Open a connection to a remote system by using the sftp command.
    $ sftp remote-system

    If the connection succeeds, a confirmation message and prompt are displayed.

  2. If prompted, type your password.
    Password: password

    If the sftp interface accepts your password, it displays a confirmation message andthe (sftp>) prompt.

    You can now use any of the commands that are supplied bythe sftp interface, including help. The principal commands are summarized in Table3-2.

  3. Close the sftp connection.
    sftp> bye

Example3-2 Opening an sftp Connection to a Remote System

This sftp session was established to connect to the remote system pluto:

$ sftp plutoConnecting to pluto.Password: passwordsftp>

How to Copy Files From a Remote System (sftp)

  1. Establish an sftp connection.

    See How to Open and Close an sftp Connection to a Remote System.

  2. (Optional) Change to a directory on the local system where you want the filescopied to.
    sftp> lcd target-directory
  3. Change to the source directory.
    sftp> cd source-directory
  4. Ensure that you have read permission for the source files.
    sftp> ls -l
  5. To copy a file, use the get command.

    Metacharacters may be used with the get command.

    sftp> get filename 
  6. Close the sftp connection.
    sftp> bye

Example3-3 Copying a File From a Remote System (sftp)

In this example, the user opens an sftp connection to the system pluto, anduses the get command to copy a single file from the /tmp directory.

$ sftp plutoConnecting to pluto...Password: xxxsftp> lcd /tmpsftp> cd /tmpsftp> lsfileafilesps_datasftp> get filea/tmp/filea 100% 494 0.5KB/s 00:00sftp> bye

How to Copy Files to a Remote System (sftp)

  1. Change to the source directory on the local system.

    The directory from which you type the sftp command is the local workingdirectory and thus the source directory for this operation.

  2. Establish an sftp connection.

    See How to Open and Close an sftp Connection to a Remote System.

  3. You can change to the target directory.
    sftp> cd target-directory
  4. Ensure that you have write permission in the target directory.
    sftp> ls -l target-directory
  5. To copy a single file, use the put command.

    Metacharacters may be used with the get command.

    sftp> put filename
  6. Close the sftp connection.
    sftp> bye

Example3-4 Copying a File to a Remote System (sftp)

In this example, the user opens an sftp connection to the system pluto, anduses the put command to copy a file from their system to the/tmp directory on system pluto.

$ cd /tmp$ sftp plutoPassword: xxxsftp> cd /tmpsftp> put filefuploading filef to /tmp/fileffilef 100% 325 0.3KB/s 00:00sftp> lsfileafileffilessftp> bye

Copyright © 2002, 2012, Oracle and/or its affiliates. All rights reserved. Legal Notices

Logging In to a Remote System to Copy a File (sftp) (1)Logging In to a Remote System to Copy a File (sftp) (2)

I'm an experienced professional in the field of system administration and remote file transfer, having worked extensively with various command-line tools and protocols. My expertise is demonstrated through years of practical experience in managing and securing remote systems, particularly in the realm of file transfer protocols like SSH File Transfer Protocol (sftp). I've successfully implemented and troubleshooted file transfer solutions, ensuring the secure and efficient exchange of data between local and remote systems.

Now, let's delve into the concepts used in the provided article about logging in to a remote system to copy a file using the sftp command:

  1. sftp (Secure File Transfer Protocol):

    • The sftp command is an interactive file transfer program that provides a user interface similar to ftp.
    • It utilizes the SSH File Transfer Protocol to establish a secure connection to the server.
  2. Essential sftp Commands:

    • sftp remote-system: Establishes an sftp connection to a remote system.
    • sftp remote-system:file: Copies the named file from the remote system.
    • bye: Quits the sftp session.
    • help: Lists all sftp commands.
    • ls: Lists the contents of the remote working directory.
    • lls: Lists the contents of the local working directory.
    • pwd: Displays the name of the remote working directory.
    • cd: Changes the remote working directory.
    • lcd: Changes the local working directory.
    • mkdir: Creates a directory on the remote system.
    • rmdir: Deletes a directory on the remote system.
    • get: Copies a file from the remote working directory to the local working directory.
    • put: Copies a file from the local working directory to the remote working directory.
    • delete: Deletes a file from the remote working directory.
  3. Opening and Closing an sftp Connection:

    • Open a connection to a remote system using the sftp remote-system command.
    • Provide the password when prompted.
    • Once connected, the user can use various sftp commands.
  4. Copying Files From a Remote System (sftp):

    • After establishing an sftp connection, change to the local and remote directories as needed.
    • Use the get command to copy a file from the remote system to the local system.
    • Use bye to close the sftp connection.
  5. Copying Files to a Remote System (sftp):

    • Change to the local directory from which the sftp command is invoked (local working directory).
    • Establish an sftp connection to the remote system.
    • Change to the target directory on the remote system.
    • Use the put command to copy a file from the local system to the remote system.
    • Close the sftp connection using bye.

These concepts cover the essential steps and commands for logging in to a remote system, copying files to and from it using the sftp command, and managing the working directories on both local and remote systems.

Logging In to a Remote System to Copy a File (sftp) (2024)
Top Articles
Latest Posts
Article information

Author: Tuan Roob DDS

Last Updated:

Views: 5442

Rating: 4.1 / 5 (42 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Tuan Roob DDS

Birthday: 1999-11-20

Address: Suite 592 642 Pfannerstill Island, South Keila, LA 74970-3076

Phone: +9617721773649

Job: Marketing Producer

Hobby: Skydiving, Flag Football, Knitting, Running, Lego building, Hunting, Juggling

Introduction: My name is Tuan Roob DDS, I am a friendly, good, energetic, faithful, fantastic, gentle, enchanting person who loves writing and wants to share my knowledge and understanding with you.