
The sftp (Secure File Transfer Protocol) command in Linux allows you to securely transfer files between local and remote systems over SSH. The SFTP command ensures data integrity and confidentiality by encrypting commands and data during transit, unlike traditional FTP applications. SFTP includes an interactive shell environment for downloading, uploading, and managing files.
This article explains how to use the sftp command to securely transfer files and directories in Linux.
Before you begin:
local_server and remote_server workstations respectively. Enable the limited user login feature on each instance.sftp Command SyntaxThe following is a basic sftp command syntax.
In the above sftp command:
[options]: Includes optional flags that modify the file transfer process.[user@host]: specifies the user and remote host when transferring files.Explore how the tar command in Linux enables efficient file compression and archiving for streamlined data storage and transfer.
sftp Command OptionsBelow are the most common sftp command options in Linux:
This article uses the following usernames and example hostnames. Replace the values with your actual usernames and server public IP addresses.
Follow the steps below to create new sample files and directories to test the sftp command on your local_server and remote_server.
Create three .txt sample files on your local server.
Create a new dir sample directory on your local server.
Create three .txt sample files on the remote server.
Create new dir1 and dir2 sample directories on the remote server.
sftp Command in LinuxFollow the steps below to securely transfer files using the sftp command to your remote server.
Connect to the remote server using the sftp command to open the interactive file transfer shell.
Enter yes to accept the remote server's fingerprint key and enter the user password when prompted.
Output:
List files under the user's home directory on the remote server.
Output:
Upload file1.txt from your local server to the dir2 directory on the remote server.
Output:
Download file4.txt from the remote server's working directory to your local server.
The above command downloads the file4.txt file from the working directory on your remote server and saves it to the working directory on your local server directory.
Output:
Upload the dir directory from the local server to the remote server using the recursive -r option.
The above command uses the recursive -r option to copy the dir directory and its contents from your local server to the remote server user's home directory /home/linuxuser2.
Output:
Download the dir1 directory from the remote server to your local server.
This command uses the -r option to recursively download the dir1 directory from the remote server to your local server user's home directory.
Output:
SFTP uses password authentication by default unless you specify a private key using the -i option. SSH Keys allow SFTP clients to connect to remote hosts without a password. Follow the steps below to set up a new SSH key and enable passwordless authentication.
Generate a new SSH key pair on your local server.
Press Enter to accept the default SSH file location, such as /home/linuxuser1/.ssh/id_rsa, and set an optional passphrase to secure the SSH key.
Output:
Copy your SSH public key to the remote server.
Enter the remote user's password when prompted to copy the SSH public key.
Output:
Access the remote server using SSH and verify that you are not prompted for the user's password.
Output:
Exit the SSH console.
Connect to the remote server using SFTP.
Upload file2.txt from your local server to the dir2 directory on the remote server.
Output:
sftp Command Advanced OptionsFollow the steps below to use advanced options with the sftp command in Linux.
Copy file3.txt to the remote server and preserve the original file's attributes.
The above command copies the file3.txt file to the /home/linuxuser2 user home directory on the remote server. The -p option keeps the file's original timestamps and permissions during transfer.
Output:
Use the -C option to enable compression and speed up the SFTP transfer.
Download file5.txt from the remote server to the dir directory on your local server.
Exit the SFTP shell.
Output:
Use the -i option to set a specific private key for authentication. Replace /path/to/private_key with your private key path.
Run the following command in your user home directory to view the default SSH key files.
Find the id_rsa file in your files to use as the private key that corresponds to your public key file with a .pub extension.
Output:
Download file6.txt file from the remote server to your local server.
Exit the SFTP shell.
Output:
Execute multiple sftp commands at once using a batch file. For example, create a new sftp_batch.txt file on your local server.
Add the following contents to the file.
Save and close the file.
Enable execute permissions on the file.
Connect to the remote server using the -b option and specify the batch file to execute.
The above sftp command executes all commands in the sftp_batch.txt batch file using the -b to transfer files to the remote server.
Output:
Use the -P option to connect to the remote server using a custom SSH port to transfer files. For example, the default SSH port 22.
Upload file1.txt from your local server to the dir2 directory on the remote server.
Output:
SFTP supports interactive shell commands for navigating and interacting with files on the remote server.
Follow the steps below to use interactive commands in your SFTP session.
Print the working remote directory in your SFTP session.
Output:
List files in the remote working directory.
Output:
List files in the local server's working directory using the lls command.
The lls command lists all files in the local working directory enabling you to view and verify files downloaded from the remote server.
Output:
Create a new directory on the remote server.
List files in the remote working directory and verify that test_directory is available.
Output:
Remove the test_directory from the remote server..
Rename file2.txt on the remote server to example.txt.
List files in the remote working directory and verify that example.txt is available.
Output:
Delete example.txt from the remote server.
Output:
Switch to the dir2 on the remote server.
Print the working directory on your local server.
Output:
Switch to the dir directory on your local server.
Print the working directory on your local server and verify the dir directory path.
Output:
Access a new shell on your local server.
The above ! command creates a new sub-shell on your local server within the sftp session.
View your server's storage usage in the new shell using the df -h command.
Output:
Exit the new shell.
Output:
Exit the SFTP session.
You have used the sftp command in Linux to transfer files from one host to another. You can use the sftp command with other Linux commands and options to securely transfer files and directories between hosts. For more information and command options, run man sftp to view the SFTP command manual.
0 Comments
Be the first to comment and share your perspective with the community.