File Transfer (SFTP) | Linux

2024/01/23

Secure File Transfer Protocol (SFTP) is a protocol for transferring files over a secure channel, typically based on the SSH (Secure Shell) protocol. SFTP provides a secure method that allows users to transfer files between local and remote Linux systems while protecting the integrity of data transmission.

Since SFTP is based on SSH connections, if you are not familiar with SSH connections, you can check out the SSH Connection with Public/Private Keys article, which includes an introduction to SSH configuration.

SFTP Connection Commands

# Connect using username and password
sftp user@remote_host
# Connect using public/private key pair
sftp -i ~/.ssh/id_rsa user@remote_host

## View the current working directory path on the local host
sftp> lpwd

## View the current working directory path on the remote host
sftp> pwd

## Change the working directory path on the local host
sftp> lcd /path/to/local/directory

## Change the working directory path on the remote host
sftp> cd /path/to/remote/directory

## List files in the current working directory on the local host
sftp> lls

## List files in the current working directory on the remote host
sftp> ls

## Upload a file to the remote host
sftp> put <filename>

## Download a file from the remote host to the local host and rename it
sftp> get <filename> <rename filename>

## Exit sftp
sftp> exit