How to Use SFTP to Transfer Files

Introduction

Secure File Transfer Protocol (SFTP) is a secure way to transfer files between two computers over the internet. It is a secure alternative to the File Transfer Protocol (FTP) and is often used with web hosting services. SFTP is a great way to transfer files securely and quickly, and it is easy to set up and use. In this guide, we will explain how to use SFTP to transfer files between two computers.

How to Use SFTP to Transfer Files

1. Download and install an SFTP client. Popular SFTP clients include FileZilla, WinSCP, and Cyberduck.

2. Open the SFTP client and enter the hostname, username, and password for the remote server.

3. Once connected, you can transfer files between the local and remote computers. To upload a file, drag and drop it from the local computer to the remote computer. To download a file, drag and drop it from the remote computer to the local computer.

4. When you’re finished transferring files, close the SFTP client.

Introduction

SFTP (Safe File Transfer Protocol) is included along with SSH as a secure way to transfer files between remote systems. It also allows users to perform basic administrative tasks on remote servers, such as managing files and directories and setting file permissions.

In this tutorial, we will show you how to use SFTP to transfer files between a local and a remote system using the terminal interface.

How to use SFTP to transfer files

Prerequisites

  • Access to a local system and a remote server, connected using an SSH public key pair.
  • A working Internet connection.
  • Access to the terminal window.

How to Connect Using SFTP

SFTP establishes a secure connection between systems by using the SSH network protocol. This allows you to connect to any system that has a copy of your public SSH key.

Connecting to another system using SFTP follows the same command syntax as connecting using SSH. The command requires you to provide a username and a remote hostname or IP address for the system you want to access:

sftp [username]@[remote hostname or IP address]

In the example below, we are connecting to a system with the IP address 192.168.100.5 using the phoenixnap username:

sftp [email protected]
Connecting to a remote server using SFTP

To end a current connection, use:

exit

How to Transfer Files Using SFTP

Using SFTP allows you to transfer files from a remote server to a local system and vice versa.

Transfer Remote Files to a Local System

Use the get command in the SFTP interface to transfer a file from a remote server to your local system:

get [path to file]

For example, to transfer a file called example_document.txt from the remote system’s Home directory to the local system, use:

get example_document.txt
Transferring a file from the remote server to the local system

By default, SFTP transfers files to the local system’s Home directory. To transfer files to a different directory, append the path to the directory to the end of the get command:

get example_document.txt Downloads
Transferring a file from the remote server to a specific directory on the local system

To change the filename on the local system, append the new file name to the end of the get command.

get example_document.txt sample01.txt
Transferring a file from the remote server to the local system and changing the file name

In the example above, the get command fetches the example_document.txt file and saves it as sample01.txt on the local system.

SFTP also allows the transfer of an entire directory from the remote system by using the -r flag, indicating a recursive transfer of all files in the directory:

get -r Example_Directory
Transferring a directory from the remote server to the local system

Add the -P flag to the get command to transfer the file or directory while preserving permissions and access times:

get -Pr Example_Directory

Use the ls command to verify the transfer to the local system:

ls -l
Verifying that the files were transferred to the local system

Transfer Local Files to a Remote Server

To transfer files from a local system to a remote server, use the put command. The put command uses the same syntax and options as the get command.

put [path to file]

For example, to transfer an example01.txt file to the remote server, use:

put example01.txt
Transferring a file from the local system to the remote server

To transfer the file to a specific directory on the remote server, append the path to the directory to the end of the put command.

put example01.txt Example_Directory
Transferring a file from the local system to a specific directory on the remote server

Appending a new filename to the end of the put command changes the name of the transferred file on the remote server.

put example01.txt text_sample.txt
Transferring a file from the local system to the remote server and changing the file name

Transferring an entire directory requires the -r flag.

put -r Test_Directory
Transferring a directory from the local system to the remote server

Add the -P flag to the put command to preserve file permissions:

put -Pr Test_Directory

Verify the file transfer by using the ls command on the remote system:

Verifying that the files were transferred to the remote server

Note: Refer to our ultimate guide for SFTP Commands to learn more about the available options and the most common use cases.

File Maintenance Using SFTP

SFTP supports basic file maintenance. For example, use SFTP to modify file and directory permissions on a remote system.

The chown command changes the file ownership similar to the chmod command:

chown [user ID] [path to file]

Unlike the chmod command, chown accepts user IDs only and not usernames. Finding the UIDs for the remote server using the SFTP interface requires you to transfer and access the /etc/passwd file:

get /etc/passwd
!less passwd

The UID for each user can be found in the third column, as separated by the colons:

Using the passwd file to find user IDs for the remote server

The chmod command works the same as in the standard shell:

chmod [permission] [path to file]

Another option is to change group file ownership with the chgrp command:

chgrp [group ID] [path to file]

Same as with the UIDs, the group IDs are found in the third column of the /etc/group file on the remote server:

get /etc/group
!less group
Using the group file to find group IDs for the remote server

SFTP allows you to set a local umask, which changes the default file permission for the files transferred to the local system.

For instance:

lumask 022

The command above changes the local umask to 022. Every file transferred after you set this umask now has the 644 permission by default. You can still preserve the original permission by using the -p flag.

Another way to change local file permissions is to use SFTP to replicate the behavior of shell commands. To do this, add an exclamation point (!) before the command name.

For instance, using the chmod command on the local system:

!chmod [permission] [path to file]

Conclusion

After reading this tutorial, you should be able to use SFTP to transfer files and directories between remote systems. For more information about transferring files, have a look at our guide to installing an FTP server on Ubuntu.

How to Use SFTP to Transfer Files

Secure File Transfer Protocol (SFTP) is a secure way to transfer files between two computers over the internet. It is a protocol that uses SSH (Secure Shell) to transfer files between two computers. SFTP is a secure alternative to FTP (File Transfer Protocol) and is used to transfer files between two computers over the internet. In this article, we will discuss how to use SFTP to transfer files.

Step 1: Install an SFTP Client

The first step in using SFTP to transfer files is to install an SFTP client. An SFTP client is a program that allows you to connect to an SFTP server and transfer files. There are many different SFTP clients available, such as FileZilla, WinSCP, and Cyberduck. Once you have chosen an SFTP client, you can download and install it on your computer.

Step 2: Connect to the SFTP Server

Once you have installed an SFTP client, you can connect to the SFTP server. To do this, you will need the IP address or hostname of the SFTP server, as well as the username and password for the server. Once you have entered this information, you can connect to the SFTP server.

Step 3: Transfer Files

Once you have connected to the SFTP server, you can begin transferring files. To do this, you will need to navigate to the directory on the SFTP server where you want to transfer the files. You can then select the files you want to transfer and click the “Upload” or “Download” button to transfer the files. You can also use the “Copy” and “Move” commands to copy or move files between directories on the SFTP server.

Step 4: Disconnect from the SFTP Server

Once you have finished transferring files, you can disconnect from the SFTP server. To do this, you can click the “Disconnect” button in the SFTP client. This will close the connection to the SFTP server and you will no longer be able to transfer files.

Conclusion

Using SFTP to transfer files is a secure and efficient way to transfer files between two computers over the internet. To use SFTP, you will need to install an SFTP client, connect to the SFTP server, transfer the files, and then disconnect from the SFTP server. With these steps, you can easily use SFTP to transfer files.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?