How to use the command 'sftp' (with examples)
The ‘sftp’ command is a powerful tool used for securely transferring files between systems over a network using the Secure Shell (SSH) protocol. It allows users to manage files and directories on remote servers securely, ensuring data integrity and privacy. Unlike traditional FTP, SFTP encapsulates both command and data in a secure channel, preventing potential interception or manipulation by unauthorized third parties. This command is particularly useful for system administrators and developers who need to move files across different machines while maintaining security.
Use case 1: Connect to a remote server and enter an interactive command mode
Code:
sftp remote_user@remote_host
Motivation:
This use case is the foundational step for transferring files via SFTP. It opens an interactive session on a remote server, allowing users to execute a variety of file management commands such as uploading, downloading, and listing files. Essentially, it provides a secure environment to manage files directly on a remote server.
Explanation:
sftp
: Invokes the secure file transfer program.remote_user@remote_host
: Specifies the username and the domain/IP of the remote machine to connect to. The “remote_user” is the account on the remote machine, and “remote_host” is the name or IP address of the remote server.
Example Output:
Connecting to remote_host...
remote_user@remote_host's password:
sftp>
Upon successful connection, you’ll be prompted for a password and then enter the ‘sftp’ prompt, ready for further file operations.
Use case 2: Connect using an alternate port
Code:
sftp -P remote_port remote_user@remote_host
Motivation:
Some servers are configured to use a port other than the default SSH port (22) due to security reasons or other configurations. This use case demonstrates how to specify an alternate port when connecting to ensure the connection is successful.
Explanation:
sftp
: Starts the secure file transfer program.-P remote_port
: The flag-P
specifies the port number to connect to.remote_port
should be replaced by the actual port number the server is configured to accept SFTP connections on.remote_user@remote_host
: Just like the previous example, this specifies the username and the server to connect.
Example Output:
Connecting to remote_host...
remote_user@remote_host's password:
sftp>
The output is akin to the previous use case, indicating a successful connection, but this time through a specified port.
Use case 3: Connect using a predefined host (in ~/.ssh/config
)
Code:
sftp host
Motivation:
Using the SSH configuration file (~/.ssh/config
), you can define various connection details for different hosts, such as alternate ports, user names, and specific SSH keys. This use case allows for the simplification of the connection command, enhancing convenience and efficiency by using these predefined settings.
Explanation:
sftp
: Initiates the SFTP protocol.host
: Refers to an alias or hostname defined in the SSH configuration file. This alias maps to a specific host configuration, removing the need to manually enter the user and domain details every time.
Example Output:
Connecting to host...
remote_user@host's password:
sftp>
This output signifies a successful connection using the predefined parameters in the SSH configuration file.
Use case 4: Transfer remote file to the local system
Code:
get /path/remote_file
Motivation:
Transferring files from a remote server to a local machine is a common requirement for data backup, analysis, or simply moving documents where they’re needed. This use case clearly demonstrates the simplicity of fetching files directly using SFTP.
Explanation:
get
: A command used within an interactive SFTP session to download files./path/remote_file
: Specifies the full path to the file on the remote server that needs to be downloaded to the local system.
Example Output:
Fetching /path/remote_file to remote_file
/path/remote_file 100% 489KB 2.5MB/s 00:00
The output shows the progress and completion of the file transfer from the remote server to the local system.
Use case 5: Transfer local file to the remote system
Code:
put /path/local_file
Motivation:
Uploading files to a remote server is crucial for scenarios like deploying web applications, updating servers, or sharing files between users. This use case ensures a secure and efficient way to move local files to remote servers.
Explanation:
put
: An SFTP command used to upload files from the local system to the remote server./path/local_file
: The local path to the file intended to be uploaded to the remote server.
Example Output:
Uploading /path/local_file to /remote_directory/local_file
/path/local_file 100% 432KB 1.5MB/s 00:00
This output indicates the file’s successful transfer from the local system to the remote directory.
Use case 6: Transfer remote directory to the local system recursively (works with put
too)
Code:
get -R /path/remote_directory
Motivation:
At times, entire directories need to be copied, not just individual files. This use case allows for copying the entire directory structure and its contents recursively, making it easier to handle larger data sets or projects containing multiple files.
Explanation:
get
: Initiates the file download within an SFTP session.-R
: A flag indicating recursive copying, which means that all subdirectories and files in the specified directory will be included./path/remote_directory
: The path to the directory on the remote server that needs to be downloaded.
Example Output:
Fetching /path/remote_directory to remote_directory
Fetching /path/remote_directory/file1 to remote_directory/file1
Fetching /path/remote_directory/file2 to remote_directory/file2
/path/remote_directory 100% 2.1MB 4.0MB/s 00:01
This output shows the recursive fetching of directories and files, providing information about each file’s progress.
Use case 7: Get list of files on local machine
Code:
lls
Motivation:
While working on an SFTP session, keeping track of what’s present on your local system can be beneficial, especially when handling file transfers and needing to verify the existence of files. This command provides that list without having to exit the SFTP environment.
Explanation:
lls
: Stands for “local list” and displays the contents of the current working directory on the local machine.
Example Output:
file1 file2 directory1
The output lists all files and directories present in the local current working directory.
Use case 8: Get list of files on remote machine
Code:
ls
Motivation:
Listing files on the remote server is invaluable for verifying the presence of specific files, assessing directory structures, or just gaining a quick overview of the available content.
Explanation:
ls
: This command, used in an SFTP session, lists the contents of the current working directory on the remote system.
Example Output:
fileA fileB directoryA
This output displays all files and directories at the location on the remote server where you’re currently navigated to.
Conclusion:
‘SFTP’ is a crucial tool in the arsenal of any professional who routinely manages files across different systems in a secure fashion. The examples provided show its versatility, from simple file transfers to complex recursive directory copying, all wrapped in a secure, encrypted protocol to ensure data integrity. Whether for casual use or within large-scale systems management, mastering ‘sftp’ can lead to significant efficiency gains and bolster security in the file transfer processes.