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 sftpconnection 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 sftpsession. |
help | Lists all sftpcommands. |
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
- 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.
- If prompted, type your password.
Password: password
If the sftp interface accepts your password, it displays a confirmation message andthe (sftp>) prompt.
See AlsoWhat is SSH Public Key Authentication?PEM, DER, CRT, and CER: X.509 Encodings and Conversions - SSL.comUnderstanding and Using Secure Shell (SSH) and Secure File Transfer Protocol (SFTP)Generating a Secure Shell (SSH) Public/Private Key PairYou can now use any of the commands that are supplied bythe sftp interface, including help. The principal commands are summarized in Table3-2.
- 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)
- Establish an sftp connection.
See How to Open and Close an sftp Connection to a Remote System.
- (Optional) Change to a directory on the local system where you want the filescopied to.
sftp> lcd target-directory
- Change to the source directory.
sftp> cd source-directory
- Ensure that you have read permission for the source files.
sftp> ls -l
- To copy a file, use the get command.
Metacharacters may be used with the get command.
sftp> get filename
- 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)
- 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.
- Establish an sftp connection.
See How to Open and Close an sftp Connection to a Remote System.
- You can change to the target directory.
sftp> cd target-directory
- Ensure that you have write permission in the target directory.
sftp> ls -l target-directory
- To copy a single file, use the put command.
Metacharacters may be used with the get command.
sftp> put filename
- 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 |
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:
-
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.
-
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.
-
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.
- Open a connection to a remote system using the
-
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.
-
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.